I keep failing test 4(security) and I don’t know why... help![solved] | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

I keep failing test 4(security) and I don’t know why... help![solved]

https://code.sololearn.com/c7es4GcK49j1/?ref=app

3rd Jan 2020, 3:44 PM
Maksym Karpliuk
Maksym Karpliuk - avatar
23 Answers
+ 6
Yea, it does now, you changed the code. Nevertheless, the 2nd part still counts.
3rd Jan 2020, 4:06 PM
Dennis
Dennis - avatar
+ 3
GxTxG$xx also fails. Your inner loop inside find should probably be j < s1.length() instead of s2?
3rd Jan 2020, 3:58 PM
Dennis
Dennis - avatar
+ 1
updated(if(find(sorted, "$T") or find(sorted, "T
quot;))), still failing
3rd Jan 2020, 3:56 PM
Maksym Karpliuk
Maksym Karpliuk - avatar
+ 1
I did j < s1.length() instead of s2, but it didn’t work
3rd Jan 2020, 4:33 PM
Maksym Karpliuk
Maksym Karpliuk - avatar
+ 1
String tested: GxTxT$xx Output: Alarm Your inner loop initialization was incorrect i.e int j = i; For that reason your condition wasn't satisfying for inner loop. Let's say input was: GxTxT$xx Sorted String: GTT$ Length of sorted string: 4 Outer loop goes from 0 - 3 So, in the initialization part i.e int j = i; When i = 2, j = 2 loop condition (j < s2.length()) doesn't satisfy which breaks the loop so all combinations of string aren't forming. Changed the lines in below code. bool find(string s1, string s2){ string s3; for(int i=0;i<s1.length();i++){ s3=""; for(int j=0;j<s2.length();j++){ s3+=s1[i+j]; cout << "s3: " << s3 << ", i: " << i << ", j: " << j << endl; } if(s3==s2){ return true; } } return false; } Edit: You can remove cout statement, I put it only for debugging.
3rd Jan 2020, 10:39 PM
blACk sh4d0w
blACk sh4d0w - avatar
0
why GxTxG$xx also fails? it outputs quiet, as supposed to. $T$ outputs ALARM, as supposed to.
3rd Jan 2020, 4:03 PM
Maksym Karpliuk
Maksym Karpliuk - avatar
0
and I think inner for loop in find() function is correct because s3 has to be the same length as s2(to check if they are equal)
3rd Jan 2020, 4:06 PM
Maksym Karpliuk
Maksym Karpliuk - avatar
0
You're reading from s1, so you should compare to it's length, not s2's. For example if the input is: "GTG
quot; and "TG
quot; You would never find the TG$ substr because it would never read the $ in s1.
3rd Jan 2020, 4:09 PM
Dennis
Dennis - avatar
0
Alexandr it returns a bool, not an int, so it never returns -1.
3rd Jan 2020, 4:12 PM
Dennis
Dennis - avatar
0
Probably, you also changed other parts of the code since I posted it which broke it more. Specifically at the point where you call the function.
3rd Jan 2020, 4:36 PM
Dennis
Dennis - avatar
0
no, i’ve changed nothing except if part
3rd Jan 2020, 4:37 PM
Maksym Karpliuk
Maksym Karpliuk - avatar
0
Didn't you change it to find(sorted, "$T") or find(sorted, "T
quot;) There is another case you're not handling I noticed, assuming the original code. TGG$ would print ALARM instead of quiet.
3rd Jan 2020, 4:53 PM
Dennis
Dennis - avatar
0
no, it calls quiet, i’ve checked
3rd Jan 2020, 4:55 PM
Maksym Karpliuk
Maksym Karpliuk - avatar
0
yes, i’ve changed if to find(sorted, "$T") or find(sorted, "T
quot;)
3rd Jan 2020, 4:56 PM
Maksym Karpliuk
Maksym Karpliuk - avatar
0
But the original code was if(find(sorted, "$GT") or find(sorted, "TG
quot;)){ return "quiet"; }else{ return "ALARM"; } I believe? The new code isn't even handling the G.
3rd Jan 2020, 4:57 PM
Dennis
Dennis - avatar
0
yes, but still what’s wrong? it checks if T “touches” G, i believe it’s enough cause i can’t find any case when it outputs wrong result
3rd Jan 2020, 4:59 PM
Maksym Karpliuk
Maksym Karpliuk - avatar
0
I tested it with the original code and only the last case fails. Which is most likely the case with the TGG$ ( and the mirrored version $GGT )
3rd Jan 2020, 5:02 PM
Dennis
Dennis - avatar
0
both of them output quiet, still no idea what’s wrong
3rd Jan 2020, 5:13 PM
Maksym Karpliuk
Maksym Karpliuk - avatar
0
Yes, it does in your new code, but I'm talking about your original code, it will print ALARM there. It will pass 1 through 5, but fail the 6th test because TG$ is not a substring of TGG$. Perhaps the find method is not going to work, might want to think of a new method?
3rd Jan 2020, 5:40 PM
Dennis
Dennis - avatar
0
well, my new code passes all test except 4th and I have no idea, why... do you have any idea what is a 4th test?
3rd Jan 2020, 6:24 PM
Maksym Karpliuk
Maksym Karpliuk - avatar