Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

generate valid sudoku board python

base  = 3
side  = base*base

# pattern for a baseline valid solution
def pattern(r,c): return (base*(r%base)+r//base+c)%side

# randomize rows, columns and numbers (of valid base pattern)
from random import sample
def shuffle(s): return sample(s,len(s)) 
rBase = range(base) 
rows  = [ g*base + r for g in shuffle(rBase) for r in shuffle(rBase) ] 
cols  = [ g*base + c for g in shuffle(rBase) for c in shuffle(rBase) ]
nums  = shuffle(range(1,base*base+1))

# produce board using randomized baseline pattern
board = [ [nums[pattern(r,c)] for c in cols] for r in rows ]

for line in board: print(line)

[6, 2, 5, 8, 4, 3, 7, 9, 1]
[7, 9, 1, 2, 6, 5, 4, 8, 3]
[4, 8, 3, 9, 7, 1, 6, 2, 5]
[8, 1, 4, 5, 9, 7, 2, 3, 6]
[2, 3, 6, 1, 8, 4, 9, 5, 7]
[9, 5, 7, 3, 2, 6, 8, 1, 4]
[5, 6, 9, 4, 3, 2, 1, 7, 8]
[3, 4, 2, 7, 1, 8, 5, 6, 9]
[1, 7, 8, 6, 5, 9, 3, 4, 2]
Comment

PREVIOUS NEXT
Code Example
Python :: Make solutions faster in python 
Python :: schedule task to midnight python 
Python :: Slicing lexicographically pandas 
Python :: hot to pay music in pygame 
Python :: check version numpy 
Python :: text to speech to specific language python 
Python :: python print time difference 
Python :: convert time zone pandas 
Python :: how to check for duplicates in a column in python 
Python :: askopenfilename 
Python :: ax set xtick size 
Python :: np array describe 
Python :: how to print not equal to in python 
Python :: Change the year in 2nd line to get the answer for the year you want. Ex: year=2010 
Python :: python overwrite text that is already printed 
Python :: pil image from numpy 
Python :: latest django version 
Python :: python subtract 2 strings 
Python :: django genericforeignkey null 
Python :: how to create notification in python 
Python :: combinations python 
Python :: how to save a dictionary as a file in python 
Python :: oppsite of abs() python 
Python :: plt axis tick color 
Python :: How to make an simple python client 
Python :: cmd python -m 
Python :: return column of matrix numpy 
Python :: python no new line 
Python :: fstring number format python 
Python :: decision tree gridsearchcv 
ADD CONTENT
Topic
Content
Source link
Name
8+6 =