Advertisement
Musical_Muze

Random Mutation of Data

Aug 27th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.47 KB | None | 0 0
  1. #This program will take two words of equal lengths and see how many
  2. #attempts it takes to change the first into the second by randomly
  3. #changing one letter at a time.
  4.  
  5. import random
  6.  
  7. #define variables
  8. word1 = ""
  9. word2 = ""
  10. array1 = ""
  11. array2 = ""
  12. rand_pos = 0
  13. rand_let = 0
  14. count = 0
  15.  
  16.  
  17. #ask for two words and validate that they are equal lengths and different words
  18. print("Welcome to the Random Mutation Simulator!")
  19. print("Give me two different words of equal lengths,")
  20. print("and I will tell you how many times it takes")
  21. print("to change the first into the second by randomly")
  22. print("changing one letter at a time.")
  23. word1 = input("What would you like your first word to be?  ")
  24. word2 = input("What would you like your second word to be?  ")
  25. while ((word1 == word2) or (len(word1) != len(word2))):
  26.     if (word1 == word2):
  27.         print("Those are the same word!")
  28.         word2 = input("Please choose a different second word. Your first word was: " + word1 + "  ")
  29.     else: # if (len(word1) != len(word2))
  30.         print("Those words are different lengths!")
  31.         word2 = input("Please choose a second word of the same length. Your first word was: " + word1 + "  ")
  32.  
  33. word1 = word1.upper()
  34. word2 = word2.upper()
  35.  
  36. print("Please wait while I try to turn " + word1 + " into " + word2 + ".....")
  37.  
  38. #change the word strings into arrays for manipulation
  39. array1 = list(word1)
  40. array2 = list(word2)
  41.  
  42.  
  43. #simulation loop
  44. while (array1 != array2):
  45.  
  46.     #generate a random position in the array
  47.     rand_pos = random.randrange(len(word1))
  48.  
  49.     #generate a random number from 1-26 and use that to get a random letter
  50.     #assign the random letter to the random position in the array
  51.     rand_let = (random.randrange(26)+1)
  52.     if (rand_let == 1):
  53.         array1[rand_pos] = "A"
  54.     elif (rand_let == 2):
  55.         array1[rand_pos] = "B"
  56.     elif (rand_let == 3):
  57.         array1[rand_pos] = "C"
  58.     elif (rand_let == 4):
  59.         array1[rand_pos] = "D"
  60.     elif (rand_let == 5):
  61.         array1[rand_pos] = "E"
  62.     elif (rand_let == 6):
  63.         array1[rand_pos] = "F"
  64.     elif (rand_let == 7):
  65.         array1[rand_pos] = "G"
  66.     elif (rand_let == 8):
  67.         array1[rand_pos] = "H"
  68.     elif (rand_let == 9):
  69.         array1[rand_pos] = "I"
  70.     elif (rand_let == 10):
  71.         array1[rand_pos] = "J"
  72.     elif (rand_let == 11):
  73.         array1[rand_pos] = "K"
  74.     elif (rand_let == 12):
  75.         array1[rand_pos] = "L"
  76.     elif (rand_let == 13):
  77.         array1[rand_pos] = "M"
  78.     elif (rand_let == 14):
  79.         array1[rand_pos] = "N"
  80.     elif (rand_let == 15):
  81.         array1[rand_pos] = "O"
  82.     elif (rand_let == 16):
  83.         array1[rand_pos] = "P"
  84.     elif (rand_let == 17):
  85.         array1[rand_pos] = "Q"
  86.     elif (rand_let == 18):
  87.         array1[rand_pos] = "R"
  88.     elif (rand_let == 19):
  89.         array1[rand_pos] = "S"
  90.     elif (rand_let == 20):
  91.         array1[rand_pos] = "T"
  92.     elif (rand_let == 21):
  93.         array1[rand_pos] = "U"
  94.     elif (rand_let == 22):
  95.         array1[rand_pos] = "V"
  96.     elif (rand_let == 23):
  97.         array1[rand_pos] = "W"
  98.     elif (rand_let == 24):
  99.         array1[rand_pos] = "X"
  100.     elif (rand_let == 25):
  101.         array1[rand_pos] = "Y"
  102.     elif (rand_let == 26):
  103.         array1[rand_pos] = "Z"
  104.    
  105.     #Count how many loops it takes to make the arrays match
  106.     count = count + 1
  107.  
  108. #once the arrays match, tell the user how many attempts it took
  109. print("It took me " + str(count) + " tries to turn " + word1 + " into " + word2 + ".")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement