Created
May 20, 2020 16:35
-
-
Save devwaseem/9bede411d701963d8dfecddd89d3cdfd 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
import string | |
punctuations = list(string.punctuation) | |
punctuations.remove("-") # we are treating hyphen as seperator so remove it from punctuation list | |
punctuations = "".join(punctuations) | |
def piglatin(ogtext): | |
def restore_punctutation(punc_indices,text): | |
text.reverse() | |
for k,v in punc_indices.items(): | |
for i in v: | |
text.insert(i,k) | |
text.reverse() | |
return "".join(text) | |
# taking a note of character indices which are uppercase | |
capital_indices = [x.isupper() if x not in punctuations else False for x in ogtext] | |
#taking a note of punctuation indices | |
punc_indices = { i:[] for i in punctuations } | |
for i in range(len(ogtext)): | |
e = ogtext[i] | |
if e in punctuations: | |
punc_indices[e].append(len(ogtext) -i -1) | |
text = [x for x in ogtext if x not in punctuations] | |
# remove punctuations that doesn't exist | |
punc_indices = { k:v for k,v in punc_indices.items() if not len(v) == 0} | |
# lower case everything since we saved the uppercase indices | |
text = list("".join(text).lower()) | |
vowels = ["a","e","i","o","u"] | |
if text[0] in vowels: #vowel": "way" suffix | |
if ogtext.endswith("way"): | |
return ogtext | |
else: | |
text = list(map(lambda x, y: y.upper() if x else y,capital_indices,text)) | |
text += list("way") | |
return restore_punctutation(punc_indices,text) | |
else: #consonant: "ay" suffix | |
text.append(text.pop(0)) | |
text = list(map(lambda x, y: y.upper() if x else y,capital_indices,text)) | |
text += list("ay") | |
return restore_punctutation(punc_indices,text) | |
print("-".join([piglatin(i) for i in input("Enter a word: ").split("-")])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Question is here:
Note: This is a python implementation.