-
Notifications
You must be signed in to change notification settings - Fork 220
/
strategy.py
59 lines (46 loc) · 1.57 KB
/
strategy.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
import time
def pairs(seq):
n = len(seq)
for i in range(n):
yield seq[i], seq[(i + 1) % n]
SLOW = 3 # in seconds
LIMIT = 5 # in characters
WARNING = 'too bad, you picked the slow algorithm :('
def allUniqueSort(s):
if len(s) > LIMIT:
print(WARNING)
time.sleep(SLOW)
srtStr = sorted(s)
for (c1, c2) in pairs(srtStr):
if c1 == c2:
return False
return True
def allUniqueSet(s):
if len(s) < LIMIT:
print(WARNING)
time.sleep(SLOW)
return True if len(set(s)) == len(s) else False
def allUnique(word, strategy):
return strategy(word)
def main():
WORD_IN_DESC = 'Insert word (type quit to exit)> '
STRAT_IN_DESC = 'Choose strategy: [1] Use a set, [2] Sort and pair> '
while True:
word = None
while not word:
word = input(WORD_IN_DESC)
if word == 'quit':
print('bye')
return
strategy_picked = None
strategies = { '1': allUniqueSet, '2': allUniqueSort }
while strategy_picked not in strategies.keys():
strategy_picked = input(STRAT_IN_DESC)
try:
strategy = strategies[strategy_picked]
result = allUnique(word, strategy)
print(f'allUnique({word}): {result}')
except KeyError as err:
print(f'Incorrect option: {strategy_picked}')
if __name__ == "__main__":
main()