Skip to content

Commit 4628ef8

Browse files
authored
Add files via upload
0 parents  commit 4628ef8

14 files changed

+379
-0
lines changed

in1

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
9
2+
9 3 0 5 6
3+
7 2 1 2
4+
10 3 3 8 13
5+
14 4 4 9 14 19
6+
3 1 7
7+
8 3 10 11 16
8+
13 4 12 17 21 22
9+
5 2 15 20
10+
6 3 18 23 24

in2

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
7
2+
4 3 0 1 6
3+
8 3 2 7 12
4+
14 3 3 4 8
5+
15 4 5 10 11 15
6+
14 6 9 13 14 18 19 24
7+
11 3 16 20 21
8+
9 3 17 22 23

in3

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
8
2+
7 3 0 1 2
3+
12 4 3 4 8 9
4+
17 6 5 7 10 11 12 13
5+
5 1 6
6+
11 3 14 19 24
7+
7 2 15 20
8+
5 3 16 17 21
9+
11 3 18 22 23

instout1

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Number of cages: Cage number 0: Cage number 1: Cage number 2: Cage number 3: Cage number 4: Cage number 5: Cage number 6: Cage number 7: Cage number 8:
2+
---Solution---
3+
4+
3 5 2 1 4
5+
5 1 3 4 2
6+
2 4 1 5 3
7+
1 2 4 3 5
8+
4 3 5 2 1
9+
10+
checks: 2745 backtracks: 534

instout2

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Number of cages: Cage number 0: Cage number 1: Cage number 2: Cage number 3: Cage number 4: Cage number 5: Cage number 6:
2+
---Solution---
3+
4+
1 2 3 4 5
5+
3 1 4 5 2
6+
2 5 1 3 4
7+
5 4 2 1 3
8+
4 3 5 2 1
9+
10+
checks: 470 backtracks: 79

instout3

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Number of cages: Cage number 0: Cage number 1: Cage number 2: Cage number 3: Cage number 4: Cage number 5: Cage number 6: Cage number 7:
2+
---Solution---
3+
4+
1 4 2 5 3
5+
2 5 4 3 1
6+
5 2 3 1 4
7+
4 3 1 2 5
8+
3 1 5 4 2
9+
10+
checks: 2025 backtracks: 390

myout1

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Number of cages: Cage number 0: Cage number 1: Cage number 2: Cage number 3: Cage number 4: Cage number 5: Cage number 6: Cage number 7: Cage number 8:
2+
---Solution---
3+
4+
3 5 2 1 4
5+
5 1 3 4 2
6+
2 4 1 5 3
7+
1 2 4 3 5
8+
4 3 5 2 1
9+
10+
checks: 2745 backtracks: 534

myout2

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Number of cages: Cage number 0: Cage number 1: Cage number 2: Cage number 3: Cage number 4: Cage number 5: Cage number 6:
2+
---Solution---
3+
4+
1 2 3 4 5
5+
3 1 4 5 2
6+
2 5 1 3 4
7+
5 4 2 1 3
8+
4 3 5 2 1
9+
10+
checks: 470 backtracks: 79

myout3

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Number of cages: Cage number 0: Cage number 1: Cage number 2: Cage number 3: Cage number 4: Cage number 5: Cage number 6: Cage number 7:
2+
---Solution---
3+
4+
1 4 2 5 3
5+
2 5 4 3 1
6+
5 2 3 1 4
7+
4 3 1 2 5
8+
3 1 5 4 2
9+
10+
checks: 2025 backtracks: 390

