-
Notifications
You must be signed in to change notification settings - Fork 12.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added English text to pig Latin converter
- Loading branch information
1 parent
a1d4821
commit b4c1e2d
Showing
1 changed file
with
38 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
""" | ||
This program converts English text to Pig-Latin. In Pig-Latin, we take the first letter of each word, | ||
move it to the end, and add 'ay'. If the first letter is a vowel, we simply add 'hay' to the end. | ||
The program preserves capitalization and title case. | ||
For example: | ||
- "Hello" becomes "Ellohay" | ||
- "Image" becomes "Imagehay" | ||
- "My name is John Smith" becomes "Ymay amenay ishay Ohnjay Mithsmay" | ||
""" | ||
|
||
|
||
def pig_latin_word(word): | ||
vowels = "AEIOUaeiou" | ||
|
||
if word[0] in vowels: | ||
return word + "hay" | ||
else: | ||
return word[1:] + word[0] + "ay" | ||
|
||
def pig_latin_sentence(text): | ||
words = text.split() | ||
pig_latin_words = [] | ||
|
||
for word in words: | ||
# Preserve capitalization | ||
if word.isupper(): | ||
pig_latin_words.append(pig_latin_word(word).upper()) | ||
elif word.istitle(): | ||
pig_latin_words.append(pig_latin_word(word).title()) | ||
else: | ||
pig_latin_words.append(pig_latin_word(word)) | ||
|
||
return ' '.join(pig_latin_words) | ||
|
||
user_input = input("Enter some English text: ") | ||
pig_latin_text = pig_latin_sentence(user_input) | ||
print("\nPig-Latin: " + pig_latin_text) |