-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorderer.py
52 lines (45 loc) · 1.7 KB
/
orderer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#Adapted from www.101computing.net/sudoku-generator-algorithm/
from random import randint, shuffle
# Use this! This returns a list of count lists of length,
# each with every number in range(count) appearing once in each position
def orderer(count):
global fac_count
fac_count = count
global order
order = [[fac_count + 1] * fac_count for i in range(fac_count)]
global numberList
numberList = list(range(fac_count))
fill_order(order)
return order
# A function to check if the order is full
def check_order(order):
for row in range(fac_count):
for col in range(fac_count):
if order[row][col] == fac_count + 1:
return False
# We have a complete grid!
return True
def fill_order(order):
# Find next empty cell
for i in range(0, fac_count*fac_count):
row = i // fac_count
col = i % fac_count
if order[row][col] == fac_count + 1:
shuffle(numberList)
for value in numberList:
# Check that this value has not already be used on this row
if value not in order[row]:
# Check this value has not already be used on this column
if value not in [order[i][col] for i in range(fac_count)]:
order[row][col] = value
if check_order(order):
return True
else:
if fill_order(order):
return True
break
order[row][col] = fac_count + 1
'''test = orderer(10)
#test output:
for i in test:
print(i)'''