-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
240 lines (212 loc) · 9.76 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
from YourCombinations import YourCombinations
# cli to ask user to enter and prompt
def cli():
# infinity loop to ask user input
while True:
print("> ", end = "")
# handle up and down key
# to get previous and next command
# from history
# up: \x1b[A
# down: \x1b[B
try:
command = input().strip()
except KeyboardInterrupt:
# handle ctrl + c
print()
continue
except EOFError:
# handle ctrl + d
print()
break
commands_str = command
# split command to get command and arguments
commands = commands_str.split()
# check if command is empty
if len(commands) == 0:
continue
# get command
command = commands[0]
# check command
if command == "exit":
break
elif command == "help":
print("help - show this message")
print("version - show version")
print("exit - exit program")
print("set ... ... ...")
print("")
print("Power Set:")
print("\tlist power")
print("\tsave power")
print("\tcount power")
print("")
print("Combinations:")
print("\tlist combinations with repeat <n>")
print("\tsave combinations with repeat <n>")
print("\tcount combinations with repeat <n>")
print("")
print("\tlist combinations without repeat <n>")
print("\tsave combinations without repeat <n>")
print("\tcount combinations without repeat <n>")
print("")
print("Permutations:")
print("\tlist permutations with repeat <n>")
print("\tsave permutations with repeat <n>")
print("\tcount permutations with repeat <n>")
print("")
print("\tlist permutations without repeat <n>")
print("\tsave permutations without repeat <n>")
print("\tcount permutations without repeat <n>")
elif command == "version":
print("1.0.0")
elif command == "set":
arguments = commands[1:]
your_combinations = YourCombinations(arguments)
# Power
elif commands_str.startswith("list power"):
for i in your_combinations.powerSet():
print(i)
elif commands_str.startswith("save power"):
file = open("power_set.txt", "w")
# file.write(str(your_combinations.powerSet()))
for i in your_combinations.powerSet():
file.write(str(i) + "\n")
file.close()
print("Saved to power_set.txt")
elif commands_str.startswith("count power"):
print("Count Power set:")
l = 0
for i in your_combinations.powerSet():
l = l + 1
print(l)
# Combinations with repeat
elif commands_str.startswith("list combinations with repeat"):
argument_length = commands_str[len("list combinations with repeat"):].strip()
if not argument_length.isdigit() or int(argument_length) <= 0:
print("Invalid argument")
continue
for i in your_combinations.combinations(int(argument_length), True):
print(i)
elif commands_str.startswith("save combinations with repeat"):
argument_length = commands_str[len("save combinations with repeat"):].strip()
if not argument_length.isdigit() or int(argument_length) <= 0:
print("Invalid argument")
continue
file = open("combinations_with_repeat.txt", "w")
# file.write(str(your_combinations.combinations(int(argument_length), True)))
for i in your_combinations.combinations(int(argument_length), True):
file.write(str(i) + "\n")
file.close()
print("Saved to combinations_with_repeat.txt")
elif commands_str.startswith("count combinations with repeat"):
argument_length = commands_str[len("count combinations with repeat"):].strip()
if not argument_length.isdigit() or int(argument_length) <= 0:
print("Invalid argument")
continue
# print(len(your_combinations.combinations(int(argument_length), True)))
l = 0
for i in your_combinations.combinations(int(argument_length), True):
l = l + 1
print(l)
# Combinations without repeat
elif commands_str.startswith("list combinations without repeat"):
argument_length = commands_str[len("list combinations without repeat"):].strip()
if not argument_length.isdigit() or int(argument_length) <= 0:
print("Invalid argument")
continue
for i in your_combinations.combinations(int(argument_length)):
print(i)
elif commands_str.startswith("save combinations without repeat"):
argument_length = commands_str[len("save combinations without repeat"):].strip()
if not argument_length.isdigit() or int(argument_length) <= 0:
print("Invalid argument")
continue
file = open("combinations_without_repeat.txt", "w")
# file.write(str(your_combinations.combinations(int(argument_length))))
for i in your_combinations.combinations(int(argument_length), False):
file.write(str(i) + "\n")
file.close()
print("Saved to combinations_without_repeat.txt")
elif commands_str.startswith("count combinations without repeat"):
argument_length = commands_str[len("count combinations without repeat"):].strip()
if not argument_length.isdigit() or int(argument_length) <= 0:
print("Invalid argument")
continue
# print(len(your_combinations.combinations(int(argument_length))))
l = 0
for i in your_combinations.combinations(int(argument_length), False):
l = l + 1
print(l)
# Permutations with repeat
elif commands_str.startswith("list permutations with repeat"):
argument_length = commands_str[len("list permutations with repeat"):].strip()
if not argument_length.isdigit() or int(argument_length) <= 0:
print("Invalid argument")
continue
for i in your_combinations.permutations(int(argument_length), True):
print(i)
elif commands_str.startswith("save permutations with repeat"):
argument_length = commands_str[len("save permutations with repeat"):].strip()
if not argument_length.isdigit() or int(argument_length) <= 0:
print("Invalid argument")
continue
file = open("permutations_with_repeat.txt", "w")
# file.write(str(your_combinations.permutations(int(argument_length), True)))
for i in your_combinations.permutations(int(argument_length), True):
file.write(str(i) + "\n")
file.close()
print("Saved to permutations_with_repeat.txt")
elif commands_str.startswith("count permutations with repeat"):
argument_length = commands_str[len("count permutations with repeat"):].strip()
if not argument_length.isdigit() or int(argument_length) <= 0:
print("Invalid argument")
continue
# print(len(your_combinations.permutations(int(argument_length), True)))
l = 0
for i in your_combinations.permutations(int(argument_length), True):
l = l + 1
print(l)
# Permutations without repeat
elif commands_str.startswith("list permutations without repeat"):
argument_length = commands_str[len("list permutations without repeat"):].strip()
if not argument_length.isdigit() or int(argument_length) <= 0:
print("Invalid argument")
continue
for i in your_combinations.permutations(int(argument_length), False):
print(i)
elif commands_str.startswith("save permutations without repeat"):
argument_length = commands_str[len("save permutations without repeat"):].strip()
if not argument_length.isdigit() or int(argument_length) <= 0:
print("Invalid argument")
continue
file = open("permutations_without_repeat.txt", "w")
# file.write(str(your_combinations.permutations(int(arguments), False)))
for i in your_combinations.permutations(int(argument_length), False):
file.write(str(i) + "\n")
file.close()
print("Saved to permutations_without_repeat.txt")
elif commands_str.startswith("count permutations without repeat"):
argument_length = commands_str[len("count permutations without repeat"):].strip()
if not argument_length.isdigit() or int(argument_length) <= 0:
print("Invalid argument")
continue
# print(len(your_combinations.permutations(int(argument_length), False)))
l = 0
for i in your_combinations.permutations(int(argument_length), False):
l = l + 1
print(l)
else:
print("Unknown command. Type 'help' to show help message.")
# main function
def main():
# show help message
print("FastCombPerm - version 1.0.0")
print("Fast Combinations and Permutations Calculator")
print("")
print("Type 'help' to show help message.")
# call cli
cli()
# start point
if __name__ == "__main__":
main()