Advertisement
Guest User

Structure suggestion tablet puzzles with only one solution

a guest
Apr 27th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.10 KB | None | 0 0
  1. import random
  2.  
  3. def removeInvalidLetters(nextLetter, code, validLetters):
  4.     '''
  5.    Inputs:
  6.    - The next letter in the code that already appeared in the code
  7.    - The code
  8.    - The current list of valid characters
  9.    Output:
  10.    - An updated list with valid characters. The nextLetter and all characters after it are removed
  11.    '''
  12.    
  13.     indexLetter = code.find(nextLetter)
  14.     for index in range(indexLetter, len(code)):
  15.         character = code[index]
  16.         if character in validLetters:
  17.             validLetters.remove(code[index])
  18.        
  19.     return validLetters
  20.  
  21. def generateCode(length):
  22.     '''
  23.    Input: the length of the code you want
  24.    Output: a code of the specified length that has only one solution
  25.    '''
  26.    
  27.     code = ""
  28.     validLetters = list(map(chr, range(65, 91)))
  29.    
  30.     for i in range(length):
  31.         nextLetter = validLetters[random.randint(0, len(validLetters)-1)]
  32.         if nextLetter in code:
  33.             validLetters = removeInvalidLetters(nextLetter, code, validLetters)
  34.         code += nextLetter
  35.    
  36.     return code
  37.  
  38. print(generateCode(27))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement