+ 3
Could anyone please tell me why the code is not working in the code coach problem named "Pig Latin". ???
string=input().replace(" ",",").split(",") h=[j.replace(j[0],"") for j in string] m=[i[0]+"ay" for i in string] li=[f"{pi}{si}" for pi,si in zip(h,m)] my_str=" ".join(li) print(my_str
15 Antworten
+ 4
# Vaibhav Pandey
# Yes! I found how to fix it!
# When you use replace, it replace all characters as default. You only need to replace (the first) one of them.
# You do that by putting a third argument in replace -> j.replace(j[0], "", 1)
string = input().split()
h = [j.replace(j[0], "", 1) for j in string]
m = [i[0] + "ay" for i in string]
li = [f"{pi}{si}" for pi, si in zip(h, m)]
my_str = " ".join(li)
print(my_str)
# You can try it now!
+ 7
Vaibhav Pandey ,
i think it is important to understand for you why your code try is failing. optimizing your code for readability and efficiency can follow.
(1) this line creates a wrong result. *j* contains the first character of the word, for *nevermind* j is *n*. then *replace()* is called.
this will replace **all** occurrences of *n*. the result is now: *evermid*, so the second *n* in this word is missing now.
(2) we can avoid this effect by using a slice instead of replace(). it starts at index position 1, and takes all characters upto the end of the word.
string=input().replace(" ",",").split(",")
#h=[j.replace(j[0],"") for j in string] # (1)
h = [j[1:] for j in string] # (2)
m=[i[0]+"ay" for i in string]
li=[f"{pi}{si}" for pi,si in zip(h,m)]
my_str=" ".join(li)
print(my_str)
+ 6
Vaibhav Pandey ,
you wrote this: h=[j.replace(j[0],"") for j in string]
(1) (2)
(1) means that the complete string is used
(2) means that it takes the first character from the string, and replace all occurrences of it
> what we can do is to *limit the replacemant to only the first occurrence*. replace() has an argument that accepts an integer value for that purpose:
...
h=[j.replace(j[0],"", 1) for j in string]
^^
+ 3
But start with fixing the the parenthesis at the last lineâŠ
If you compare your code to mine, you will see that min works for example if you input an empty string or a space, but your gives a out of range error.
+ 2
# Hi, Vaibhav Pandey !
# You can compare your code this one:
print(*(s[1:] + s[0] + "ay" for s in input().split()))
+ 2
You can compare the result the diffrent codes. They should give the same results. If the donât, then you have to figure out why it is so.
+ 2
Vaibhav Pandey
Yes, I now undestand the problem.
When you have two same letters like âggâ, your code just take one of them to the result!!!
Your code -> ray ytay jhay yay
My code -> rray ytay jhhay yyay
+ 1
Per Bratthammar đđyou got me bro
+ 1
Per Bratthammar When I used your code with no input it doesn't show any error but with my code it shown out of range error.
+ 1
Thank you bro u r awesome đ the code is now working.
0
Per Bratthammar But bro how to compare your code with mine i don't understand your code properly it's too high level
0
Per Bratthammar Great the error is solved but the problem is still not working for test result 3 and 5 in "Pig Latin".
0
Per Bratthammar any suggestions? i don't know how to solve it.
0
Lothar Great explanation bro now I understand it but I wrote j[0] that means it should replace only 1st character of the string j, don't understand why it replaced all occurrences of the first word.
0
My any code was not working because I m a beginner