sampleTests-2.py

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import unittest
2+
from solverFuncs import *
3+
4+
class TestCases(unittest.TestCase):
5+
6+
def test_check_rows0(self):
7+
puzzle = []
8+
puzzle.append([5, 1, 2, 3, 4])
9+
puzzle.append([1, 2, 3, 4, 5])
10+
puzzle.append([2, 3, 0, 5, 1])
11+
puzzle.append([3, 0, 0, 1, 2])
12+
puzzle.append([0, 0, 0, 0, 0])
13+
self.assertTrue(check_rows_valid(puzzle))
14+
15+
def test_check_columns0(self):
16+
puzzle = []
17+
puzzle.append([5, 1, 2, 3, 4])
18+
puzzle.append([1, 2, 3, 4, 5])
19+
puzzle.append([2, 3, 0, 5, 1])
20+
puzzle.append([3, 0, 0, 1, 2])
21+
puzzle.append([0, 0, 0, 0, 0])
22+
self.assertTrue(check_columns_valid(puzzle))
23+
24+
def test_check_cages0(self):
25+
puzzle = []
26+
puzzle.append([5, 1, 2, 3, 4])
27+
puzzle.append([1, 2, 3, 4, 5])
28+
puzzle.append([2, 3, 0, 5, 1])
29+
puzzle.append([3, 0, 0, 1, 2])
30+
puzzle.append([0, 0, 0, 0, 0])
31+
cages = []
32+
cages.append([6, 2, 0, 5])
33+
cages.append([7, 3, 2, 6, 7])
34+
cages.append([9, 2, 4, 9])
35+
self.assertTrue(check_cages_valid(puzzle, cages))
36+
37+
def test_check_valid0(self):
38+
puzzle = []
39+
puzzle.append([5, 1, 2, 3, 4])
40+
puzzle.append([1, 2, 3, 4, 5])
41+
puzzle.append([2, 3, 0, 5, 1])
42+
puzzle.append([3, 0, 0, 1, 2])
43+
puzzle.append([0, 0, 0, 0, 0])
44+
cages = []
45+
cages.append([6, 2, 0, 5])
46+
cages.append([7, 3, 2, 6, 7])
47+
cages.append([9, 2, 4, 9])
48+
self.assertTrue(check_valid(puzzle, cages))
49+
50+
def test_get_cages0(self):
51+
cages = [[5, 2, 0, 5],
52+
[3, 1, 1],
53+
[8, 3, 2, 3, 4]]
54+
self.assertEqual(cages, get_cages())
55+
56+
# Run the unit tests.
57+
if __name__ == '__main__':
58+
unittest.main()
59+

sampleTests.py

