Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d0eabe8

Browse files
style: format code with autopep8
Format code with autopep8 This commit fixes the style issues introduced in 7bd2e9e according to the output from Autopep8. Details: None
1 parent 7bd2e9e commit d0eabe8

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

‎KenKen Puzzle Solver/kenken_puzzle_solver.py‎

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import math
1+
import math
2+
23

34
def is_valid(board, row, col, num, group_size):
45
# Check if 'num' can be placed in the given position without violating the rules
@@ -8,14 +9,16 @@ def is_valid(board, row, col, num, group_size):
89
return False
910

1011
# Check the group constraints
11-
group_start_row, group_start_col = row - row % group_size, col - col % group_size
12+
group_start_row, group_start_col = row - \
13+
row % group_size, col - col % group_size
1214
for i in range(group_size):
1315
for j in range(group_size):
1416
if board[group_start_row + i][group_start_col + j] == num:
1517
return False
1618

1719
return True
1820

21+
1922
def find_empty_cell(board):
2023
# Find an empty cell (cell with value 0) on the board and return its coordinates
2124
for i in range(len(board)):
@@ -24,6 +27,7 @@ def find_empty_cell(board):
2427
return i, j
2528
return None
2629

30+
2731
def solve_kenken(board, group_size):
2832
# Function to solve the KenKen puzzle using backtracking
2933
empty_cell = find_empty_cell(board)
@@ -44,6 +48,7 @@ def solve_kenken(board, group_size):
4448

4549
return False
4650

51+
4752
def validate_puzzle_input(size, groups):
4853
# Function to validate the user-provided KenKen puzzle input
4954
# Check for the following conditions:
@@ -63,25 +68,30 @@ def validate_puzzle_input(size, groups):
6368
valid_operations = {'+', '-', '*', '/'}
6469
for target, operation in groups:
6570
if target <= 0:
66-
print("Invalid target number. The target number should be greater than 0 for each group.")
71+
print(
72+
"Invalid target number. The target number should be greater than 0 for each group.")
6773
return False
6874
if operation not in valid_operations:
6975
print("Invalid operation. Valid operations are '+', '-', '*', or '/'.")
7076
return False
7177

7278
return True
7379

80+
7481
def get_puzzle_input():
7582
# Function to get the KenKen puzzle input from the user
7683
size = int(input("Enter the size of the grid (e.g., 5 for a 5x5 puzzle): "))
77-
group_size = int(input("Enter the size of each group (e.g., 3 for a standard 9x9 puzzle): "))
84+
group_size = int(
85+
input("Enter the size of each group (e.g., 3 for a standard 9x9 puzzle): "))
7886

7987
groups = []
8088
for i in range(size):
8189
while True:
8290
try:
83-
group_target = int(input(f"Enter the target number for group {i + 1}: "))
84-
group_operation = input(f"Enter the operation for group {i + 1} (+, -, *, /): ")
91+
group_target = int(
92+
input(f"Enter the target number for group {i + 1}: "))
93+
group_operation = input(
94+
f"Enter the operation for group {i + 1} (+, -, *, /): ")
8595
groups.append((group_target, group_operation))
8696
break
8797
except ValueError:
@@ -93,12 +103,14 @@ def get_puzzle_input():
93103
print("Invalid puzzle input. Please try again.")
94104
return None
95105

106+
96107
def print_board(board):
97108
# Function to pretty print the KenKen board
98109
for row in board:
99110
print(" ".join(str(num) if num != 0 else "-" for num in row))
100111
print()
101112

113+
102114
def main():
103115
# Main function to run the KenKen puzzle solver
104116
print("KenKen Puzzle Solver")
@@ -116,5 +128,6 @@ def main():
116128
else:
117129
print("No solution exists.")
118130

131+
119132
if __name__ == "__main__":
120133
main()

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /