Monkey Theorem - pick random letters until input string is generated
<p>I wanted to make a code resembles the infinite monkey theorem
<strong>the theorem states that:</strong> </p>

<blockquote>
  <p>a monkey hitting keys at random on a typewriter keyboard for infinite
  amount of time will almost surely type a given text such as the
  complete works of Shakespeare.</p>
</blockquote>

<p>well , suppose we replace a monkey with a python to generate one sentence of Shakespeare ? and calculate the trials</p>

<p>for example: "methinks it is like a weasel"</p>

<p>Here is what I wrote:</p>

<pre><code>def Monkey_Theorem(sentence):
    sentence.lower()
    string_letters = 'abcdefghijklmnopqrstuvwxyz'
    import random
    a = []
    l = ''
    x = 0
    trials = 0
    while x &lt; (len(sentence) - 1):
        trials += 1
        z = random.choice(string_letters)
        if z == sentence[x]:
            a.append(z)
            x += 1
        elif z == " ":
            trials -= 1
            a.append(" ")
        if x &gt; (len(sentence) - 1):
            l = ''.join(l)
            break
    return l and trials
</code></pre>

<p>I wrote this into python 2.7 IDLE , pressed enter then wrote:</p>

<pre><code>Monkey_Theorem("methinks it is like a weasel")
</code></pre>

<p>after that I pressed enter.. it moved to the next line and didn't come up with the (>>>) </p>

<p>and after sometime no result and when trying to close the IDLE with despair on my face , it says program still running</p>

<p><strong>HERE'S THE QUESTION :</strong>  </p>

<p>(A) is the while loop an infinite one ??<br>
(B) or my IDLE is slow ??<br>
(C) other cause<br>
and how to fix the reason</p>

<p><strong>One more request</strong> 
how to detect the case if I encounter this situation in the future whether it's (A) , (B) , (C)?</p>

<p>You can print something within the loop to see if it is infinite... For example, if <code>z</code> is never equal to the <code>sentence[x]</code>, then you won't increment it, and the loop never stops. </p>

<p>That will happen when you need to guess a space character because you have not put one into your string. Therefore, even <code>elif z == " ":</code> will never be entered. </p>

<p>Here is a working version of your code.</p>

<p>Some things to note: Keeping track of <code>a</code> or <code>l</code> is pointless. At the end of the function, you will be returning the exact same input as <code>sentence</code>. Also, there is a simple import to get all the english letters. </p>

<pre><code>from string import ascii_lowercase as alpha
import random

# add space character to possible choices
alpha += ' '

def Monkey_Theorem(sentence):
    sentence = sentence.lower()
    x = 0
    trials = 0
    while x &lt; len(sentence):
        trials += 1
        z = random.choice(alpha)
        if z == sentence[x]:
            if z == " ": # this isn't really necessary
                trials -= 1
            print "guessed %s at trial %d" % (z, trials)
            x += 1
    return trials

trials = Monkey_Theorem("hello world")
print "number of trials = ", trials
</code></pre>

<p>Output </p>

<pre><code>guessed h at trial 49
guessed e at trial 52
guessed l at trial 58
guessed l at trial 107
guessed o at trial 145
guessed   at trial 157
guessed w at trial 168
guessed o at trial 197
guessed r at trial 232
guessed l at trial 248
guessed d at trial 269
number of trials =  269
</code></pre>

python