DEV Community

Stanley Zheng
Stanley Zheng

Posted on

Codefights alphabeticShift

Solutions for this codefight problem solution
https://app.codesignal.com/arcade/intro/level-6/PWLT8GBrv9xXy4Dui/description

Given a string, replace each its character by the next one in the English alphabet (z would be replaced by a).

Example

For inputString = "crazy", the output should be
alphabeticShift(inputString) = "dsbaz".

First Pass with mutuable memory

def alphabeticShift(inputString):

    new_word = []
    for i in inputString:
        current = ord(i)
        print (current)
        current = (current + 1)
        if current > 122:
            current = current - 26

        new_word.append(chr(current))

    return ''.join(new_word)
Enter fullscreen mode Exit fullscreen mode

second pass a bit cleaner using lambdas



def ordshift(i):
    current = ord(i) + 1
    if current > 122:
        current = current - 26
    return chr(current)

def alphabeticShift(inputString):

    new_word = list(map(ordshift, inputString ))
    return ''.join(new_word)

Enter fullscreen mode Exit fullscreen mode

Top comments (0)