catalins.tech with Gatsby

[Coderbyte] – Letter Changes [Easy]

2019 Feb 19th

For today’s article, we have the ‘Letter Changes’ problem. The full description from Coderbyte is below:

Have the function LetterChanges(str) take the str parameter being passed and modify it using the following algorithm. Replace every letter in the string with the letter following it in the alphabet (ie. c becomes d, z becomes a). Then capitalize every vowel in this new string (a, e, i, o, u) and finally return this modified string. 

Let’s try and put this problem in a pseudocode way before solving it.

new variable = modified string that is empty at first
vowels = list of vowels

for each letter in the given string
    replace current letter with the next letter from the alphabet 
    if letter at the current position is in the vowels' list
        capitalize letter
    else 
        pass
next letter

return the new string

Now let’s proceed to the real solution, which is longer. The first step is to create an empty variable that will store the final modified string and a list of vowels.

We also know that we have to check each letter in the given string. That means a for loop is needed. However, the string might contain uppercase and lowercase letters. Therefore, the first thing in the for loop is to transform each character to lowercase.

def LetterChanges(str): 
    
    # holds the newly created String
    newStr = ''
    
    # store the vowels in a list
    vowels = ['a', 'e', 'i', 'o', 'u']
    
    # loop over each character in the string
    for ch in str:
        
        # transform the character to lowercase
        ch = ch.lower()

Up to this point, it is not difficult to understand what happens. Besides the uppercase and lowercase letters, a string might also contain special characters such as “!”#$%&'()*+,-./:;<=>?@[\]^_`{|}~”, which need to be removed. To do so, we check the ASCII code of the letter at the current position. The uppercase letters are represented with a number from 65 to 90. On the other hand, lowercase letters are represented with a number between 97 and 122. Thus, if the current letter is between 97 and 122 is a lowercase letter – NO special character. The Python’s ord() function returns the ASCII code of the letter.

To replace the current letter with the next alphabet letter, we need to add 1 to the ASCII code of the letter. The Python’s chr() method returns a letter when given an ASCII code.

However, before replacing the letters, we need to check if the current letter is ‘z’. If it is ‘z’, we just swap it with ‘a’ because if we add 1 to the ASCII code of ‘z’, it returns a special character.

That is best represented by the code below.

def LetterChanges(str): 
    
    # holds the newly created String
    newStr = ''
    
    # store the vowels in a list
    vowels = ['a', 'e', 'i', 'o', 'u']
    
    # loop over each character in the string
    for ch in str:
        
        # transform the character to lowercase
        ch = ch.lower()
        
        # check if the character is a letter and not a special character such as "!,.%" etc
        # ord function returns the ASCII code of the character
        # in ASCII, the lowercase letters are starting from 97 (a) to 122 (z)
        if ord(ch) > 96 and ord(ch) < 123:
            
            # if the char is z we need to change to a
            # as in the ASCII table, z is followed by {
            # http://www.asciitable.com/
            if ch == 'z':
                ch = 'a'
                
            # chr function takes the ASCII code and converts it back to a character
            # we add 1 to the actual ASCII code of the character and convert it to a character again
            else:
                ch = chr(ord(ch) + 1)

There are two things left now – check if the letter is a vowel and add the letter to the empty variable. If the letter is a vowel, it is transformed into an uppercase letter by calling the method upper() on it.

Now add the character/letter to the variable and repeat the same process for the next letter until there is no letter left. The full solution is below:

def LetterChanges(str): 
    
    # holds the newly created String
    newStr = ''
    
    # store the vowels in a list
    vowels = ['a', 'e', 'i', 'o', 'u']
    
    # loop over each character in the string
    for ch in str:
        
        # transform the character to lowercase
        ch = ch.lower()
        
        # check if the character is a letter and not a special character such as "!,.%" etc
        # ord function returns the ASCII code of the character
        # in ASCII, the lowercase letters are starting from 97 (a) to 122 (z)
        if ord(ch) > 96 and ord(ch) < 123:
            
            # if the char is z we need to change to a
            # as in the ASCII table, z is followed by {
            # http://www.asciitable.com/
            if ch == 'z':
                ch = 'a'
                
            # chr function takes the ASCII code and converts it back to a character
            # we add 1 to the actual ASCII code of the character and convert it to a character again
            else:
                ch = chr(ord(ch) + 1)
    
        # if the character is a vowel, just make it uppercase
        if ch in vowels:
            ch = ch.upper()
        
        # add the character to the String 
        # repeat again for the next char
        newStr += ch
    
    # code goes here 
    return newStr

The Coderbyte solution is similar and can be seen below:

def LetterChanges(str): 
  
  # our new string with the modified characters
  newString = ""

  # begin by looping through each character in the string
  for char in str:
    
    # check if the current character is an alphabetic character
    if char.isalpha():

      # check if character is z
      if char.lower() == 'z':
        char = 'a'

      # if alphabetic character then add 1 to its ASCII value 
      # by using the built-in ord function then convert back to character
      else:
        char = chr(ord(char) + 1)

    # if new character is a vowel then capitalize it
    if char in 'aeiou':
      char = char.upper()

    # add this modified character to the new string
    newString = newString + char

  return newString