Checking if a string's characters are ascending alphabetically and its ascent is evenly spaced python

Posted by FRU5TR8EDD on Stack Overflow See other posts from Stack Overflow or by FRU5TR8EDD
Published on 2012-03-29T09:17:21Z Indexed on 2012/04/14 5:28 UTC
Read the original article Hit count: 191

Filed under:
|

So need to check if a string's characters are ascending alphabetically and if that ascent is evenly spaced.

a = "abc"
b = "ceg"

So a is alphabetically ascending and it's spacing is 1 (if you convert to the ordinal values they are 97,98,99). And b is also alphabetically ascending and it's spacing is 2 (99,101,103).

And I am sticking with the following code:

a = 'jubjub'
words1 = []
ords = [ord(letter) for letter in a]
diff = ords[1] - ords[0]
for ord_val in range(1, len(ords)-1):
    if diff > 0:
        if ords[ord_val + 1] - ords[ord_val] == diff:
            if a not in words1:
                words1.append((a, diff))
print words1

How come 'jubjub' works, 'ace' works, but 'catcat' doesn't?

© Stack Overflow or respective owner

Related posts about python

Related posts about homework