+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
import unittest
2+
from solverFuncs import *
3+
4+
class TestCases(unittest.TestCase):
5+
6+
def test_check_rows0(self):
7+
puzzle = []
8+
puzzle.append([5, 1, 2, 3, 4])
9+
puzzle.append([1, 2, 3, 4, 5])
10+
puzzle.append([2, 3, 0, 5, 1])
11+
puzzle.append([3, 0, 0, 1, 2])
12+
puzzle.append([0, 0, 0, 0, 0])
13+
self.assertTrue(check_rows_valid(puzzle))
14+
15+
def test_check_rows1(self):
16+
puzzle = []
17+
puzzle.append([1, 1, 1, 1, 1])
18+
puzzle.append([6, 6, 3, 4, 5])
19+
puzzle.append([2, 3, 0, 5, 1])
20+
puzzle.append([3, 0, 1, 1, 2])
21+
puzzle.append([0, 0, 0, 0, 0])
22+
self.assertFalse(check_rows_valid(puzzle))
23+
24+
def test_check_columns0(self):
25+
puzzle = []
26+
puzzle.append([5, 1, 2, 3, 4])
27+
puzzle.append([1, 2, 3, 4, 5])
28+
puzzle.append([2, 3, 0, 5, 1])
29+
puzzle.append([3, 0, 0, 1, 2])
30+
puzzle.append([0, 0, 0, 0, 0])
31+
self.assertTrue(check_columns_valid(puzzle))
32+
33+
def test_check_columns1(self):
34+
puzzle = []
35+
puzzle.append([5, 1, 2, 3, 4])
36+
puzzle.append([1, 2, 3, 4, 5])
37+
puzzle.append([2, 3, 0, 5, 1])
38+
puzzle.append([1, 6, 2, 1, 2])
39+
puzzle.append([0, 0, 0, 0, 0])
40+
self.assertFalse(check_columns_valid(puzzle))
41+
42+
def test_check_columns2(self):
43+
puzzle = []
44+
puzzle.append([5, 1, 2, 3, 4])
45+
puzzle.append([3, 2, 3, 4, 5])
46+
puzzle.append([2, 3, 0, 5, 1])
47+
puzzle.append([1, 6, 1, 1, 2])
48+
puzzle.append([0, 0, 0, 0, 0])
49+
self.assertFalse(check_columns_valid(puzzle))
50+
51+
def test_check_cages0(self):
52+
puzzle = []
53+
puzzle.append([5, 1, 2, 3, 4])
54+
puzzle.append([1, 2, 3, 4, 5])
55+
puzzle.append([2, 3, 0, 5, 1])
56+
puzzle.append([3, 0, 0, 1, 2])
57+
puzzle.append([0, 0, 0, 0, 0])
58+
cages = []
59+
cages.append([6, 2, 0, 5])
60+
cages.append([7, 3, 2, 4, 1])
61+
cages.append([5, 2, 1, 8])
62+
self.assertTrue(check_cages_valid(puzzle, cages))
63+
64+
def test_check_cages1(self):
65+
puzzle = []
66+
puzzle.append([5, 1, 2, 3, 4])
67+
puzzle.append([1, 2, 3, 4, 5])
68+
puzzle.append([2, 3, 0, 5, 1])
69+
puzzle.append([3, 0, 0, 1, 2])
70+
puzzle.append([0, 0, 0, 0, 0])
71+
cages = []
72+
cages.append([6, 2, 2, 5])
73+
cages.append([7, 3, 2, 6, 7])
74+
cages.append([9, 2, 4, 9])
75+
self.assertFalse(check_cages_valid(puzzle, cages))
76+
77+
def test_check_cages1(self):
78+
puzzle = []
79+
puzzle.append([5, 1, 2, 3, 4])
80+
puzzle.append([1, 2, 3, 4, 5])
81+
puzzle.append([2, 3, 0, 5, 1])
82+
puzzle.append([3, 0, 0, 1, 2])
83+
puzzle.append([0, 0, 0, 0, 0])
84+
cages = []
85+
cages.append([6, 2, 0, 5])
86+
cages.append([7, 3, 1, 2, 3])
87+
cages.append([9, 2, 1, 8])
88+
self.assertFalse(check_cages_valid(puzzle, cages))
89+
90+
def test_check_cages2(self):
91+
puzzle = []
92+
puzzle.append([5, 1, 2, 3, 4])
93+
puzzle.append([1, 2, 3, 4, 5])
94+
puzzle.append([2, 3, 0, 5, 1])
95+
puzzle.append([3, 0, 0, 1, 2])
96+
puzzle.append([0, 0, 0, 0, 0])
97+
cages = []
98+
cages.append([6, 2, 0, 5])
99+
cages.append([7, 3, 1, 2, 4])
100+
cages.append([9, 2, 0, 9])
101+
self.assertFalse(check_cages_valid(puzzle, cages))
102+
103+
def test_check_valid0(self):
104+
puzzle = []
105+
puzzle.append([5, 1, 2, 3, 4])
106+
puzzle.append([1, 2, 3, 4, 5])
107+
puzzle.append([2, 3, 0, 5, 1])
108+
puzzle.append([3, 0, 0, 1, 2])
109+
puzzle.append([0, 0, 0, 0, 0])
110+
cages = []
111+
cages.append([6, 2, 0, 5])
112+
cages.append([7, 3, 2, 4, 1])
113+
cages.append([5, 2, 1, 8])
114+
self.assertTrue(check_cages_valid(puzzle, cages))
115+
116+
117+
118+
# Run the unit tests.
119+
if __name__ == '__main__':
120+
unittest.main()
121+

