-
Notifications
You must be signed in to change notification settings - Fork 0
/
day_forty.py
61 lines (48 loc) · 1.88 KB
/
day_forty.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env python3
"""Write a function called translate that takes the following
words and translates them into pig Latin.
a = 'i love python'
Here are the rules:
1. If a word starts with a vowel (a,e, i, o, u) add 'yay' at the
end. For example, 'eat' will become 'eatyay'
2. If a word starts with anything other than a vowel, move
the first letter to the end and add 'ay' to the end. For
example, 'day' will become 'ayday'.
Your code should return:
iyay ovelay ythonpa"""
from collections import deque
def translate(stringInEnglish: str) -> str:
"""
Translates a given English string into Pig Latin.
Args:
stringInEnglish (str): The input string in English.
Returns:
str: The translated string in Pig Latin.
Example:
>>> translate("The arc of a life well-lived")
"hetay arcyay ofyay ayay ifelay ell-livedway"
"""
# Split the input English string into words and convert to lowercase.
wordList = stringInEnglish.lower().split(" ")
pig_latin_word_list = []
# Iterate through each word in the input.
for word in wordList:
# Check if the word starts with a vowel.
if word.startswith(("a", "e", "i", "o", "u")):
# If it starts with a vowel, add "yay" to the end.
word += "yay"
pig_latin_word_list.append(word)
else:
# If it doesn't start with a vowel, convert to Pig Latin.
wordDeque = deque(word)
wordDeque.rotate(-1) # moves first letter to the end of word
wordDeque.append("ay")
pig_latin_word_list.append("".join(wordDeque))
# Join the Pig Latin words and return the translated string.
return " ".join(pig_latin_word_list)
# Test cases
assert (
translate("The arc of a life well-lived")
== "hetay arcyay ofyay ayay ifelay ell-livedway"
)
assert translate("I love python") == "iyay ovelay ythonpay"