Program to convert a given string to Pig Latin (#9712)

* Program to convert a given string to Pig Latin 

This is a program to convert a user given string to its respective Pig Latin form

As per wikipedia (link: https://en.wikipedia.org/wiki/Pig_Latin#Rules)
For words that begin with consonant sounds, all letters before the initial vowel are placed at the end of the word sequence. Then, "ay" is added, as in the following examples:
"pig" = "igpay"
"latin" = "atinlay"
"banana" = "ananabay"

When words begin with consonant clusters (multiple consonants that form one sound), the whole sound is added to the end when speaking or writing.
"friends" = "iendsfray"
"smile" = "ilesmay"
"string" = "ingstray"

For words that begin with vowel sounds, one just adds "hay", "way" or "yay" to the end. Examples are:
"eat" = "eatway"
"omelet" = "omeletway"
"are" = "areway"

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update pig_latin.py

Added f-string

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update pig_latin.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update pig_latin.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update pig_latin.py

* Update pig_latin.py

* Update pig_latin.py

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Christian Clauss <cclauss@me.com>
This commit is contained in:
Rohan Sardar 2023-10-05 14:22:40 +05:30 committed by GitHub
parent f3be0ae9e6
commit e29024d14a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

44
strings/pig_latin.py Normal file
View File

@ -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) = }")