-
Notifications
You must be signed in to change notification settings - Fork 0
/
piglatin.py
53 lines (33 loc) · 1.1 KB
/
piglatin.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
"""Turn a phrase into Pig Latin.
This takes a space-separated phrase and returns it in Pig Latin.
Rules:
1. If the word begins with a consonant (not a, e, i, o, u),
move the first letter to the end and add 'ay'
2. If the word begins with a vowel, add 'yay' to the end
For example:
>>> pig_latin('hello awesome programmer')
'ellohay awesomeyay rogrammerpay'
"""
vowels = {"a", "e", "i", "o", "u"}
def pig_latin(phrase):
"""Turn a phrase into pig latin.
There will be no uppercase letters or punctuation in the phrase.
>>> pig_latin('hello awesome programmer')
'ellohay awesomeyay rogrammerpay'
"""
words = phrase.split(" ")
sentence = ""
for word in words:
sentence += pig_latin_word(word)
sentence += " "
return sentence[:-1]
def pig_latin_word(word):
if word[0] in vowels:
return word + "yay"
else:
return word[1:]+ word[0] + "ay"
pig_latin('hello awesome programmer')
if __name__ == '__main__':
import doctest
if doctest.testmod().failed == 0:
print "\n*** ALL TESTS PASSED. REATGAY OBJAY!\n"