Thursday, September 21, 2023
HomeSoftware EngineeringAlphabet Symmetry in Python

Alphabet Symmetry in Python

The problem

Take into account the phrase "abode". We will see that the letter a is in place 1 and b is in place 2. Within the alphabet, a and b are additionally in positions 1 and 2. Discover additionally that d and e in abode occupy the positions they might occupy within the alphabet, that are positions 4 and 5.

Given an array of phrases, return an array of the variety of letters that occupy their positions within the alphabet for every phrase. For instance,

resolve(["abode","ABc","xyzD"]) = [4, 3, 1]

See check instances for extra examples.

Enter will include alphabet characters, each uppercase and lowercase. No areas.

The answer in Python code

Choice 1:

from operator import eq
from string import ascii_lowercase

def resolve(strings):
    return [sum(map(eq, s.lower(), ascii_lowercase)) for s in strings]

Choice 2:

def resolve(arr):
    return [sum(i == ord(c) - ord('A') for i, c in enumerate(s.upper())) for s in arr]

Choice 3:

def resolve(phrases):
  return [sum(a==b for a, b in zip(w.lower(), 'abcdefghijklmnopqrstuvwxyz')) for w in words]

Take a look at instances to validate our resolution"Fundamental assessments")
check.assert_equals(resolve(["encode","abc","xyzD","ABmD"]),[1, 3, 1, 3])


Please enter your comment!
Please enter your name here

Most Popular

Recent Comments