Codewars – https://www.codewars.com/kata/…
Consider the word
"abode". We can see that the letter
a is in position
b is in position
2. In the alphabet,
b are also in positions
2. Notice that
e also occupy the positions they would occupy in the alphabet, which are positions
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.
def solve(arr): return [['abcdefghijklmnopqrstuvwxyz'.index(y) == x for x,y in enumerate(e.lower())].count(True) for e in arr]
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)) print(solve(["IAMDEFANDJKL","thedefgh","xyzDEFghijabc"]))
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.