Python3: Alphabet symmetry

Alphabet symmetry

Codewars –…

Consider the word "abode". We can see that the letter a is in position 1 and b is in position 2. In the alphabet, a and b are also in positions 1 and 2. Notice that d and e also occupy the positions they would occupy in the alphabet, which are positions 4 and 5.

Given an array of words, return an array of the number of letters that occupy their positions in the alpahabet for each word. For example, solve(["abode","ABc","xyzD"]) = [4,3,1]. See test cases for more examples.

Input will consist of alphabet characters, both uppercase and lowercase. No spaces.

Good luck!

Best Solution

def solve(arr): 
 return [['abcdefghijklmnopqrstuvwxyz'.index(y) == x for x,y in enumerate(e.lower())].count(True) for e in arr]

My Solution

def solve(arr):
    return list(map(lambda s: sum(1 if i+1==num else 0 for i,num in enumerate(list(map(lambda x: x-96, map(ord,s.lower()))))), arr))


My Comment

I would say that codewars are mainly about one-liners and lambdas. What can simply be more effective? I have converted my solution from 12 lines which were more easy to debug to one-liner.

My idea is simple, firstly I convert chars to numbers represented using alphabets by map function and ord (inverse of char) and get some ASCII index which is moved from first alphabetic number by number 96.

After that it is quite easy., again lambdas and maps. Yes, that’s my solution.