DEV Community

HHMathewChan
HHMathewChan

Posted on • Originally published at rebirthwithcode.tech

Python Exercise 13: Scottish Screaming

Question

  • write a program to transform a speech to a strong Scottish accent
  • a strong Scottish accent makes every vowel similar to an "e",
    • so you should replace every vowel with an "e".
  • Additionally, it is being screamed, so it should be in block capitals.

  • Create a function that takes a string and returns a string.

example

to_scottish_screaming("hello world") ➞ "HELLE WERLD"

to_scottish_screaming("Mr. Fox was very naughty") ➞ "MR. FEX WES VERY NEEGHTY"

to_scottish_screaming("Butterflies are beautiful!") ➞ "BETTERFLEES ERE BEEETEFEL!"
Enter fullscreen mode Exit fullscreen mode

My attempt

Fail attempt

  • algorithm
>Turn a sentence to Scottish accent
>>replace all the vowel in the string with e and capitalise the whole sentence
>>>find every vowel in the string 
   set up a vowel list
   for every letter equals to one of the vowel list it is a vowel
>>>replace the vowel e
>>>capitalise the string
Enter fullscreen mode Exit fullscreen mode
  • code
def to_scottish_screaming(string: str):  
    vowel_list = ["a", "e", "i", "o", "u"]  
    string = string.lower()  
    for letter in string:  
        if letter in vowel_list:  
            scottish_scraming = string.replace(letter, "e")  
    scottish_scraming = scottish_scraming.upper()  
    return scottish_scraming
Enter fullscreen mode Exit fullscreen mode
  • This fail as the scottish_scraming only replace one letter when updated.

Attempt 2 (success)

  • algorithm
>Turn a sentence to Scottish accent
>>replace all the vowel in the string with e and capitalise the whole sentence
>>>find every vowel in the string
   initialise a vowel list
   check the letter in the string one by one
   if the letter is exist in the vowel list, then it is vowel
>>>replace the vowel with e and add to a new string
   initialise an empty string
   if the letter is a vowel, add "e" to the new string at same position
   otherwise, add the original letter to the new string at same position
>>>capitalise the new string
   call upper() method on the string
>>>print out the result
Enter fullscreen mode Exit fullscreen mode
  • code
def to_scottish_screaming(string: str):  
    string = string.lower()  
    vowel_list = ["a", "e", "i", "o", "u"]  
    scottish_scraming = str()  
    for letter in string:  
        scottish_scraming += "e" if letter in vowel_list else letter  
    scottish_scraming = scottish_scraming.upper()  
    return scottish_scraming  


print(to_scottish_screaming("A, wonderful, day, don't, you, think?"))
Enter fullscreen mode Exit fullscreen mode

Other solution

use re.sub() method

  • the first argument is pattern or string
  • the [] is used to indicate a set of characters,
    • so AIOU can be list individually,
    • AIOU will become "A", "I", "O", "U"
    • i.e. replace "A", "I", "O", "U" with 'E' in the upper-cased of the string
import re  


def to_scottish_screaming(string: str):  
    scottish_scraming = re.sub("[AIOU]", "E", string.upper())  
    return scottish_scraming
Enter fullscreen mode Exit fullscreen mode

My reflection

  • just keep praticing writing algorithm first to solve proble, a bit more familiar to work in this way.
  • re.sub() method is much simple in replace characters in a string, I should remember next time.

Credit

Discussion (0)