Advertisement
Guest User

Untitled

a guest
Sep 26th, 2017
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.88 KB | None | 0 0
  1. def rot( c, n ):
  2. """ rotates the single letter, c, by n spots in the alphabet
  3. """
  4. if 'a' <= c <= 'z':
  5. if ord(c) + n <= ord('z'):
  6. return chr( ord(c) + n )
  7. else:
  8. return chr( ord(c)+n-26)
  9. elif 'A' <= c <= 'Z':
  10. if ord(c) + n <= ord('Z'):
  11. return chr( ord(c) + n)
  12. else:
  13. return chr( ord(c) + n - 26)
  14. else:
  15. return c
  16.  
  17. def list_to_str( L ):
  18. """ L must be a list of characters; then,
  19. this returns a single string from them
  20. """
  21. if len(L) == 0: return ''
  22. return L[0] + list_to_str( L[1:] )
  23. #Here's how to test list_to_str: list_to_str( ['c','s','5','!'] ) should return 'cs5!'
  24.  
  25. def encipher( S, n ):
  26. """Takes in a string, S, and rotates that string through the alphabet by
  27. n letters. Upper case letters stay upper case, same for lower case.
  28. non-alphabetic characters do not change.
  29. """
  30. return list_to_str([rot(c,n) for c in S])
  31.  
  32. def letterScore( let ):
  33. """ output: value of character as scrabble tile
  34. input: single - character string
  35. """
  36. if let in 'anorsteil':
  37. return 1
  38. elif let in 'dg':
  39. return 2
  40. elif let in 'bcpm':
  41. return 3
  42. elif let in 'fhvwy':
  43. return 4
  44. elif let in 'k':
  45. return 5
  46. elif let in 'jx':
  47. return 8
  48. elif let in 'qz':
  49. return 10
  50. else:
  51. return 0
  52. def scrabbleScore( S ):
  53. """ output: scrabble score of string
  54. input: a string S
  55. """
  56. if len(S)==0:
  57. return 0
  58. else:
  59. return scrabbleScore( S[1:] ) + letterScore(S[0])
  60. def decipher( S ):
  61. """ returns the original english
  62. """
  63. E_all = [encipher( S, n) for n in range(26)]
  64. print (E_all)
  65. LoL = [ [scrabbleScore(w), w] for w in E_all]
  66. print[LoL]
  67. bestWord = min(LoL)
  68. return bestWord[1]
  69.  
  70. def remZero( L ):
  71. """removes 0's from the list L"""
  72. if len(L) == 0:
  73. return L
  74. elif L[0] != 0:
  75. return L[0:1] + remZero(L[1:])
  76. else:
  77. return remZero(L[1:])
  78.  
  79. def remOne( L ):
  80. """removes 1's from the list L
  81. """
  82. if len( L ) == 0:
  83. return L
  84. elif L[0] != 1:
  85. return L[0:1] + remOne(L[1:])
  86. else:
  87. return remOne(L[1:])
  88.  
  89. def blsort( L ):
  90. """sorts the list L in asceding order
  91. note: should be assumed that L contains only binary digits
  92. """
  93. if len(L) == 0:
  94. return ''
  95. else:
  96. return remOne(L) + remZero(L)
  97.  
  98. def removeone(e,L):
  99. if len(L) == 0:
  100. return L
  101. elif L[0] != e:
  102. return L[0:1] + removeone(e,L[1:])
  103. else:
  104. return L[1:]
  105.  
  106. def gensort( L ):
  107. """ returns the list L in ascending order
  108. """
  109. if len(L) == 0:
  110. return L
  111. else:
  112. return [(min(L))] + gensort(removeone(min(L),L))
  113.  
  114. def jscore( S, T ):
  115. """returns the Jotto score of S compared with T
  116. """
  117. if len(S) == 0:
  118. return 0
  119. else:
  120. if S[0] in T:
  121. return 1 + jscore(S[1:],removeone(S[0],T))
  122. else:
  123. return jscore(S[1:],T)
  124.  
  125. def count( e, L ):
  126. """ returns the # of e's in L """
  127. LC = [ e==x for x in L ]
  128. return sum( LC )
  129.  
  130. def exact_change( target_amount, L ):
  131. """ returns true or false according to if the contents of L can be summed up
  132. to reach the target_amount
  133. """
  134. if target_amount == 0:
  135. return True
  136. elif target_amount<0:
  137. return False
  138. elif len(L) == 0:
  139. return False
  140. else:
  141. result1 = exact_change(target_amount,L[1:])
  142. result2 = exact_change(target_amount-L[0], L[1:])
  143. if result1 == True or result2 == True:
  144. return True
  145. else:
  146. return False
  147.  
  148. def LCS( S, T ):
  149. """returns the longest shared sequence b/t S and T
  150. """
  151. if len(S) == 0 or len(T) == 0:
  152. return ''
  153. elif S[0] == T[0]:
  154. return str(S[0]) + str(LCS(S[1:],T[1:]))
  155. else:
  156. result1 = LCS(S[1:],T)
  157. result2 = LCS(S,T[1:])
  158. if len(result1) > len(result2):
  159. return result1
  160. else:
  161. return result2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement