id/email
password
forgot password | create account
about | help | code help+videos | done | prefs
CodingBat code practice

 

peter@norvig.com > palindromic_odometer
prev  |  next  |  chance

This was CarTalk Puzzler #200803: RAY: This came in from Terry Sare and I didn't even have to change it. Terry writes, "I was driving on the highway the other day recently and I happened to notice my odometer. Like most odometers nowadays, it shows six digits, in whole miles only -- no tenths of a mile. So, if my car had 300,000 miles, for example, I'd see 3-0-0-0-0-0. And that's all. Until I drove another mile, at which point it would read 3-0-0-0-0-1. "Now, what I saw that day was very interesting. I noticed that the last 4 digits were palindromic, that is they read the same forwards as backwards. For example, '5-4-4-5' is a palindrome. So, my odometer could have read 3-1-5-4-4-5, with those last four digits, starting with the units, then the tens, then the hundreds, and finally the thousands, being the palindrome. "One mile later, the last 5 numbers were palindromic. For example, it could have read 3-6-5-4-5-6. "One mile after that, the middle 4 out of 6 numbers were palindromic. So, the first and last numbers weren't involved in the palindrome, but the middle 4 were palindromic. "And you ready for this? One mile later, all 6 were palindromic! For example, 2-1-3-3-1-2. " Pretty good, huh? I thought so. So, here again are the conditions as stated by Terry. "I noticed that the last 4 digits were palindromic. I drove a mile, and the last 5 were palindromic. I drove another mile and the middle 4 were palindromic, and the ends were not involved. And then one mile later, all 6 digits were palindromic." The question is, what did Terry see on the odometer when he first looked? (NOTE: It is hard to translate a Puzzler like this into CodingBat, because there would be only one test case, and that would give away the answer. So instead, we will ask you to solve a part of the problem: define "palindromic_odometer(m)" to return True if the mileage m meets the conditions of the problem. There will be one test with the correct answer, but you won't get to see it because it will be in the "other tests" category.)


palindromic_odometer(100000) → False
palindromic_odometer(101221) → False
palindromic_odometer(222222) → False

...Save, Compile, Run (ctrl-enter)

def palindromic_odometer(m):

Editor font size %:
Shorter output


Forget It! -- delete my code for this problem

Progress graphs:
 Your progress graph for this problem
 Random user progress graph for this problem
 Random Epic Progress Graph

Python Help

Difficulty: 321 Post-solution available

Copyright Nick Parlante 2017 - privacy