12.07.2015 Views

Think Python - Denison University

Think Python - Denison University

Think Python - Denison University

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

86 Chapter 9. Casestudy: word play9.6 Glossaryfileobject: A value that represents anopen file.problem recognition: A way of solving a problem by expressing it as an instance of a previouslysolvedproblem.special case: A testcase that isatypical or non-obvious (and lesslikelytobe handled correctly).9.7 ExercisesExercise9.7 ThisquestionisbasedonaPuzzlerthatwasbroadcastontheradioprogramCarTalk 2 :Give me a word with three consecutive double letters. I’ll give you a couple of wordsthat almost qualify, but don’t. For example, the word committee, c-o-m-m-i-t-t-e-e. Itwould 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 i’s it would work. But there is a word that has threeconsecutivepairsoflettersandtothebestofmyknowledgethismaybetheonlyword.Of course thereare probably 500 morebut Ican only think of one. What isthe word?Writeaprogram tofind it. You can see mysolution atthinkpython.com/code/cartalk.py.Exercise 9.8 Here’s another Car Talk Puzzler 3 :“I was driving on the highway the other day and I happened to notice my odometer.Likemostodometers,itshowssixdigits,inwholemilesonly. So,ifmycarhad300,000miles, forexample, I’dsee 3-0-0-0-0-0.“Now, what I saw that day was very interesting. I noticed that the last 4 digits werepalindromic; that is, they read the same forward as backward. For example, 5-4-4-5 isa palindrome, somyodometer could have read3-1-5-4-4-5.“One mile later, the last 5 numbers were palindromic. For example, it could have read3-6-5-4-5-6. Onemileafterthat,themiddle4outof6numberswerepalindromic. Andyou ready forthis? One milelater, all6were palindromic!“The question is,what was onthe odometer when Ifirstlooked?”Writea<strong>Python</strong>programthattestsallthesix-digitnumbersandprintsanynumbersthatsatisfytheserequirements. You can seemysolution atthinkpython.com/code/cartalk.py.Exercise 9.9 Here’s another Car Talk Puzzler you can solve withasearch 4 :“Recently I had a visit with my mom and we realized that the two digits that makeup my age when reversed resulted in her age. For example, if she’s 73, I’m 37. Wewonderedhowoftenthishashappenedovertheyearsbutwegotsidetrackedwithothertopics and wenever came upwithan answer.“WhenIgothomeIfiguredoutthatthedigitsofourageshavebeenreversiblesixtimesso far. I also figured out that if we’re lucky it would happen again in a few years, andifwe’rereallyluckyitwouldhappenonemoretimeafterthat. Inotherwords,itwouldhave happened 8times over all. So thequestion is,how old am I now?”2 www.cartalk.com/content/puzzler/transcripts/200725.3 www.cartalk.com/content/puzzler/transcripts/200803.4 www.cartalk.com/content/puzzler/transcripts/200813

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!