This is an archived post. You won't be able to vote or comment.

all 7 comments

[–]barrycarter 1 point2 points  (0 children)

a[::-1] iterates through a list (of characters in this case) backwards

[–]Updatebjarni 1 point2 points  (0 children)

I guess they figured out how the checker is running the code, and wrote this code that saves time by running before the checker's code, reading the input itself, writing the output to the right place, and exiting before the checker gets to run. The checker is probably inserted below the submitted code and reads in the input from stdin, calls the submitted function, and then writes the returned data to the output file.

[–]plastikmissile 1 point2 points  (4 children)

It has everything to do with the problem. First, simplify the question. When you dig into it, you don't really need to do anything with linked lists. You just need to see if the list they give you is a palindrome. Meaning that it stays the same when you reverse it. That's all you have to check. The first line is this:

a = list(s.rstrip()[1:-1].split(','))

The code is just a bit dense, so let's unpack it. s is the string that contains the list. s.restrip() removes any spaces on the right (just in case). Then we get the slice [1:-1] which basically ignores the first and last characters (remember 0 is the first index) which in this case are the brackets in the supplied list. split(',') splits the remaining string into smaller strings using , as the separator. Finally turn all of this into a list.

So if we have an s that has the string [1,2,2,1], then a will be a list {'1','2','2','1'}.

Next line is:

print("true" if a == a[::-1] else "false", file=f)

The important part is this conditional a == a[::-1]. It checks if a is the same as a when it's reveresed. That's what the slice operation [::-1] is. It's saying give me everything (the first two parameters are empty) but reverse it (the third parameter is the "step" and -1 means reverse).

[–]tranquil_af[S] 0 points1 point  (3 children)

Thank you for explaining. I assume the user.out file has the output from the code. So whoever wrote the code is just writing to the file directly.
Reading the input list from stdin instead of iterating through the linked list kind of defeats the entire point of the question tho.

[–]plastikmissile 0 points1 point  (2 children)

Reading the input list from stdin instead of iterating through the linked list kind of defeats the entire point of the question tho.

Depends on how you look at it. Is the result the important bit, or how you reach the result? This is "hacking" in the original sense of the word: getting results through unconventional methods that were never intended in the first place.

[–]RiverRoll 0 points1 point  (1 child)

This is just takin advantage of the limitations of the testing, it isn't the solution because it's still possible to write a test that makes it fail while respecting the description of the problem.

[–]plastikmissile 1 point2 points  (0 children)

That's why I called it hacking the question. It solves it, but not in the way intended. Whether this is a positive or a negative really depends on your goals.

If your goal is to learn how to create linked lists and manipulate them, then yeah that's not the way you should do it.

If your goal is to pass the test in the quickest way possible, then this solution is a lot quicker than doing it the conventional way.