+ 5

[SPOILERS] Pig Latin - Python Solution - Is it how you are supposed to solve that?

I was wondering if there was a simpler way to get the same results. This is the first "medium" Code Coach challenge I'm doing. I am almost done with the "Python Developer" course. Let me know what you think below, if the indentation is correct or show me your solutions : sentence = input() word_list = sentence.split(" ") pigged_words_list = [] for each_word in word_list: first_letter = each_word[0] each_word = each_word + first_letter + "ay" lenght = int(len(each_word)) stripped = each_word[1:lenght] pigged_words_list.append(stripped) pigged_sentence = " ".join(pigged_words_list) print(pigged_sentence)

4th Apr 2024, 6:56 PM
Karl Breault
Karl Breault - avatar
10 ответов
+ 8
Karl Breault I agree, there is a better way, though your proposed fix does not change the output at all. The space still is added after the word. Here is an improvement to the one-liner that better aligns with how the print features were meant to work: print(*[word[1:] + word[0] + "ay" for word in input().split()]) This also passes the Code Coach tests. It works by first building a list of Pig Latin words. Then it unpacks the list using the *[ ] unpacking operator so that the contents of the list are presented to print as though they were individual arguments separated by commas instead of one whole list. Print naturally separates them in the output with spaces between. There is no trailing space, and it ends with a newline.
4th Apr 2024, 9:01 PM
Brian
Brian - avatar
+ 5
Yes, it can be done more simply. This prints each word as it is converted. words = input().split() for word in words: print(word[1:] + word[0] + "ay", end=" ") Here is the same thing as a 1-liner: [print(word[1:] + word[0] + "ay", end=" ") for word in input().split()]
4th Apr 2024, 7:09 PM
Brian
Brian - avatar
+ 5
Ordinarily print would display each word on a separate line. The code keeps the output on one line by overriding print's default line ending. It uses end=" " to replace the default of newline with space, which serves as a word separator instead of a line ending. A caveat with this technique is that it leaves an extra space hanging at the end of the line. Nonetheless, it passes all the Code Coach tests.
4th Apr 2024, 8:02 PM
Brian
Brian - avatar
+ 5
Brian , each of your both sample codes creates an unnecessarily space at the end of the output.
5th Apr 2024, 6:10 AM
Lothar
Lothar - avatar
+ 5
Brian , Tibor Santa my try is from about one year ago: print(" ".join([i[1:] + i[0] + "ay" for i in input().split()])) this is very similar to the code that Tibor Santa has posted. i used a list comprehension inside the join() function, whereas the other code uses a generator expression.
5th Apr 2024, 11:08 AM
Lothar
Lothar - avatar
+ 4
Hi Lothar you are right about the extraneous space in my first examples. I pointed that out, too. It was a deliberate compromise in order to keep the code easy to read for a beginner. Note in a follow-up post I introduced the unpacking operator to clean it up. I haven't seen the unpacking operator mentioned in Sololearn courses, so I am hesitant to recommend it in a Code Coach solution, but it seems fitting for the one-liner. If you have time, please add your coding ideas, too. I am sure Karl Breault would like more viewpoints.
5th Apr 2024, 7:40 AM
Brian
Brian - avatar
+ 3
Ty Brian, this argument is really helpful. I think in that case, the strip() or rstrip() method should be added to the print function. It is better to do things right in the first place 😊 Here's the corrected code, on a 1-liner. [print(word[1:] + word[0] + "ay".rstrip(), end=" ") for word in input().split()] .rstrip() to call the method on the printed string and remove the whitespace at the end.
4th Apr 2024, 8:12 PM
Karl Breault
Karl Breault - avatar
+ 3
Brian I see, I should have double-checked that. Thank you for sharing your knowledge and the corrected one-liner. This was really helpful.
4th Apr 2024, 9:17 PM
Karl Breault
Karl Breault - avatar
+ 2
Ty Brian, I see how this code could be improved, but Im not sure I understand how you only get one output from that code. Would you mind explaining?
4th Apr 2024, 7:43 PM
Karl Breault
Karl Breault - avatar
+ 2
A different one-liner which follows the logic of your original code, but compressed into a generator expression within join. print(' '.join(w[1:]+w[0]+'ay' for w in sentence.split())) The logic is the same: - break down the sentence to a sequence of words - with each word, use list slicing to combine the letters according to the task - finally join again all the words to a single string w[1:] is all letters except the first one (a slice with a starting index, but without an ending index).
5th Apr 2024, 9:51 AM
Tibor Santa
Tibor Santa - avatar