Skip to content

Instantly share code, notes, and snippets.

@trueskawka
Created August 22, 2016 15:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trueskawka/f50f2c4a01d3142ed3dd4f97183b2fe7 to your computer and use it in GitHub Desktop.
Save trueskawka/f50f2c4a01d3142ed3dd4f97183b2fe7 to your computer and use it in GitHub Desktop.
#piggy - pig latin
#piggy("This is a string, yeah?")
#"Isthay isay a ingstray, eahyay?"
#separate words
#check if the first letter is capitalized
#regex - find first consonant group - from the start
#if there is one - move it to the back
#add "ay"
#capitalize if it was capitalized
#y is a consonant
import re
def piggy(string):
array = string.split()
array2 = []
for x in array:
capital = x[0].isupper()
output = x
group = re.search(r'^[^aeiouAEIOU]+', x)
non_word = re.search(r'[^\w]$', x)
if group:
if non_word:
output = x[group.end():-1] + x[group.start():group.end()].lower()
else:
output = x[group.end():] + x[group.start():group.end()].lower()
is_consonant = re.search(r'[^aeiouAEIOU]', output[-1])
if is_consonant:
output += "ay"
if non_word:
output += x[-1]
if capital:
output = output.capitalize()
array2.append(output)
return " ".join(array2)
print(piggy("This is a string, yeah?"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment