12.07.2015 Views

Think Python - Denison University

Think Python - Denison University

Think Python - Denison University

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

68 Chapter 7. IterationBut if you were “lazy,” you probably cheated by learning a few tricks. For example, to find theproduct of n and 9, you can write n−1 as the first digit and 10−n as the second digit. This trick isageneral solutionfor multiplying any single-digit number by9. That’s analgorithm!Similarly, the techniques you learned for addition with carrying, subtraction with borrowing, andlong division are all algorithms. One of the characteristics of algorithms is that they do not requireany intelligence to carry out. They are mechanical processes in which each step follows from thelastaccording toasimplesetof rules.In my opinion, it is embarrassing that humans spend so much time in school learning to executealgorithms that, quite literally,require nointelligence.Ontheotherhand,theprocessofdesigningalgorithmsisinteresting,intellectuallychallenging,andacentral part of what we call programming.Some of the things that people do naturally, without difficulty or conscious thought, are the hardestto express algorithmically. Understanding natural language is a good example. We all do it, but sofarno one has been able toexplain how we doit,at least not inthe formof an algorithm.7.7 DebuggingAsyoustartwritingbiggerprograms,youmightfindyourselfspendingmoretimedebugging. Morecode means morechances tomake anerror and more place forbugs tohide.Onewaytocutyourdebuggingtimeis“debuggingbybisection.” Forexample,ifthereare100linesinyour program and you check them one at atime, itwould take 100 steps.Instead, try to break the problem in half. Look at the middle of the program, or near it, for anintermediate value you can check. Add a print statement (or something else that has a verifiableeffect) and run theprogram.If the mid-point check is incorrect, there must be a problem in the first half of the program. If it iscorrect, the problem isinthesecond half.Every time you perform a check like this, you halve the number of lines you have to search. Aftersixsteps(whichisfewerthan100),youwouldbedowntooneortwolinesofcode,atleastintheory.In practice it is not always clear what the “middle of the program” is and not always possible tocheck it. It doesn’t make sense to count lines and find the exact midpoint. Instead, think aboutplaces in the program where there might be errors and places where it is easy to put a check. Thenchoose a spot where you think the chances are about the same that the bug is before or after thecheck.7.8 Glossarymultipleassignment: Makingmorethanoneassignmenttothesamevariableduringtheexecutionof aprogram.update: An assignment where thenew value of thevariable depends on theold.initialization: Anassignment that gives an initialvalue toavariable that willbe updated.increment: An update that increases thevalue of avariable (oftenby one).

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

Saved successfully!

Ooh no, something went wrong!