Pig Latin
Note: This page contains a small but interesting piece of Python code which I call snippets. You can find more such codes on my Python snippets page.
Contents
Problem ¶
You have two friends who are speaking Pig Latin to each other! Pig Latin is the same words in the same order except that you take the first letter of each word and put it on the end, then you add 'ay' to the end of that. ("road" = "oadray")
Task
Your task is to take a sentence in English and turn it into the same sentence in Pig Latin!
Input Format
A string of the sentence in English that you need to translate into Pig Latin. (no punctuation or capitalization)
Output Format
A string of the same sentence in Pig Latin.
Sample Input
"nevermind youve got them"
ample Output
"evermindnay ouveyay otgay hemtay"
Solution ¶
Here is my solution to the above problem. Remember that there could be more than one way to solve a problem. If you have a more efficient or concise solution, please leave a comment.
print(" ".join([word[1:]+word[0]+"ay" for word in input().split()]))
Explanation ¶
My approach (or the algorithm)¶
- Seperate each word from the sentence.
- Remove the first letter from each word.
- Add the removed first letter at the end of each word.
- Add "ay" at the end of each word.
- Join words into a sentence.
The code¶
-
split()
Splits each word in the sentence into a list. -
[word[1:]
Start with the word without its first letter. -
+word[0]
Add first letter at the end of the word. -
+"ay"
add "ay" at the end. -
[word[1:]+word[0]+"ay" for word in input().split()]
Perform all the steps above on all words using list comprehension. -
" ".join()
Join them into a sentence.
The problem question is picked from SoloLearn. Here is my SoloLearn code and my SoloLearn profile page.
Last updated 2021-01-09 18:02:21.275982 IST
Comments