Last active
September 10, 2020 10:21
-
-
Save willy-r/2a99b71c69a862b2df4701b362fa704e to your computer and use it in GitHub Desktop.
Write a function that converts a sentence into Pig Latin (https://en.m.wikipedia.org/wiki/Pig_Latin#Rules).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def _to_pig_latin(word: str): | |
"""Converts the word into Pig Latin.""" | |
VOWELS = 'aeiou' | |
if word[0].lower() in VOWELS: | |
return f'{word}yay' | |
consonants = 0 | |
for letter in word.lower(): | |
if letter not in VOWELS: | |
consonants += 1 | |
continue | |
return f'{word[consonants:]}{word[:consonants]}ay' | |
def to_pig_latin(sentence: str): | |
"""Converts the sentence into Pig Latin.""" | |
pig_latin_sentence = [_to_pig_latin(word) for word in sentence.split()] | |
return ' '.join(pig_latin_sentence) | |
if __name__ == '__main__': | |
CASES = ( | |
('pig', 'igpay'), | |
('latin', 'atinlay'), | |
('banana', 'ananabay'), | |
('smile', 'ilesmay'), | |
('string', 'ingstray'), | |
('stupid', 'upidstay'), | |
('eat', 'eatyay'), | |
('omelet', 'omeletyay'), | |
('are', 'areyay'), | |
('I love Cassidy newsletter', 'Iyay ovelay assidyCay ewsletternay'), | |
) | |
for sentence, expected in CASES: | |
result = to_pig_latin(sentence) | |
assert result == expected, f'{result} != {expected}' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment