Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#TASK 2 IN PYTHON PLS THANK YOU Task 2: Generting Suduko board In the template file, we provided solutions function, this function will return possible

#TASK 2 IN PYTHON PLS THANK YOU

image text in transcribedimage text in transcribed

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

Task 2: Generting Suduko board In the template file, we provided solutions function, this function will return possible solutions to the board as a list of solutions, where each item in this list is a solved board. You don't need to understand how this function works. However, you just need to understand the format of its input and output as you will use this function to extend the behaviour of the play function such that on input 'g' k, (i.e., the letter g followed by an integer k> 1), or 'generate' k, it generates a new random kby k? Sudoku board such that the board has a unique solution. Hints: Think of a good decomposition of this problem. Some ideas for useful subproblems are, e.g., generating a random full game board that is valid (i.e., satisfies the placement constraints of Sudoku) and checking whether a given partially filled board has a unique solution. The partially filled board can be generated by repeatedly removing a number from the board (set that cell to 0) and see if the resulting game board would still have a unique solution. The number to be removed can be selected randomly. You should aim to remove as much numbers as possible. For randomising you can use the function shuffle from the random module. Your initial solutions to this problem are likely rather slow. Make sure to first test with the smallest board size (k =2). 4 2:07 ..l 4G 105 104 sudoku_te sudoku_te mplate_A... mplate_A... 0, 3, 10, 4, 0, 0, 15, 0, 0, 0 0, 0, 0], [ 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 11], [11, 6, 8, 0, 0, 0, 15, 0, 14, 0, 0, 0, 0, 13, 0, 2]] 105 106 107 sudokus = [[], [], [small, small2], [big, big2, big3, big4], [giant, giant2, giant3]] 108 109 ########### Module functions %23%23%23%23%23%23%23%23 ############## 110 111 112 def print_board(board): 113 Prints a given board to the console in a way that aligns the content of columns and makes the subgrids visible. 114 115 116 Input : a Sudoku board (board) of size 4x4, 9x9, or 16x16 Effect: prints the board to the console 117 For example: 118 119 120 121 122 123 >>> print_board(small2) 11 2:07 il 4G sudoku_te mplate_A... sudoku_te mplate_A... X 14 | 124 125 126 127 128 129 | 21 | 31 >>> print_board (big) 130 1 14 789 178 | 561 | 2 | 36 | 8 | | 57| 1 | 18 12 51 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 1|64 197 19 | | 3 | 21 1 >>> print_board(giant2) | 5 | 4 8 6 1 9G| | 1 | D|47| F 81 ID | 73 | 95A 1 | BCFIA | 5 34 DI 148 149 150 151 152 153 154 F 131 721 | 5 1 1 C] 3 5 B E 91 47 C FG | TE F 169 C1 2:07 Il 4G sudoku_te sudoku_te mplate_A... mplate_A... X 13 F4 DE | IB GA | D| 1 78| 1 9A | 7 | 35 | TE GF 9 11 154 155 156 157 158 159 160 161 162 163 164 165 166 167 19 2 E 418 | | | E A1931 171 18 G 1|2 EB4 3 1 1| 5 | 6 6 C 1 print(board) 168 169 170 171 172 def subgrid_values (board, r, c): 173 174 Input : Sudoku board (board), row index (r), and column index (c) 175 Output: list of all numbers that are present in the subgrid of the board related to the 176 field (r, c) 177 178 For example: 179 180 >>> subgrid_values (small2, 1, 3) 181 [1] 2:07 il 4G sudoku_te mplate_A... sudoku_te mplate_A... X 186 187 pass 188 def options (board, i, j): 189 190 191 192 IIIIII 193 Input : Sudoku board (board), row index (r), and column index (c) Output: list of all numbers that player is allowed to place on field (r, c), i.e., those that are not already present in row r, column c, and subgrid related to field (r, c) 194 195 For example: 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 >>> options (small2, 0, 0) [2, 3] >>> options(big, 6, 8) [3, 8] >>> options(giant2, 1, 5) [2, 5, 6, 9, 11, 12, 16] pass def hint (board): Input : Sudoku board (board) Output: print board with a hint X 2:07 il 4G . sudoku_te mplate_A... sudoku_te mplate_A... X 111111 214 215 def play(board): 216 217 Input : Sudoku board 218 Effect: Allows user to play board via console 219 220 221 print_board(board) 222 while True: 223 inp = input().split(' ') 224 if len(inp) == 3 and inp[0].isdecimal() and inp[1].isdecimal() and inp [2].isdecimal(): 225 i = int(inp[0]) 226 j = int(inp[1]) 227 x = int(inp[2]) 228 if x in options (board, i, j): 229 board[i][j] = x 230 else: 231 print("Error ") 232 print_board(board) 233 elif len(inp) ==3 and (inp[0] == 'n' or inp[0] == 'new') and inp[1].isdecimal() and inp[2].isdecimal(): 234 k = int(inp[1]) 235 d = int(inp[2]) 236 if k = 2: 281 return res 282 return res 283 ########### A2 functions 284 ##################### ###### 285 def inferred (board): 287 288 Input : Sudoku board Output: new Soduko board with all 289 values field from input board plus all values that can be 200 Linferred by reneated annlication of X 286

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

3. Describe the communicative power of group affiliations

Answered: 1 week ago