The challenge
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 also that d
and e
in abode
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 alphabet 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.
The solution in Python code
Option 1:
from operator import eq
from string import ascii_lowercase
def solve(strings):
return [sum(map(eq, s.lower(), ascii_lowercase)) for s in strings]
Option 2:
def solve(arr):
return [sum(i == ord(c) - ord('A') for i, c in enumerate(s.upper())) for s in arr]
Option 3:
def solve(words):
return [sum(a==b for a, b in zip(w.lower(), 'abcdefghijklmnopqrstuvwxyz')) for w in words]
Test cases to validate our solution
test.it("Basic tests")
test.assert_equals(solve(["abode","ABc","xyzD"]),[4,3,1])
test.assert_equals(solve(["abide","ABc","xyz"]),[4,3,0])
test.assert_equals(solve(["IAMDEFANDJKL","thedefgh","xyzDEFghijabc"]),[6,5,7])
test.assert_equals(solve(["encode","abc","xyzD","ABmD"]),[1, 3, 1, 3])