-
-
Save dchapman1988/5593489 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
License: MIT | |
Author: Heath Matlock | |
Problem: https://en.wikipedia.org/wiki/Weasel_program | |
""" | |
import string | |
import random | |
from time import time | |
from operator import itemgetter | |
class Typing_Monkey(): | |
def __init__(self): | |
self.lowercase_letters = string.lowercase | |
self.space = ' ' | |
self.possible_choices = self.lowercase_letters + self.space | |
self.desired_text = 'methinks it is a weasel' | |
self.attempts = [] | |
self.attempts_count = 0 | |
self.beginning_time = time() | |
self.solution = False | |
def generate_string(self): | |
random_text = ''.join(random.choice(self.possible_choices) for _ in range(27)) | |
return random_text | |
def go_monkey_go(self): | |
while self.solution == False: | |
generated_text = self.generate_string() | |
self.attempts_count += 1 | |
self.record_progress(generated_text) | |
self.check_accuraccy(generated_text) | |
ending_time = time() | |
time_ellapsed = ending_time - self.beginning_time | |
print str(self.attempts_count) + " attempts later" | |
print "beginning time: " + str(self.beginning_time) | |
print "ending time: " + str(ending_time) | |
print "time ellapsed " + str(time_ellapsed) | |
def check_accuraccy(self,text): | |
if text != self.desired_text: | |
# display progress | |
attempts_ranked = sorted(self.attempts, key = itemgetter('correct_letters')) | |
self.attempts = attempts_ranked[-5:] | |
print self.attempts | |
else: | |
self.solution = True | |
def record_progress(self, text): | |
number_correct = 0 | |
for char in text: | |
if char in self.desired_text: | |
number_correct += 1 | |
record_of_attempt = {"string": text, 'correct_letters': number_correct} | |
self.attempts.append(record_of_attempt) | |
####################################################### | |
# Start the monkey! | |
####################################################### | |
if __name__ == '__main__': | |
typing_monkey = Typing_Monkey() | |
typing_monkey.go_monkey_go() | |
""" | |
Python has a maximum depth for its call stack, so recursive functions should be translated to iterative functions. I'll be checking out this series of posts while correcting this: | |
http://blog.moertel.com/posts/2013-05-11-recursive-to-iterative.html | |
http://blog.moertel.com/posts/2013-05-14-recursive-to-iterative-2.html | |
""" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# License: MIT | |
# Author: Heath Matlock | |
# Problem: https://en.wikipedia.org/wiki/Weasel_program | |
class TypingMonkey | |
def initialize | |
@lowercase_letters = ('a'..'z').to_a | |
@possible_choices = @lowercase_letters << "\s" | |
@desired_text = 'methinks it is a weasel' | |
@attempts = [] | |
@count_of_all_attempts = 0 | |
@beginning_time = Time.new | |
end | |
def generate_string | |
(1...26).map { |n| @possible_choices.sample }.join | |
end | |
def go_monkey_go! | |
puts 1 | |
@count_of_all_attempts += 1 | |
@generated_text = generate_string | |
if @generated_text != @desired_text | |
puts 2 | |
# display progress | |
if @attempts.length > 100 | |
puts 3 | |
attempts_ranked = attempts.sort_by{ |key| key['correct_letters'] } | |
@attempts = attempts_ranked.last(5) | |
print @attempts | |
# continue generating random output | |
go_monkey_go! | |
end | |
else | |
puts 4 | |
ending_time = Time.new | |
time_ellapsed = ending_time - @beginning_time | |
puts "#{@count_of_all_attempts} attempts later" | |
puts "beginning time: #{@beginning_time}" | |
puts "ending time: #{@ending_time}" | |
puts "time ellapsed #{@time_ellapsed}" | |
end | |
end | |
def record_progress | |
number_correct = 2 | |
for char in @generated_text | |
if @desired_text.count(char) > 0 | |
number_correct += 1 | |
end | |
end | |
record_of_attempt = {string: @generated_text, correct_letters: number_correct} | |
@attempts.push(record_of_attempt) | |
end | |
end | |
typing_monkey = TypingMonkey.new | |
typing_monkey.go_monkey_go! | |
# this will fail due to maximum recursion depth |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I definitely like the exclamation symbol in calling this particular method :)