23 Answers
New AnswerI am trying to solve the code coach "Longest common substring". I wrote a code that in my mind is supposed to work but it prints "{" that is how I initialized the array where I store the substring to output. I choose "{" because it comes after "z" as ASCII value. I can see no bug, please help me.🙏 I filled the code with comments to make it easy to read and left a sample input to copy/paste at the top of the code. EDIT: now the code works, but fails the last test https://code.sololearn.com/c4JRT6EmYVIq/?ref=app
11/22/2020 8:35:26 PM
Davide23 Answers
New AnswerDavide Crazy thought: what if the very last test case the common substring is one char long? I tried this, and your code prints {
Omg of course 🤦 I need a do while. The condition is for word[w] but I initialize word[w] into the loop. So the condition is not met since the very first time 🤦
The string that you posted wasn't handled properly because x is the first letter of the first word and the loop with k till k>j doesn't acces the first letter (k=0)
Liked this one so much I typed up a quick solution in Python: https://code.sololearn.com/cCxE5xFSY514/?ref=app
NotAPythonNinja nice job 👌 I:ll try to learn Python from your code😃🤗 It's nice to see how short is the code in Python
NotAPythonNinja yes it works for the sample string that I gave. But it fals half of the tests in the code coach 😞🤔 I am going to copy the one that I can see
NotAPythonNinja I tried the one failed that I can see and here it worked😅 I was missing the +1 in strncpy in the code of the code coach. Now the program fails only the last test and I can't see it 😭
NotAPythonNinja I completed the code coach 🎉🥳 Thank you so much for the help 🤗🤗🤗 The problem is that the for with i and j run till i or j < len so if len is 1 they are not entered. And the for with k should run till k>=j for the same reason. edit: the loops for i and j were actually right. The one with k was the only bugged one
I tried it for you and it fails half of the tests. I can see one of them: funny funeral funfair
Davide I had a go at translating my Python solution back into C, and arrived at this monstrosity: Well, it works. 😁😂 https://code.sololearn.com/cxsq0hr27KrV/?ref=app
NotAPythonNinja I tried it for you and it passes all the tests! 🎉🥳🥳🥳 However I tried entering 3 words of 2 letters like fg fg fg and it doesn't work properly. And you can remove the while at the beginning to take off new lines because fgets strops at the first \n, it doesn't read multiple lines
NotAPythonNinja 🥳you are very welcome 🤗 It's nice to have people with whom to share and to test codes🤗
Learn Playing. Play Learning
SoloLearn Inc.
4 Embarcadero Center, Suite 1455Send us a message