-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInfinite monkey theorem
More file actions
45 lines (34 loc) · 889 Bytes
/
Infinite monkey theorem
File metadata and controls
45 lines (34 loc) · 889 Bytes
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
import random
# function to generate
# a random string
def generateOne(strlen):
# string with all the alphabets
# and a space
alphabet = "abcdefghijklmnopqrstuvwxyz "
res =""
for i in range(strlen):
res+= alphabet[random.randrange(27)]
return res
# function to determine the
# score of the generated string
def score(goal, testString):
numSame = 0
for i in range(len(goal)):
if goal[i] == testString[i]:
numSame+= 1
return numSame / len(goal)
# main function to call the previous
# two functions until the goal is achieved
def main():
goalString = "a github pull request"
newString = generateOne(35)
best = 0
newScore = score(goalString, newString)
while newScore<1:
if newScore>best:
print(newString)
best = newScore
newString = generateOne(35)
newScore = score(goalString, newString)
# Driver code
main()