-
Notifications
You must be signed in to change notification settings - Fork 0
/
infintepython.py
50 lines (42 loc) · 1.38 KB
/
infintepython.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
'''
'''
import string
import random
shakespeare = "methinks it is like a weasel"
def make_string(best_guess):
'''create a string from aplphabet if solution not found for index'''
choices = string.ascii_lowercase + " "
current_best = [i for i in best_guess]
for i in range(len(current_best)):
if current_best[i] == "*":
current_best[i] = random.choice(choices)
# return ''.join(random.choice(choices) for x in range(28))
return ''.join(current_best)
def rate_string(some_str):
'''rate current string and reset character if no match'''
# current = [i for i in attempt]
score = 0
rate_this = [i for i in some_str]
for i in range(len(rate_this)):
if rate_this[i] == shakespeare[i]:
score += 1
else:
rate_this[i] = "*"
rated = ''.join(rate_this)
return rated, score
def caller(goal):
'''call make_string and rate_string until solution found'''
str_guess = ""
count = 0
best = 0
best_guess = "*" * len(goal)
while goal != best_guess:
str_guess = make_string(best_guess)
score = rate_string(str_guess)[1]
count += 1
if score > best:
best_guess = rate_string(str_guess)[0]
best = score
print(str_guess, score)
print "it took {} iterations to find the solution".format(count)
caller(shakespeare)