+ 1

Longest common substring code coach

I 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

22nd Nov 2020, 8:35 PM
Davide
Davide - avatar
20 Respuestas
+ 4
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 🤦
22nd Nov 2020, 9:30 PM
Davide
Davide - avatar
+ 4
It works👏👏🥳
23rd Nov 2020, 12:20 AM
Davide
Davide - avatar
+ 3
So in the loop at 22 strtok is not working 🤔🤔🤔
22nd Nov 2020, 9:23 PM
Davide
Davide - avatar
+ 3
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)
22nd Nov 2020, 10:32 PM
Davide
Davide - avatar
+ 3
NotAPythonNinja nice job 👌 I:ll try to learn Python from your code😃🤗 It's nice to see how short is the code in Python
22nd Nov 2020, 11:49 PM
Davide
Davide - avatar
+ 1
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
22nd Nov 2020, 9:40 PM
Davide
Davide - avatar
+ 1
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 😭
22nd Nov 2020, 9:51 PM
Davide
Davide - avatar
+ 1
NotAPythonNinja omg you are right 😱😱😱
22nd Nov 2020, 10:08 PM
Davide
Davide - avatar
+ 1
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
22nd Nov 2020, 10:28 PM
Davide
Davide - avatar
+ 1
I tried it for you and it fails half of the tests. I can see one of them: funny funeral funfair
22nd Nov 2020, 11:52 PM
Davide
Davide - avatar
+ 1
A one-liner solution!?😱 That's a job for Python ninjas 🤣
22nd Nov 2020, 11:59 PM
Davide
Davide - avatar
+ 1
There were 2 more that I can't see but probably are for the same reason
23rd Nov 2020, 12:09 AM
Davide
Davide - avatar
+ 1
NotAPythonNinja 14 lines!?😱😱 You are on the way to become a python ninja🤣
23rd Nov 2020, 12:43 AM
Davide
Davide - avatar
+ 1
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
23rd Nov 2020, 5:51 PM
Davide
Davide - avatar
+ 1
No don't worry about that, strtok will ignore the new line at the end.
23rd Nov 2020, 6:34 PM
Davide
Davide - avatar
+ 1
NotAPythonNinja 🥳you are very welcome 🤗 It's nice to have people with whom to share and to test codes🤗
23rd Nov 2020, 10:02 PM
Davide
Davide - avatar
+ 1
hey guys
24th Nov 2020, 1:48 AM
Jojo Colebrooke
+ 1
// Hello where. Here is My version on C#. it pass all 6 tests: https://www.sololearn.com/ru/compiler-playground/c81zxq1Buzf4 using System; using System.Collections.Generic; using System.Linq; public class Program { public static void Main() { string[] words = Console.ReadLine().Split(' '); Array.Sort(words, (x, y) => x.Length.CompareTo(y.Length)); string word = words[0]; int length = word.Length; for (int i = length; i > 0; i--) { HashSet<string> substrings = new HashSet<string>(); for (int j = 0; j <= length - i; j++) { substrings.Add(word.Substring(j, i)); } List<string> ok = substrings.Where(ss => words.Skip(1).All(w => w.Contains(ss))).OrderBy(s => s).ToList(); if (ok.Any()) { Console.WriteLine(ok[0]); break; } } } }
8th Jan 2024, 2:35 PM
Игорь Акатов
Игорь Акатов - avatar
0
NotAPythonNinja thanks 🤗 After reading your comment I tried a printf after the strcpy at line 45 to print sub and I saw that the program was printing "sololear" missing the "n", so I added the +1 in the strncpy. However this wasn't the main problem 😔 It was just a bug I found thanks to you
22nd Nov 2020, 9:09 PM
Davide
Davide - avatar
- 1
Sample Input: SoloLearn Learning LearningIsFun Learnable Sample Output: Learn Explanation: Learn is the longest common substring for the words SoloLearn Learning LearningIsFun Learnable.
22nd Nov 2020, 8:36 PM
Davide
Davide - avatar