ebook img

Ten Essays on Fizz Buzz: Meditations on Python, Mathematics, Science, Engineering, and Design PDF

160 Pages·2019·1.409 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Ten Essays on Fizz Buzz: Meditations on Python, Mathematics, Science, Engineering, and Design

Ten Essays on Fizz Buzz Meditations on Python, mathematics, science, engineering, and design Joel Grus Thisbookisforsaleathttp://leanpub.com/fizzbuzz Thisversionwaspublishedon2020-07-06 ISBN978-0-9824818-2-0 ThisisaLeanpubbook.LeanpubempowersauthorsandpublisherswiththeLean Publishingprocess.LeanPublishingistheactofpublishinganin-progressebook usinglightweighttoolsandmanyiterationstogetreaderfeedback,pivotuntilyou havetherightbookandbuildtractiononceyoudo. ©2020JoelGrus Contents Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . i 1. 100PrintStatements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 AlgorithmandAbstraction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 ReusabilityandChangeability . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 TestabilityandFizzBuzz . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 TestabilityBeyondFizzBuzz . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 GeneratingtheprintStatements . . . . . . . . . . . . . . . . . . . . . . . . . 10 2. if/elif/elif/else . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 MindtheOrder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 DivisibilityandModulus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 “We’reAdders,WeNeedLogstoMultiply” . . . . . . . . . . . . . . . . . . . 18 SomeOtherChecks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 SomeWell-KnownTricks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 Eliminatingelif . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 3. TheCycleof15 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 EquivalenceClasses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 NoneasaSentinel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 TruthinessandLogic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 dict.get . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 4. Euclid’sSolution. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 PrimeNumbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 PerformanceOptimization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 Factorization. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 WhatPrimeFactorizationHastoDowithFizzBuzz . . . . . . . . . . . . . 42 CONTENTS GreatestCommonDivisorsandLeastCommonMultiples . . . . . . . . . . 43 gcdandFizzBuzz . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 Euclid’sAlgorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 5. Trigonometry . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 SinandCos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 int . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 PiandTau . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 OperatorOverload . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 6. ABigNumber . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 Decimal,Binary,andHexadecimal . . . . . . . . . . . . . . . . . . . . . . . . 58 ASecretEncoding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 PrefixCodes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 HuffmanCoding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 DataModeling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 OurDataModel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 ConstructingtheCode. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 Digression:MutableDefaultValues . . . . . . . . . . . . . . . . . . . . . . . . 73 Digression:heapq . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 Decoding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 “AccidentallyQuadratic” . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 RegularExpressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 7. InfiniteIterables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 Iterables,Iterators,andLazyInfiniteSequences . . . . . . . . . . . . . . . . 84 itertools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 zipvsmap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 PuttingItAllTogether . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 Bonus:PowerFizz . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 8. RandomGuessing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 random . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 PseudorandomnessandtheSeed . . . . . . . . . . . . . . . . . . . . . . . . . 99 CreatingaPseudorandomGenerator . . . . . . . . . . . . . . . . . . . . . . . 100 AdvancesintheSameIterator . . . . . . . . . . . . . . . . . . . . . . . . . . . 104 ValuesandReferences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 CONTENTS ArgumentUnpacking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106 TheCycleof15,Redux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 Wheredid23_977_775comefrom? . . . . . . . . . . . . . . . . . . . . . . . . 109 Puttingitalltogether . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 9. MatrixMultiplication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 n-DimensionalArrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 Digression:Recursion,Iteration,andTailRecursion . . . . . . . . . . . . . . 115 NumPy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118 WorkingWithArrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 FizzBuzzasaMatrixMultiplicationProblem. . . . . . . . . . . . . . . . . . 123 HowDidIChooseThoseWeights? . . . . . . . . . . . . . . . . . . . . . . . . 125 10. FizzBuzzinTensorflow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 MachineLearning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 FizzBuzzasaMachineLearningProblem . . . . . . . . . . . . . . . . . . . . 128 LearningandGeneralization . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133 FeatureEngineering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133 DeepLearning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138 Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140 HowDoesItWork? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140 AbouttheAuthor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152 Acknowledgements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153 Introduction FizzBuzzisthefollowing(simple)problem: Print the numbers from 1 to 100, except that if the number is divisible by 3,insteadprint“fizz”;ifthenumberisdivisibleby5,insteadprint“buzz”; andifthenumberisdivisibleby15,insteadprint“fizzbuzz”. It originated as a children’s game, but has since taken on a new life as a lowest- common-denominatorlitmustestforassessingcomputerprogrammers. If you are an experienced programmer, it is an extremely easy problem to solve. Becauseofthis,ithastakenonathirdlifeastheprototypicalbad interviewproblem. Everyone knows that it’s the question you ask people to make sure that they’re not completelyincompetentas programmers. Accordingly,if yourinterviewerasks you to solve it, he’s suggesting he thinks it possible that you’re completely incompetent asaprogrammer.Youwouldnotbewrongtofeelinsulted! Myassociationwiththisproblembeganin2016,whenIwroteablogpostcalledFizz Buzz in Tensorflow, the (possibly fictional) story of one such insulted programmer whodecidedtoshowuphisinterviewerbyapproachingFizzBuzzasadeeplearning problem. This post went modestly viral, and ever since then I have been seen as a thoughtleaderintheFizzBuzzspace. Accordingly,overtheyearsIhavecomeupwithand/orcollectedvariousotherstupid and/orcleverwaysofsolvingFizzBuzz.Ihavenotbloggedaboutthem,asIamnot the sort of person who beats a joke to death, but occasionally I will tweet about them,andrecentlyinresponsesomeonesuggestedthatIwriteabookon“100Ways ofWritingFizzBuzzinPython.” Now,Icouldprobablycomeupwith100waysofsolvingFizzBuzz,butmostofthem would not be very interesting. Luckily for you, I was able to come up with 10 that areinterestinginvariousways,eachofwhichturnedouttobeagoodlaunching-off point for (sometimes meandering) discussions of various aspects of coding, Python, Introduction ii Fizz Buzz, mathematics, software design, technical interviewing, and various other topics.Hence“TenEssaysonFizzBuzz”.¹ In many ways this is a strange book. Its goal is not to teach you a specific field or a specific technology. I hope you will learn a lot from reading it, but it’s not really a book that you’d read in order to learn anything in particular. Most technical books areaboutspecifictechnicaltopics;thisonesortofisn’t. Nonetheless, it is a technical book. Each essay contains code that implements a different solution of Fizz Buzz. Each essay uses code to illustrate its ideas. Each essay represents my current best thinking about how to solve problems using code. Ifyouhaveacodingjob,youshouldfeelnoreluctancetoexpensethisbooktoyour employer. AsIprimarilycodeinPython,allofthesesolutionswillbeinPython.Someofthem will use features that were only introduced in Python 3.6, and some of them will demonstrate concepts and techniques that are mostly specific to Python. On some levelthisisdeeplyaPythonbook.However,mygoalwastowriteabookthatwould be interesting and enlightening even if you are not a Python programmer. I’ll leave ittoyoutojudgehowsuccessfulIwas. IfIhaveexploredmorewaysofsolvingFizzBuzzthanothers,itisbecauseIstoodon theshouldersofgiants.The“if/elif/elif/else”solutionisthecanonicalone;the“100 printstatements”solutionisobvious;the“randomguessing”solutionIlearnedabout from a Stack Overflow question; the remaining solutions I’m pretty sure I came up withmyself,althoughformanyofthemitisanearcertaintythatotherpeoplehave independentlycomeupwithsimilar(ifnotbasicallyidentical)solutions.That’show programmingworks. Thesolutionsfromthisbookareallavailableatgithub.com/joelgrus/fizzbuzz,incase youdon’tfeelliketypingthemintothecomputeryourself. ¹AfterIstartedwritingthisbookIdiscoveredthereisablogpost“TwentyWaystoFizzBuzz”,veryfewofwhich overlapwiththese. 1. 100 Print Statements FizzBuzzoriginatedasagameforchildren.Theideawasthatthechildrenwouldsit inacircleandgoaroundinsequencecallingoutthenextnumber;substituting“fizz” or “buzz” or “fizzbuzz” as appropriate; punishing mistakes according to house rules withvaryinglevelsofcruelty. Well, if children can come up with the correct outputs without using a computer, thensocanwe.Thissuggestswhatispossiblytheleastimaginativesolution–figure outthecorrectoutputsbyhandandexplicitlyprinteachone: print('1') print('2') print('fizz') print('4') print('buzz') print('fizz') print('7') print('8') print('fizz') print('buzz') print('11') print('fizz') print('13') print('14') print('fizzbuzz') print('16') print('17') print('fizz') print('19') print('buzz') print('fizz') 100PrintStatements 2 print('22') print('23') print('fizz') print('buzz') print('26') print('fizz') print('28') print('29') print('fizzbuzz') print('31') print('32') print('fizz') print('34') print('buzz') print('fizz') print('37') print('38') print('fizz') print('buzz') print('41') print('fizz') print('43') print('44') print('fizzbuzz') print('46') print('47') print('fizz') print('49') print('buzz') print('fizz') print('52') print('53') print('fizz') print('buzz') print('56') print('fizz') 100PrintStatements 3 print('58') print('59') print('fizzbuzz') print('61') print('62') print('fizz') print('64') print('buzz') print('fizz') print('67') print('68') print('fizz') print('buzz') print('71') print('fizz') print('73') print('74') print('fizzbuzz') print('76') print('77') print('fizz') print('79') print('buzz') print('fizz') print('82') print('83') print('fizz') print('buzz') print('86') print('fizz') print('88') print('89') print('fizzbuzz') print('91') print('92') print('fizz')

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.