-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpig_latin.py
32 lines (28 loc) · 924 Bytes
/
pig_latin.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
'''
PROBLEM STATEMENT:
~~~~~~~~~~~~~~~~~~
Design a program to take a word as an input, and then encode it into a Pig Latin.
'''
'''
DESIGN:
~~~~~~
if the first letter is a vowel:
append 'way' to the end of the word and return that
otherwise:
iterate over all chars that are not vowels, keep track on index
slice where the first vowel is found, rearrange, and put back together
append 'ay' to the end of the word and return that
'''
def toPigLatin(_str: str) -> str:
vowels = ['a', 'e', 'i', 'o', 'u']
firstLetter = _str[0]
if (firstLetter in vowels): # words that start with a vowel
return f"{_str}yay"
else: # words that start with a consonant
index = 0
for char in _str:
if (char in vowels):
break
index += 1
return f"{_str[index:]}{_str[:index]}ay"
print(toPigLatin("yello"))