How to Solve “reflectString” in CodeFights

The Problem:

Define an alphabet reflection as follows: a turns into z, b turns into y, c turns into x, …, n turns into m, m turns into n, …, z turns into a.

Define a string reflection as the result of applying the alphabet reflection to each of its characters.

Reflect the given string.

Example

For inputString = "name", the output should be
reflectString(inputString) = "mznv".

Input/Output

  • [time limit] 4000ms (py)
  • [input] string inputString

    A string of lowercase characters.

    Constraints:
    3 ≤ inputString.length ≤ 10.

  • [output] string

The Solution:
def reflectString(inputString):
return "".join(map(lambda x:chr(ord('z')-(ord(x)-ord('a'))),inputString))

The Explanation:

We can simply convert all the letters in the inputString and join them to a new string in one return statement. The way to convert them is to iterate over each element and for each of them;

(i) find the distance from ‘a’ –> n
(ii) go n step backwards from ‘z’
(iii) this is the letter we want in the output.
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s