How to find the length/interval between two alphabets in reverse order?

Viewed 140

I have a list of paired strings. I want to find the length/interval between two alphabets. So far I'm able to find the interval for ordered alphabets using

alpha =["AM",  "KQ",  "ZN",  "XM",  "UK"]
    
length = []
for i in alpha:
    length.append(len([chr(k) for k in range(ord(i[0]),ord(i[1]))]))
    print(length)
[12]
[12, 6]
[12, 6, 0]
[12, 6, 0, 0]
[12, 6, 0, 0, 0]

I'm unable to find the interval for reversed alphabets?

How to find the length/interval between two alphabets in reverse order? Is there a python function to find interval/length between two alphabets in different orders like A-Z or X-A?

5 Answers

Instead of generating a list between ord(i[0]) and ord(i[1]), and finding its length, there's a better solution, just finding the difference between ord(i[0]) and ord(i[1]).

This doesn't immediately solve the issue for reversed alphabets, since a reversed alphabet would generate a negative number, but you can solve this with the builtin abs() function which takes the absolute value of a number:

length.append(abs(ord(i[0]) - ord(i[1])))

Why don't you simply do this?

alpha = ["AM", "KQ", "ZN", "XM", "UK"]

length = []
for i in alpha:
    if ord(i[0]) >= ord(i[1]):
        diff = ord(i[0]) - ord(i[1])
    else:
        diff = ord(i[1]) - ord(i[0])
    length.append(diff)
    print(length)
    
# [12]
# [12, 6]
# [12, 6, 12]
# [12, 6, 12, 11]
# [12, 6, 12, 11, 10]

To my knowledge, there is not, however, what you could do is check the order of them and use whichever comes first as the first argument to find the length. So, if the first letter comes after the second letter, you would use that as the first argument to the length checker. You would also need to check if the letters are the same. The code can be done as follows.

alpha =["AM",  "KQ",  "ZN",  "XM",  "UK"]
    
length = []
for i in alpha:
    if(len([chr(k) for k in range(ord(i[0]),ord(i[1]))]) == 0):
        if(len([chr(k) for k in range(ord(i[1]),ord(i[0]))]) == 0):
            length.append(0)
        else:
            length.append(len([chr(k) for k in range(ord(i[1]),ord(i[0]))]))
    else:
        length.append(len([chr(k) for k in range(ord(i[0]),ord(i[1]))]))
print(length)

Also, you should probably move your print statement out of the for loop, as it is somewhat extraneous to print it every time, or just do one argument each time.

You can use a shortcut conditional expression and check if the left value is greater than the right value.

alpha = ["AM", "KQ", "ZN", "XM", "UK"]
length = []
for i in alpha:
    length.append(ord(i[0]) - ord(i[1])) if ord(i[0]) > ord(i[1]) else length.append(ord(i[1]) - ord(i[0]))

print(length)

Or in a bit more readable way:

alpha = ["AM", "KQ", "ZN", "XM", "UK"]
length = []
for i in alpha:
    a, b = ord(i[0]), ord(i[1])
    length.append(a - b) if a > b else length.append(b - a)

print(length)

Output

[12, 6, 12, 11, 10]

maybe you need if condition 'ZN'--in your code produces zero--0 if ord(element[0]>ord[element[1] then reverse it (to 'NZ' for example) or subtract smaller from bigger or just take abs(difference)

Related