Advertisement
acclivity

pyScrambleFilmTitles

May 26th, 2023
933
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.61 KB | Software | 0 0
  1. # Create Scrambled Text containing a Film Title
  2.  
  3. import random
  4.  
  5. letters = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz"
  6.  
  7. # A list of Film Titles from which we will try and make usable scrambled text
  8. titles = ["Toy Story", "Silent Running", "Star Wars", "The Good Life", "Ground Hog Day", "High Noon",
  9.           "Forrest Gump", "The Godfather", "Mary Poppins", "Great Expectations", "Romeo and Juliet",
  10.           "Pulp Fiction", "Citizen Kane", "Thunderball", "Goldfinger", "The Searchers", "Top Gun"]
  11.  
  12.  
  13. def func(s, v, z):          # s = film title, v = size of array to be created, z = initial letter for output
  14.     s = z + s               # add initial dummy letter to the film title
  15.     s += "."                # append a terminal full-stop
  16.     x = 0
  17.     arr = ['-'] * v         # create an initial "empty" array where scrambled output will be created
  18.     for j, c in enumerate(s):
  19.         try:
  20.             d = arr[x]
  21.         except:
  22.             return False       # error. went outside of array
  23.         if arr[x] != "-":
  24.             return False       # error:- this array slot already used
  25.         arr[x] = c
  26.         off = 1 + (ord(c) & 15)     # compute increment or decrement
  27.         if off % 2:
  28.             x += off                # add increment to index
  29.         else:
  30.             x -= off                # subtract increment from index
  31.  
  32.     # scrambling was successful. fill the empty cells with random letters
  33.     for j, c in enumerate(arr):
  34.         if c == "-":
  35.             arr[j] = random.choice(letters)
  36.     mystring = "".join(arr)
  37.     return mystring                 # return scrambled text
  38.  
  39.  
  40. for sent in titles:                         # For each film title ...
  41.     for vv in range(40, 120, 5):            # Try a variety of array lengths
  42.         aa = random.choice(letters)        # Choose a dummy starting letter
  43.  
  44.         res = func(sent, vv, aa)            # Try and scramble this film title
  45.         if res:                             # Check if successful
  46.             print(sent, "Succeeded with a scrambled length of: ", vv)        # show the good results
  47.             print(res, "\n")
  48.             break
  49.  
  50.  
  51. # Sample results:
  52. # Toy Story Succeeded with a scrambled length of:  55
  53. # aiFodtHVZPE.XJGmTErFoyIdNfZeNtTx SoHvqToGdyculkzqjiDSTT
  54. #
  55. # Silent Running Succeeded with a scrambled length of:  65
  56. # cEiXtASnc RiBuXZLrtHyfnPugjnPpZFhs.kciGgcVgGdCZlMJVoeonZpiHCeSUiO
  57. #
  58. # Mary Poppins Succeeded with a scrambled length of:  50
  59. # HAxcCVvSeMruLUnnkRNxICppi.ntvsHpoRMK PoEVBgrdayjoJ
  60. #
  61. # Top Gun Succeeded with a scrambled length of:  40
  62. # deERkTltgEoOszlxTkcYqhniGXlKurhxJAp G.Rh
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement