Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #This program will take two words of equal lengths and see how many
- #attempts it takes to change the first into the second by randomly
- #changing one letter at a time.
- import random
- #define variables
- word1 = ""
- word2 = ""
- array1 = ""
- array2 = ""
- rand_pos = 0
- rand_let = 0
- count = 0
- #ask for two words and validate that they are equal lengths and different words
- print("Welcome to the Random Mutation Simulator!")
- print("Give me two different words of equal lengths,")
- print("and I will tell you how many times it takes")
- print("to change the first into the second by randomly")
- print("changing one letter at a time.")
- word1 = input("What would you like your first word to be? ")
- word2 = input("What would you like your second word to be? ")
- while ((word1 == word2) or (len(word1) != len(word2))):
- if (word1 == word2):
- print("Those are the same word!")
- word2 = input("Please choose a different second word. Your first word was: " + word1 + " ")
- else: # if (len(word1) != len(word2))
- print("Those words are different lengths!")
- word2 = input("Please choose a second word of the same length. Your first word was: " + word1 + " ")
- word1 = word1.upper()
- word2 = word2.upper()
- print("Please wait while I try to turn " + word1 + " into " + word2 + ".....")
- #change the word strings into arrays for manipulation
- array1 = list(word1)
- array2 = list(word2)
- #simulation loop
- while (array1 != array2):
- #generate a random position in the array
- rand_pos = random.randrange(len(word1))
- #generate a random number from 1-26 and use that to get a random letter
- #assign the random letter to the random position in the array
- rand_let = (random.randrange(26)+1)
- if (rand_let == 1):
- array1[rand_pos] = "A"
- elif (rand_let == 2):
- array1[rand_pos] = "B"
- elif (rand_let == 3):
- array1[rand_pos] = "C"
- elif (rand_let == 4):
- array1[rand_pos] = "D"
- elif (rand_let == 5):
- array1[rand_pos] = "E"
- elif (rand_let == 6):
- array1[rand_pos] = "F"
- elif (rand_let == 7):
- array1[rand_pos] = "G"
- elif (rand_let == 8):
- array1[rand_pos] = "H"
- elif (rand_let == 9):
- array1[rand_pos] = "I"
- elif (rand_let == 10):
- array1[rand_pos] = "J"
- elif (rand_let == 11):
- array1[rand_pos] = "K"
- elif (rand_let == 12):
- array1[rand_pos] = "L"
- elif (rand_let == 13):
- array1[rand_pos] = "M"
- elif (rand_let == 14):
- array1[rand_pos] = "N"
- elif (rand_let == 15):
- array1[rand_pos] = "O"
- elif (rand_let == 16):
- array1[rand_pos] = "P"
- elif (rand_let == 17):
- array1[rand_pos] = "Q"
- elif (rand_let == 18):
- array1[rand_pos] = "R"
- elif (rand_let == 19):
- array1[rand_pos] = "S"
- elif (rand_let == 20):
- array1[rand_pos] = "T"
- elif (rand_let == 21):
- array1[rand_pos] = "U"
- elif (rand_let == 22):
- array1[rand_pos] = "V"
- elif (rand_let == 23):
- array1[rand_pos] = "W"
- elif (rand_let == 24):
- array1[rand_pos] = "X"
- elif (rand_let == 25):
- array1[rand_pos] = "Y"
- elif (rand_let == 26):
- array1[rand_pos] = "Z"
- #Count how many loops it takes to make the arrays match
- count = count + 1
- #once the arrays match, tell the user how many attempts it took
- print("It took me " + str(count) + " tries to turn " + word1 + " into " + word2 + ".")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement