Last active
August 29, 2015 14:04
-
-
Save chestergrant/928b8afa09f804af12d9 to your computer and use it in GitHub Desktop.
Search through the character space to find your name
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
import math | |
import random | |
#Randomly modifies part of a possible solution | |
def modify_solution(solution,characters): | |
solution_idx = random.randint(0,len(solution)-1) | |
character_idx = random.randint(0,len(characters)-1) | |
output = copy_solution(solution) | |
output[solution_idx] = characters[character_idx] | |
return output | |
#Measures how good of a solution our possible solution is | |
def goodness(ans,solution): | |
num_correct_characters = 0 | |
for i in range(len(ans)): | |
if ans[i] == solution[i]: | |
num_correct_characters += 1 | |
return num_correct_characters | |
#copy one list to another | |
def copy_solution(_from): | |
_to = [] | |
for i in range(len(_from)): | |
_to.append(_from[i]) | |
return _to | |
#Generate the initial possible solution | |
def generate_random_solution(num_characters,characters): | |
output = [] | |
for i in range(num_characters): | |
random_index = random.randint(0,len(characters)-1) | |
output.append(characters[random_index]) | |
return output | |
name = "chester grant" | |
characters = "abcdefghijklmnopqrstuvwxyz " | |
num_characters = 13 | |
current_solution = generate_random_solution(num_characters,characters); | |
previous_solution = copy_solution(current_solution) | |
while not "".join(current_solution) == name : | |
if goodness(list(name),current_solution) >= goodness(list(name),previous_solution): | |
print "".join(current_solution) | |
previous_solution = copy_solution(current_solution) | |
current_solution = modify_solution(current_solution,characters) | |
else: | |
current_solution = modify_solution(previous_solution,characters) | |
print "".join(current_solution) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment