Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # do this once, outside the loop
- alphabet=set(string.ascii_lowercase)
- # inside the loop, just 1 line:
- missingletter=(alphabet-set(yourlist)).pop()
- frompos, topos = 0, len(str)
- for i in range(1,100): #never say forever with bisection...
- trypos = (frompos+topos+1)/2
- print "try:",frompos,trypos,topos
- if alphabet[trypos] != str[trypos]:
- topos = trypos
- else:
- frompos = trypos
- if topos-frompos==1:
- if alphabet[topos] != str[topos]:
- print alphabet[frompos]
- else:
- print alphabet[topos]
- break
- >>> import string
- >>> sourcelist = 'abcdefghijklmnopqrstuvwx'
- >>> [letter for letter in string.ascii_lowercase if letter not in sourcelist]
- ['y', 'z']
- >>>
- >>> string.ascii_lowercase
- 'abcdefghijklmnopqrstuvwxyz'
- >>> string.letters
- 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
- >>> string.hexdigits
- '0123456789abcdefABCDEF'
- >>> string.octdigits
- '01234567'
- >>> string.digits
- '0123456789'
- >>> string.printable
- '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~ tnrx0bx0c'
- >>>
- print chr(2847 - sum(map(ord, theString)))
- def missing_letter_basic(s):
- for letter in string.ascii_lowercase:
- if letter not in s: return letter
- raise Exception("No missing letter")
- find_missing_letter = dict((string.ascii_lowercase[:i]+string.ascii_lowercase[i+1:],
- string.ascii_lowercase[i]) for i in range(26)).get
- >>> find_missing_letter('abcdefghijklmnoprstuvwxyz')
- 'q'
- "b" "m" "y"
- bisect : 2.762 2.872 2.922 (Phil H)
- find_gap : 3.388 4.533 5.642 (unwind)
- listcomp : 2.832 2.858 2.822 (monkut)
- listcomp_set : 4.770 4.746 4.700 As above, with sourcelist=set(sourcelist) first
- set_difference : 2.924 2.945 2.880 (Phil H)
- sum : 3.815 3.806 3.868
- sum_imap : 3.284 3.280 3.260
- basic : 0.544 1.379 2.359
- dict_lookup : 0.135 0.133 0.134
- for i in xrange(1, len(a)):
- if a[i] != a[i - 1] + 1:
- print a[i - 1] + 1, "is missing"
- letterSet = set()
- for word in wordList:
- letterSet.update(set(word.lower()))
- import string
- alphabet = set(string.lowercase)
- missingLetters = alphabet.difference(letterSet)
- class MissingFinder(object):
- "A simplified missing items locator"
- def __init__(self, alphabet):
- "Store a set from our alphabet"
- self.alphabet= set(alphabet)
- def missing(self, sequence):
- "Return set of missing letters; sequence not necessarily set"
- return self.alphabet.difference(sequence)
- >>> import string
- >>> finder= MissingFinder(string.ascii_lowercase)
- >>> finder.missing(string.ascii_lowercase[:5] + string.ascii_lowercase[6:])
- >>> set(['f'])
- >>> # rinse, repeat calling finder.missing
Add Comment
Please, Sign In to add comment