Guest User

Untitled

a guest
Jun 24th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.82 KB | None | 0 0
  1. # do this once, outside the loop
  2. alphabet=set(string.ascii_lowercase)
  3. # inside the loop, just 1 line:
  4. missingletter=(alphabet-set(yourlist)).pop()
  5.  
  6. frompos, topos = 0, len(str)
  7. for i in range(1,100): #never say forever with bisection...
  8. trypos = (frompos+topos+1)/2
  9. print "try:",frompos,trypos,topos
  10. if alphabet[trypos] != str[trypos]:
  11. topos = trypos
  12. else:
  13. frompos = trypos
  14. if topos-frompos==1:
  15. if alphabet[topos] != str[topos]:
  16. print alphabet[frompos]
  17. else:
  18. print alphabet[topos]
  19. break
  20.  
  21. >>> import string
  22. >>> sourcelist = 'abcdefghijklmnopqrstuvwx'
  23. >>> [letter for letter in string.ascii_lowercase if letter not in sourcelist]
  24. ['y', 'z']
  25. >>>
  26.  
  27. >>> string.ascii_lowercase
  28. 'abcdefghijklmnopqrstuvwxyz'
  29.  
  30. >>> string.letters
  31. 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
  32.  
  33. >>> string.hexdigits
  34. '0123456789abcdefABCDEF'
  35.  
  36. >>> string.octdigits
  37. '01234567'
  38.  
  39. >>> string.digits
  40. '0123456789'
  41.  
  42. >>> string.printable
  43. '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~ tnrx0bx0c'
  44. >>>
  45.  
  46. print chr(2847 - sum(map(ord, theString)))
  47.  
  48. def missing_letter_basic(s):
  49. for letter in string.ascii_lowercase:
  50. if letter not in s: return letter
  51. raise Exception("No missing letter")
  52.  
  53. find_missing_letter = dict((string.ascii_lowercase[:i]+string.ascii_lowercase[i+1:],
  54. string.ascii_lowercase[i]) for i in range(26)).get
  55.  
  56. >>> find_missing_letter('abcdefghijklmnoprstuvwxyz')
  57. 'q'
  58.  
  59. "b" "m" "y"
  60. bisect : 2.762 2.872 2.922 (Phil H)
  61. find_gap : 3.388 4.533 5.642 (unwind)
  62. listcomp : 2.832 2.858 2.822 (monkut)
  63. listcomp_set : 4.770 4.746 4.700 As above, with sourcelist=set(sourcelist) first
  64. set_difference : 2.924 2.945 2.880 (Phil H)
  65. sum : 3.815 3.806 3.868
  66. sum_imap : 3.284 3.280 3.260
  67. basic : 0.544 1.379 2.359
  68. dict_lookup : 0.135 0.133 0.134
  69.  
  70. for i in xrange(1, len(a)):
  71. if a[i] != a[i - 1] + 1:
  72. print a[i - 1] + 1, "is missing"
  73.  
  74. letterSet = set()
  75. for word in wordList:
  76. letterSet.update(set(word.lower()))
  77.  
  78. import string
  79. alphabet = set(string.lowercase)
  80. missingLetters = alphabet.difference(letterSet)
  81.  
  82. class MissingFinder(object):
  83. "A simplified missing items locator"
  84. def __init__(self, alphabet):
  85. "Store a set from our alphabet"
  86. self.alphabet= set(alphabet)
  87. def missing(self, sequence):
  88. "Return set of missing letters; sequence not necessarily set"
  89. return self.alphabet.difference(sequence)
  90.  
  91. >>> import string
  92. >>> finder= MissingFinder(string.ascii_lowercase)
  93.  
  94. >>> finder.missing(string.ascii_lowercase[:5] + string.ascii_lowercase[6:])
  95. >>> set(['f'])
  96. >>> # rinse, repeat calling finder.missing
Add Comment
Please, Sign In to add comment