Just curious, what’s the most pythonic/efficient way to determine if
sequence of 3 characters are in consecutive alpha order?
Below a quick&dirty way that seems to work, other, nicer implementations?
I suppose one alternative approach might be to sort a copy the
sequence and compare it with the original. Nope, that wouldn’t account
for gaps in the sequence.
(This is not homework – listeners to NPR Sunday Morning progam will
know)
def checkSequence(n1, n2, n3):
""" check for consecutive sequence of 3 """
s = ord('a')
e = ord('z')
# print n1, n2, n3
for i in range(s, e+1):
if ((n1+1) == n2) and ((n2+1) == n3):
return True
return False
def compareSlice(letters):
""" grab 3 letters and sent for comparison """
letters = letters.lower()
if checkSequence(ord(letters[0]), ord(letters[1]), ord(letters[2])):
print '==> seq: %s' % letters
return True
return False
Easy:
Alternatively, one could use
string.find().I guess a function using this would look like: