1- import math
1+ import math
2+
23
34def 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+
1922def 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+
2731def 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+
4752def 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+
7481def 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+
96107def 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+
102114def 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+
119132if __name__ == "__main__" :
120133 main ()
0 commit comments