Skip to content

Program to convert a given string to Pig Latin #9712

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Oct 5, 2023
44 changes: 44 additions & 0 deletions strings/pig_latin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
def pig_latin(word: str) -> str:
"""Compute the piglatin of a given string.
https://en.wikipedia.org/wiki/Pig_Latin
Usage examples:
>>> pig_latin("pig")
'igpay'
>>> pig_latin("latin")
'atinlay'
>>> pig_latin("banana")
'ananabay'
>>> pig_latin("friends")
'iendsfray'
>>> pig_latin("smile")
'ilesmay'
>>> pig_latin("string")
'ingstray'
>>> pig_latin("eat")
'eatway'
>>> pig_latin("omelet")
'omeletway'
>>> pig_latin("are")
'areway'
>>> pig_latin(" ")
''
>>> pig_latin(None)
''
"""
if not (word or "").strip():
return ""
word = word.lower()
if word[0] in "aeiou":
return f"{word}way"
for i, char in enumerate(word): # noqa: B007
if char in "aeiou":
break
return f"{word[i:]}{word[:i]}ay"


if __name__ == "__main__":
print(f"{pig_latin('friends') = }")
word = input("Enter a word: ")
print(f"{pig_latin(word) = }")