This was the assignment:
Here’s a self check that really covers everything so far. You may have heard of the infinite monkey theorem? The theorem states that a monkey hitting keys at random on a typewriter keyboard for an infinite amount of time will almost surely type a given text, such as the complete works of William Shakespeare. Well, suppose we replace a monkey with a Python function. How long do you think it would take for a Python function to generate just one sentence of Shakespeare? The sentence we’ll shoot for is: “methinks it is like a weasel”
You’re not going to want to run this one in the browser, so fire up your favorite Python IDE. The way we’ll simulate this is to write a function that generates a string that is 27 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
Soo i have created a python program which generates the string provided, i am in learning phase, can someone please suggest any improvement or any tips or corrections.
Note: I know i have not did the exact method said in that question, but i have created the expected result.
makeRandSent: This will give me random character.
compare: This will give me final result by comparing each user character with randomly generated character and it will only change those chracter which are not match.
def makeRandSent(userSent): counter = 0 randSent = [""] * (len(userSent) - 1) randSent.clear() while counter < len(userSent): counter = counter + 1 randSent.append(myalphlist[random.randrange(0,len(myalphlist) - 1)]) return randSent def compare(userSent): myBoolList = [False] * len(userSent) myRandSent = makeRandSent(userSent) print("random sent: ",myRandSent) myFinalList = [""] * len(userSent) while myBoolList.count(False) > 0: counter = 0 myRandSent = makeRandSent(userSent) for char in myRandSent: if char == userSent[counter]: myBoolList[counter] = True myFinalList[counter] = char counter = counter + 1 print(myBoolList," Final: ",myFinalList) return myBoolList
Output after several automatic attempts by program:
[True, True, True, True, True, True] Final: ['p', 'r', 'a', 't', 'i', 'k']