Skip to content

Instantly share code, notes, and snippets.

@tyler-austin
Created June 19, 2017 13:53
Show Gist options
  • Save tyler-austin/b1df0be89cb865f33ec8b667ee31d282 to your computer and use it in GitHub Desktop.
Save tyler-austin/b1df0be89cb865f33ec8b667ee31d282 to your computer and use it in GitHub Desktop.

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".

Input/Output

  • [time limit] 4000ms (py3)

  • [input] string inputString

    Non-empty string consisting of lowercase English characters.

    Guaranteed constraints:

     1 ≤ inputString.length ≤ 10.
    
  • [output] string

    The result string after replacing all of its characters.

def alphabetic_shift(input_string):
return ''.join(chr((ord(char) - ord('a') + 1) % 26 + ord('a')) for char in input_string)
import unittest
from alphabetic_shift import alphabetic_shift
class TestAlphabeticShift(unittest.TestCase):
def test_1(self):
input_string = 'crazy'
solution = 'dsbaz'
result = alphabetic_shift(input_string)
self.assertEqual(result, solution)
def test_2(self):
input_string = 'z'
solution = 'a'
result = alphabetic_shift(input_string)
self.assertEqual(result, solution)
if __name__ == '__main__':
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment