Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

Missing Assignments

Views: 711
Kernel: Python 3 (Anaconda 2020)
#These are included so that the Ex 22 functions may work: def lineSudSolver1(A): '''this function takes a 1D sudoku (a line of numbers containing missing numbers denoted by a '0') and returns the missing numbers''' mnum = [] for num in range(1,10): #iterates through the list if num not in A: #if a number is not in A (the 1D sudoku), returns number mnum.append(num) continue return(mnum) def boxSudSolver1(B): '''takes a 2D array of 9 numbers, finds any missing numbers denoted by 0 and returns a list of those missing numbers''' mnum = [] for num in range(1,10): #iterates through the list if num not in B: #if a number is not in A (the 1D sudoku), returns number mnum.append(num) continue return(mnum)
import numpy as np sudSimp = np.array([ [1, 0, 6, 4, 5, 9, 3, 8, 2], [0, 5, 0, 0, 0, 3, 9, 6, 1], [9, 0, 8, 1, 0, 6, 5, 0, 4], [7, 9, 0, 3, 4, 8, 0, 2, 5], [0, 2, 3, 6, 0, 7, 0, 0, 8], [6, 0, 0, 2, 0, 5, 1, 3, 7], [0, 0, 7, 5, 0, 0, 0, 4, 9], [8, 0, 9, 0, 6, 4, 2, 0, 3], [3, 4, 0, 9, 0, 2, 0, 1, 6] ])
def row(board,x,y): '''takes 3 inputs, a 9x9 board and two coordinates, and outputs a list of numbers that are not in that row''' num = lineSudSolver1(board[x,:]) return(num) print(row(sudSimp,0,1)) print(row(sudSimp,7,7))
[7] [1, 5, 7]
def col(board,x,y): '''takes 3 inputs, a 9x9 board and two coordinates, and outputs a list of numbers that are not in that column''' num = lineSudSolver1(board[:,y]) return(num) print(col(sudSimp,0,1)) print(col(sudSimp,7,7))
[1, 3, 6, 7, 8] [5, 7, 9]
def board(board,x,y): '''takes 3 inputs, a 9x9 board and two coordinates, and outputs in that box a list of numbers''' xmin = 3*(x//3) xmax = xmin + 3 ymin = 3*(y//3) ymax = ymin + 3 box = board[xmin:xmax,ymin:ymax] solved = boxSudSolver1(box) return(solved) print(board(sudSimp,0,1)) print(board(sudSimp,7,7))
[2, 3, 4, 7] [5, 7, 8]