Mirror stamp tickets
EasyOnline Assessment
Find out how many stamps it takes to turn a fairground ticket into a "mirror" ticket.
A ticket carries a positive whole number n. A number is a mirror number if it reads the same forwards and backwards in ordinary decimal digits (7, 4884 and 1001 are mirror numbers; 120 is not).
The booth attendant works like this: if the ticket's current number is a mirror number, she is done. Otherwise she stamps it, which replaces the number by *the number plus its reversal*. The reversal of a number is its digits written backwards, read as a normal number, so leading zeros of the reversal simply disappear (the reversal of 120 is 21). She stamps at most max_stamps times, and the number is checked again after every stamp, including the last one.
Input: n, a positive integer, and max_stamps, a non-negative integer.
Output: a list [stamps, value]: how many stamps were used and the final number on the ticket. If the number is still not a mirror number after max_stamps stamps, return [-1, value], where value is the number on the ticket after those max_stamps stamps.
Example 1
- Input:
- [87,10]
- Output:
- [4,4884]
- Explanation
- 87 is not a mirror number: 87 + 78 = 165, then 165 + 561 = 726, 726 + 627 = 1353, 1353 + 3531 = 4884. That is a mirror number after 4 stamps.
Example 2
- Input:
- [120,5]
- Output:
- [1,141]
- Explanation
- The reversal of 120 is 21 (the leading zero of
021is dropped), so one stamp gives 120 + 21 = 141, a mirror number.
Example 3
- Input:
- [196,4]
- Output:
- [-1,13783]
- Explanation
- 196 is not a mirror number and keeps growing: 196, 887, 1675, 7436, 13783. After the 4 allowed stamps it is still not a mirror number, so the stamp count is -1 and the value is 13783.
Constraints
1 <= n <= 1,000,0000 <= max_stamps <= 12