您在這裡

Exercises

4 九月, 2015 - 14:38

Exercise 9.7.This question is based on a Puzzler that was broadcast on the radio program Car Talk (http://www.cartaLk.com/content/puzzLers ):

Give me a word with three consecutive double letters. Ill give you a couple of words that almost qualify, but dont. For example, the word committee, c-o-m-m-i-t-t-e-e. It would be great except for the i that sneaks in there. Or Mississippi: M-i-s-s-i-s-s-ip-p-i. If you could take out those is it would work. But there is a word that has three consecutive pairs of letters and to the best of my knowledge this may be the only word. Of course there are probably 500 more but I can only think of one. What is the word?

Write a program to nd it. Solution: http://thinkpython.com/code/cartaLkl.py.

Exercise 9.8.Heres another Car Talk Puzzler (http://www.cartalk.com/content/puzzlers):

I was driving on the highway the other day and I happened to notice my odometer. Like most odometers, it shows six digits, in whole miles only. So, if my car had 300,000 miles, for example, Id see 3-0-0-0-0-0.

Now, what I saw that day was very interesting. I noticed that the last 4 digits were palindromic; that is, they read the same forward as backward. For example, 5-4-4-5 is a palindrome, so my odometer could have read 3-1-5-4-4-5.

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. And you ready for this? One mile later, all 6 were palindromic!

The question is, what was on the odometer when I rst looked?

Write a Python program that tests all the six-digit numbers and prints any numbers that satisfy these requirements. Solution: http://thinkpython.com/code/cartaLk2.py.

Exercise 9.9.Heres another Car Talk Puzzler you can solve with a search (http://www.cartaLk.com/content/puzzLers ):

Recently I had a visit with my mom and we realized that the two digits that make up my age when reversed resulted in her age. For example, if shes 73, Im 37. We wondered how often this has happened over the years but we got sidetracked with other topics and we never came up with an answer.

When I got home I gured out that the digits of our ages have been reversible six times so far. I also gured out that if were lucky it would happen again in a few years, and if were really lucky it would happen one more time after that. In other words, it would have happened 8 times over all. So the question is, how old am I now?

Write a Python program that searches for solutions to this Puzzler. Hint: you might nd the string method zfill useful.

Solution: http://thinkpython.com/code/cartaLk3.py.