-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonkeys.py
60 lines (45 loc) · 1.69 KB
/
monkeys.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
"""The way we’ll simulate this is to write a function that
generates a string that is 28 characters long by choosing
random letters from the 26 letters in the alphabet plus the space.
We’ll write another function that will score each generated string
by comparing the randomly generated string to the goal.
A third function will repeatedly call generate and score,
then if 100% of the letters are correct we are done.
If the letters are not correct then we will generate a whole new string.
To make it easier to follow your program’s progress this
third function should print out the best string generated
so far and its score every 1000 tries."""
import random
def monkey():
# returns the result of a monkey writing 28 random letters
alphabet = "abcdefghijklmnopqrstuvwxyz "
alphabetLength = len(alphabet)
result = ''
randomNum = 0
for i in range(28):
randomNum = random.randint(0, alphabetLength - 1)
result += alphabet[randomNum]
return result
def score(monkeySaid):
# returns the percentage score of how close the monkey got to the target
target = "methinks it is like a weasel"
accuracy = 0
targetList = list(target)
monkeyList = list(monkeySaid)
for i in range(len(targetList)):
if (monkeyList[i] == targetList[i]):
accuracy += 3.57
return accuracy
def guess(accuracy):
# continously calls score until the monkey gets it right
# prints the best result every 1000 guesses
i = 0
while (accuracy < 99):
i += 1
score(monkey())
print(accuracy)
print(i)
if (i == 1000):
print(accuracy)
score(monkey())
guess(score(monkey()))