Skip to content

Commit e793920

Browse files
RohanSardarpre-commit-ci[bot]cclauss
authored andcommitted
Program to convert a given string to Pig Latin (TheAlgorithms#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 <[email protected]>
1 parent ae92a33 commit e793920

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

Diff for: strings/pig_latin.py

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
def pig_latin(word: str) -> str:
2+
"""Compute the piglatin of a given string.
3+
4+
https://en.wikipedia.org/wiki/Pig_Latin
5+
6+
Usage examples:
7+
>>> pig_latin("pig")
8+
'igpay'
9+
>>> pig_latin("latin")
10+
'atinlay'
11+
>>> pig_latin("banana")
12+
'ananabay'
13+
>>> pig_latin("friends")
14+
'iendsfray'
15+
>>> pig_latin("smile")
16+
'ilesmay'
17+
>>> pig_latin("string")
18+
'ingstray'
19+
>>> pig_latin("eat")
20+
'eatway'
21+
>>> pig_latin("omelet")
22+
'omeletway'
23+
>>> pig_latin("are")
24+
'areway'
25+
>>> pig_latin(" ")
26+
''
27+
>>> pig_latin(None)
28+
''
29+
"""
30+
if not (word or "").strip():
31+
return ""
32+
word = word.lower()
33+
if word[0] in "aeiou":
34+
return f"{word}way"
35+
for i, char in enumerate(word): # noqa: B007
36+
if char in "aeiou":
37+
break
38+
return f"{word[i:]}{word[:i]}ay"
39+
40+
41+
if __name__ == "__main__":
42+
print(f"{pig_latin('friends') = }")
43+
word = input("Enter a word: ")
44+
print(f"{pig_latin(word) = }")

0 commit comments

Comments
 (0)