Skip to content

Instantly share code, notes, and snippets.

@MasterOdin
Last active August 29, 2015 14:01
Show Gist options
  • Save MasterOdin/c325d835fd70aac5d5f8 to your computer and use it in GitHub Desktop.
Save MasterOdin/c325d835fd70aac5d5f8 to your computer and use it in GitHub Desktop.
Turns a phrase into pig latin following the rules described on https://en.wikipedia.org/wiki/Pig_Latin
# simple check for if something is a "vowel"
def isVowel(letter):
return letter in ["a","e","i","o","u","y"]
# only phrases that contain only alpha characters is accepted
valid = False
while not valid:
phrase = raw_input("Enter a phrase ==> ")
valid = phrase.replace(" ","").isalpha()
# separate it out to handle each word
words = phrase.split()
for i in range(len(words)):
word = words[i]
# special case for words that start with vowels (y doesn't count here)
if isVowel(word[0]) and word[0] != 'y':
word += "-ay"
else:
j = 1;
while j < len(word):
if isVowel(word[j]):
break
j += 1
word = word[j:]+"-"+word[:(j)]+"ay"
# make sure new word has the same first letter capitalization as old word
if word[word.index('-')+1].isupper():
word = word.lower().capitalize()
else:
word = word.lower()
words[i] = word
# .join() applies the string between all elements of the list. Easier than a for loop.
pig_latin = ' '.join(words)
print pig_latin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment