Today the coding calendar Advent of Code started.
You should try it too, Advent of Code.
I tried to solve the first challenge. It was pretty straight forward.
Except, I was stumped by a simple problem.
Here is my first attempt,
|
|
The thing is it gives correct result for the test data. So, I assumed it would work for the input data. But, the program always
gave me 1 less than the actual answer. I couldn’t figure out why.
Notice, I use string comparison in the if condition on line 5.
I knew using string for comparison of numbers was a bad idea but, the following IDLE experiments convinced me otherwise.
|
|
If you didn’t know (I certainly didn’t) you can compare strings with the >
and <
operators in python.
This led me to believe that comparing numbers would work without explicitly using integers.
|
|
Here is the problem. String comparison compares the first character of the strings
and the tries to distinguish which is larger.
Here, even though 1024 is greater than 946, 9 is greater than 1!
The string comparison operator doesn’t know better.
I could’ve casted them to integers or zfill
ed them to have same number of digits to solve the problem.
And I did, the program now works!
|
|
So, the takeaway is, use numbers(the datatype) when dealing with numbers.