+ 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

18th Mar 2023, 3:14 PM
Vaibhav Pandey
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!
18th Mar 2023, 4:26 PM
Per Bratthammar
Per Bratthammar - avatar
+ 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)
18th Mar 2023, 4:33 PM
Lothar
Lothar - avatar
+ 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] ^^
18th Mar 2023, 6:56 PM
Lothar
Lothar - avatar
+ 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.
18th Mar 2023, 3:23 PM
Per Bratthammar
Per Bratthammar - avatar
+ 2
# Hi, Vaibhav Pandey ! # You can compare your code this one: print(*(s[1:] + s[0] + "ay" for s in input().split()))
18th Mar 2023, 3:21 PM
Per Bratthammar
Per Bratthammar - avatar
+ 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.
18th Mar 2023, 3:37 PM
Per Bratthammar
Per Bratthammar - avatar
+ 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
18th Mar 2023, 4:05 PM
Per Bratthammar
Per Bratthammar - avatar
+ 1
Per Bratthammar 😂😂you got me bro
18th Mar 2023, 3:32 PM
Vaibhav Pandey
+ 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.
18th Mar 2023, 3:45 PM
Vaibhav Pandey
+ 1
Thank you bro u r awesome 😎 the code is now working.
18th Mar 2023, 4:32 PM
Vaibhav Pandey
0
Per Bratthammar But bro how to compare your code with mine i don't understand your code properly it's too high level
18th Mar 2023, 3:34 PM
Vaibhav Pandey
0
Per Bratthammar Great the error is solved but the problem is still not working for test result 3 and 5 in "Pig Latin".
18th Mar 2023, 4:04 PM
Vaibhav Pandey
0
Per Bratthammar any suggestions? i don't know how to solve it.
18th Mar 2023, 4:09 PM
Vaibhav Pandey
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.
18th Mar 2023, 4:40 PM
Vaibhav Pandey
0
My any code was not working because I m a beginner
20th Mar 2023, 5:42 AM
á‚áŽ„áŽ·á—áá•á‚áŹ ᏕᏂᏗᏒᎷᏗ
á‚áŽ„áŽ·á—áá•á‚áŹ ᏕᏂᏗᏒᎷᏗ - avatar