solver.py

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#Name: Robert Mooy
2+
#Instructor: Workman
3+
#Project 3
4+
5+
from solverFuncs import *
6+
7+
puzzle = [[0,0,0,0,0], [0,0,0,0,0], [0,0,0,0,0], [0,0,0,0,0], [0,0,0,0,0]]
8+
cages = get_cages()
9+
checks = 0
10+
backtracks = 0
11+
row = 0
12+
col = 0
13+
print()
14+
while row < 5:
15+
puzzle[row][col] +=1
16+
check_puzzle = check_valid(puzzle, cages)
17+
checks +=1
18+
if check_puzzle == True:
19+
col += 1
20+
if col >= 5:
21+
col = 0
22+
row += 1
23+
else:
24+
while puzzle[row][col] == 5:
25+
backtracks += 1
26+
puzzle[row][col] = 0
27+
col -= 1
28+
if col < 0:
29+
col = 4
30+
row -= 1
31+
32+
print("---Solution---\n")
33+
print("%d %d %d %d %d" % (puzzle[0][0], puzzle[0][1], puzzle[0][2], puzzle[0][3], puzzle[0][4]))
34+
print("%d %d %d %d %d" % (puzzle[1][0], puzzle[1][1], puzzle[1][2], puzzle[1][3], puzzle[1][4]))
35+
print("%d %d %d %d %d" % (puzzle[2][0], puzzle[2][1], puzzle[2][2], puzzle[2][3], puzzle[2][4]))
36+
print("%d %d %d %d %d" % (puzzle[3][0], puzzle[3][1], puzzle[3][2], puzzle[3][3], puzzle[3][4]))
37+
print("%d %d %d %d %d" % (puzzle[4][0], puzzle[4][1], puzzle[4][2], puzzle[4][3], puzzle[4][4]))
38+
print("\nchecks: %d backtracks: %d" % (checks, backtracks))

solverFuncs.py

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
def get_cages():
2+
numcages = int(input("Number of cages: "))
3+
cages = []
4+
x = 0
5+
for x in range(numcages):
6+
info = input("Cage number %d: " %x).split()
7+
new = []
8+
for i in info:
9+
new.append(int(i))
10+
cages.append(new)
11+
return cages
12+
13+
def check_row(row):
14+
seen = []
15+
for num in row:
16+
if num in seen and num != 0 or num > 5:
17+
return False
18+
else:
19+
seen.append(num)
20+
return True
21+
22+
def check_rows_valid(puzzle):
23+
for row in puzzle:
24+
if check_row(row) == False:
25+
return False
26+
return True
27+
28+
def check_col(puzzle, index):
29+
seen = []
30+
for row in puzzle:
31+
if row[index] in seen and row[index] != 0 or row[index] > 5:
32+
return False
33+
else:
34+
seen.append(row[index])
35+
return True
36+
37+
def check_columns_valid(puzzle):
38+
index = 0
39+
for col in puzzle:
40+
if check_col(puzzle, index) == False:
41+
return False
42+
index += 1
43+
return True
44+
45+
def check_cage(puzzle, cage):
46+
cagesum = 0
47+
complete = True
48+
for index in cage[2:]:
49+
row = index // 5
50+
col = index % 5
51+
if puzzle[row][col] == 0:
52+
complete = False
53+
cagesum += puzzle[row][col]
54+
if cagesum != cage[0] and complete:
55+
return False
56+
if cagesum >= cage[0] and not complete:
57+
return False
58+
return True
59+
60+
def check_cages_valid(puzzle, cages):
61+
for cage in cages:
62+
if check_cage(puzzle, cage) == False:
63+
return False
64+
return True
65+
66+
def check_valid(puzzle, cages):
67+
if check_rows_valid(puzzle) == False:
68+
return False
69+
elif check_columns_valid(puzzle) == False:
70+
return False
71+
elif check_cages_valid(puzzle, cages) == False:
72+
return False
73+
else:
74+
return True

solverInst

11.2 KB
Binary file not shown.

0 commit comments

Comments
 (0)