/
main.py
executable file
·203 lines (175 loc) · 5.31 KB
/
main.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#!/usr/bin/env python3
"""
responsible for calling other modules and interacting with user
To solve the challenge problem, run:
./main.py --count 7 7 -k2 -q2 -b2 -n1
"""
import sys
from time import time as now
import argparse
from pieces import ChessPiece
from solution import (
find_solutions_s,
find_solutions_r,
find_solutions_q,
)
from chess_util import format_board
from cmd_util import input_yesno
from cmd_chess_util import input_problem
def count_or_show_by_generator(gen, count_enable, row_count, col_count):
"""
gen: a generator returned by find_solutions_*
count_enable: bool, only count solutions/configurations, don't show them
"""
if count_enable:
print('Calculating, please wait... (Control+C to cancel)')
tm0 = now()
try:
solution_count = sum(1 for _ in gen)
except KeyboardInterrupt:
print('\nGoodbye')
return
delta = now() - tm0
print('Number of Unique Configurations: %s' % solution_count)
print('Running Time: %.4f seconds' % delta)
else:
print('Found Configurations:\n')
for board in gen:
print(format_board(board, row_count, col_count))
try:
input('Press Enter to see the next, Control+C to exit')
except KeyboardInterrupt:
print('\nGoodbye')
break
def interactive_main():
"""
ask the board size and pieces count
calculate and show all possible unique configurations
or just count unique configurations depending on user input
"""
row_count, col_count, count_by_symbol = input_problem()
count_enable = input_yesno(
'Count configurations? [Yes/No] ',
default=False,
)
gen = find_solutions_s(
row_count,
col_count,
count_by_symbol,
)
count_or_show_by_generator(
gen,
count_enable,
row_count,
col_count,
)
def argparse_main():
"""
parses the command line arguments and options, and performs operations
"""
parser = argparse.ArgumentParser(add_help=True)
parser.add_argument(
action='store',
dest='row_count',
type=int,
help='number of rows in the board',
)
parser.add_argument(
action='store',
dest='col_count',
type=int,
help='number of columns in the board',
)
parser.add_argument(
'-c',
'--count',
dest='count_enable',
action='store_true',
default=False,
help='only count the number of unique configurations, '
'don\'t show them',
)
parser.add_argument(
'--recursive',
dest='recursive',
action='store_true',
default=False,
help='use recursive implementation instead of stack implementation'
)
for cls in ChessPiece.class_list:
plural_name = cls.name + 's'
parser.add_argument(
'-' + cls.symbol.lower(),
'--' + plural_name,
dest=cls.name,
type=int,
default=0,
help='number of %s' % plural_name
)
args = parser.parse_args()
count_by_symbol = {
cls.symbol: getattr(args, cls.name, 0)
for cls in ChessPiece.class_list
}
find_solutions = find_solutions_r if args.recursive else find_solutions_s
gen = find_solutions(
args.row_count,
args.col_count,
count_by_symbol,
)
count_or_show_by_generator(
gen,
args.count_enable,
args.row_count,
args.col_count,
)
# ______________________ Test Functions ______________________ #
def compare_find_solutions_result():
"""
run and compare the result of 3 implementations of find_solutions
make sure they all return the same set of configurations
with no duplicates
"""
row_count, col_count, count_by_symbol = input_problem()
solution_set_list = []
# solution_set_list is a list of sets, one set for each implementation
func_list = (
find_solutions_r,
find_solutions_q,
find_solutions_s,
)
for func in func_list: # pylint!
solution_set = set()
for board in func(row_count, col_count, count_by_symbol):
board_tuple = tuple(sorted(board.items()))
assert board_tuple not in solution_set
solution_set.add(board_tuple)
solution_set_list.append(solution_set)
print('Number of solutions: %s (%s)' % (len(solution_set), func))
assert solution_set_list[1:] == solution_set_list[:-1] # all items equal
def compare_find_solutions_time():
"""
run and compare the running time of 3 implementations of find_solutions
"""
row_count, col_count, count_by_symbol = input_problem()
time_list = []
func_list = (
find_solutions_s,
find_solutions_r,
find_solutions_q,
find_solutions_s,
find_solutions_r,
find_solutions_q,
)
for func in func_list: # pylint!
tm0 = now()
for _ in func(row_count, col_count, count_by_symbol):
pass
delta = now() - tm0
time_list.append(delta)
print('%.4f seconds (%s)' % (delta, func))
if __name__ == '__main__':
if len(sys.argv) > 1:
argparse_main()
else:
interactive_main()