It's a sign - why does this solution NOT fail?! | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 15

It's a sign - why does this solution NOT fail?!

The description explicitly says that 4 strings are to be given, like this: CAT MONDAYS RACECAR TACOS And now look at my solution: words = [] for i in range(1): try: inp = input() if not inp: break except: break words.append(inp.strip()) print('Open' if any(word==word[::-1] for word in words) else 'Trash') Look at the range: The loop is executed just ONCE. The errorhandling would abort if something is wrong with the input. Now I should have only one word in the list. But I pass all the tests anyway. Anybody an idea what's going on here?

29th Dec 2019, 7:41 PM
HonFu
HonFu - avatar
24 Answers
+ 9
Could be an edge case that the first element in all the test cases reflect the expected answer of the entire group SL should fix the test cases if that's true The 3rd test case and above are hidden so can't verify this. But the first and second conform to the theory 😯
29th Dec 2019, 7:53 PM
Burey
Burey - avatar
+ 10
Waittttt a minute HonFu !!! This passes: word = input().replace("\r", "") print('Open' if word==word[::-1] else 'Trash') The input function sometimes adds \r for some reason My theory still stands 🥳
29th Dec 2019, 8:55 PM
Burey
Burey - avatar
+ 10
This is why we need randomized tests and not just 6 but 600 :D
30th Dec 2019, 12:52 PM
Schindlabua
Schindlabua - avatar
+ 6
I just printed out 4 inputs: They do give them. Only... well, what Burey found.
29th Dec 2019, 9:54 PM
HonFu
HonFu - avatar
+ 4
No need for blowing minds Saurabh Tiwari It's the immediate logical explanation Tho, more often than not in programming you can be surprised by the output, no matter how sure you are of the code 😅😅😅
29th Dec 2019, 8:06 PM
Burey
Burey - avatar
+ 4
HonFu nice That debunks my theory and shred it to pieces 😅😅
29th Dec 2019, 8:52 PM
Burey
Burey - avatar
+ 4
This passes all cases word = input().split() print('Open' if word[0]==word[0][::-1] else 'Trash') Burey's theory is still alive
29th Dec 2019, 9:34 PM
Coding Cat
Coding Cat - avatar
+ 3
Burey 🤯🤯🤯 that's some real problem with the test cases if that is true
29th Dec 2019, 8:03 PM
Saurabh Tiwari
Saurabh Tiwari - avatar
+ 3
✳AsterisK✳, why is it supposed to be a bad practice for tasks? Logically, when an error situation is created by something you can't control (like when bad input is given), exception handling is exactly what would be used. The tasks here generally give very limited information. Especially for C writers you are always left to guess: How large should I make my char arrays, how long will the input be? There's a lot of tweaking to be done. If you only do five or six tests, they should be good tests.
30th Dec 2019, 12:09 PM
HonFu
HonFu - avatar
+ 3
HonFu the thing is just that, the test cases will not go beyond what is said to be given, if the problem says write a program to sort 4 characters it ain't gonna be 5, that's kind of the rule, like they should not be a a constraint to guess if the input will be more than asked or less
30th Dec 2019, 12:14 PM
✳AsterisK✳
✳AsterisK✳ - avatar
+ 3
✳AsterisK✳, under normal circumstances you are right - but don't forget we're dealing with sloppy Sololearn here! Have you read what this question is about? They explicitly say 'take four inputs', and then they design the tests so randomly that you can pass even if you take only one input! That's not the only case we've encountered: In pig latin, people got failed tests because they removed punctuation. And yeah, right, they wrote in the description that we don't have to take care of that. But IF you care, you'll fail. Which can only mean that they input punctuation at least once, although that makes no sense in the context. Or look here: https://www.sololearn.com/discuss/2104980/?ref=app They demand you use ceil, but depending on how you write the code, you will fail with ceil and succeed with round. So yeah, in a situation where there are a sufficient number of test cases and they are all well-designed, you should be able to get by without error-handling. But this is not what we have here. ;-)
30th Dec 2019, 12:50 PM
HonFu
HonFu - avatar
+ 3
yeah, i understand your point HonFu
30th Dec 2019, 11:31 PM
✳AsterisK✳
✳AsterisK✳ - avatar
22nd May 2020, 9:54 AM
Nachiketa
Nachiketa - avatar
+ 3
I think the problem with test cases is that in some of the hidden cases the input (i.e. "sign text") is made up of two or more words. Thus, solutions using split() fail. The key is to take exactly 4 inputs. Of course this is particular to this problem and environment, where the SL editor takes inputs on several lines at once. This one-liner works: print('Open' if any(w == w[::-1] for w in [input(), input(), input(), input()]) else 'Trash')
20th Dec 2021, 4:27 PM
TensorFlow23
TensorFlow23 - avatar
+ 2
HonFu Burey No, if you use word=input() it is not only the first word in word. You are reversing the whole input. This is not comperable.
29th Dec 2019, 9:26 PM
Coding Cat
Coding Cat - avatar
+ 2
Should they seriously have designed their tests that sloppily? I mean, that's almost random. Or did they reconsider to one input and forgot to change the text?
29th Dec 2019, 9:49 PM
HonFu
HonFu - avatar
+ 2
Nobody knows. It's like real live 😂
29th Dec 2019, 9:51 PM
Coding Cat
Coding Cat - avatar
+ 2
HonFu i heard and i noticed that using an exception is a bad practice for the challenge and even in codewars its been given in description not to use exception handling, that might be a problem too
30th Dec 2019, 11:58 AM
✳AsterisK✳
✳AsterisK✳ - avatar
+ 1
Burey, I just tried this: word = input() print('Open' if word==word[::-1] else 'Trash') If the only reason was that always the first input makes the difference, shouldn't I succeed with this version as well? It fails some tests though.
29th Dec 2019, 8:49 PM
HonFu
HonFu - avatar
+ 1
Just FYI: _________________ _________________ _________________ https://code.sololearn.com/cIWXP6Vv67ts/#py
28th Sep 2021, 11:57 PM
_] | [\]
_] | [\] - avatar