Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

infinite monkey theorem

import string
import random
import sys

#blind method
def generate(length):
    characters = string.ascii_lowercase+" "
    return "".join([random.choice(characters) for i in range(length)])

def score(test_string,target_string):
    test_list = zip([i for i in test_string],[i for i in target_string])
    return (sum([1 for test_letter, target_letter in test_list if test_letter == target_letter])/len(target_string))*100

def generate_and_score(phrase):
    counter = 0
    best_score = 0
    best_attempt = None
    while best_score < 100: 
        this_attempt = generate(len(phrase))
        this_score = score(this_attempt,phrase)
        counter += 1
        if this_score > best_score:
            best_score = this_score
            best_attempt = this_attempt
        if counter % 1000 == 0:
            print("Tries: {}; Best Guess: {} with a score of {}".format(counter, best_attempt, best_score))
    print("Success in {} guesses!".format(counter))

#hill-climbing method
def improved_generate(best_attempt,phrase):
    characters = string.ascii_lowercase+" "
    return "".join([random.choice(characters) if a != p else a for a,p in zip([i for i in best_attempt],[i for i in phrase])])

def generate_and_score_improved(phrase):
    counter = 0
    best_score = 0
    best_attempt = None
    while best_score < 100:
        this_attempt = improved_generate(best_attempt,phrase) if best_attempt else generate(len(phrase))
        this_score = score(this_attempt,phrase)
        counter += 1
        if this_score > best_score:
            best_score = this_score
            best_attempt = this_attempt
        if counter % 1000 == 0:
            print("Tries: {}; Best Guess: {} with a score of {}".format(counter, best_attempt, best_score))
    print("Success in {} guesses!".format(counter))
Comment

PREVIOUS NEXT
Code Example
Python :: python flatten one liner 
Python :: python telegram 
Python :: random playing card generator python 
Python :: python replace string with int in list 
Python :: calculate area under the curve in python 
Python :: sys module in python 
Python :: saving model 
Python :: python calculated row in dataframe subtract 
Python :: list of dictionary values 
Python :: multiple assessment in python 
Python :: read mouse log python 
Python :: heading none in pandas import 
Python :: check if object exists python 
Python :: pandas join two dataframes 
Python :: remove df rows if two column values are not matching 
Python :: pairwise combinations groupby 
Python :: python using strip trim white sapce 
Python :: split column and rename them 
Python :: pd.loc 
Python :: Python Add/Change List Elements 
Python :: python get timestamp 2020-04-23T12:00:00Z 
Python :: scikit learn decistion tree 
Python :: expanding nebula foobar 
Python :: list dictionary to json file python with tab 
Python :: binary search iterative 
Python :: bell number python 
Python :: python get image RGB data from URL 
Python :: remove hh:mm:ss from pandas dataframe column 
Python :: django query string route 
Python :: python 3 docs 
ADD CONTENT
Topic
Content
Source link
Name
7+9 =