Created
August 22, 2016 15:34
-
-
Save trueskawka/f50f2c4a01d3142ed3dd4f97183b2fe7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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