The lucky ticket
- O(log n) · Easy
- Free
- Python
- JavaScript
- digits
- booleans
- loops
Problem
On bus route 14, some riders check the serial number on their ticket before throwing it away. If the number reads the same forward and backward, they say it brings luck, and the ticket goes in the wallet. Write a function that takes the serial number, a whole number 0 or greater, and returns true if it reads the same both ways and false if not. 12321 backward is 12321: you return true. 1231 backward is 1321: false.
A single-digit number, 0 included, always reads the same. And zeros at the end count: 120 backward would be 021, which is not 120, so it does not read the same.
Examples
Same both ways, odd length
12321 → true
Starts and ends alike, but does not match
1231 → false
Same both ways, even length
4554 → true
A single digit
7 → true
Without its zero it would match
1210 → false
Besides these, the challenge has hidden tests that are revealed when you submit your solution.
You start with this
Python
def reads_same(serial):
passJavaScript
function readsSame(serial) {
}It opens in your browser, with the editor and the tests. It is free and you do not need an account to start.