Index

Symbols and Numbers Benfording the Battlegrounds project, 366 == (comparison) operator, 36–37 Benford’s law, 348–363 % (modulo) operator, 78, 255–256 bigrams, 52 * (splat) operator, 25 Birthday Paradox problem, 238 2of4brif.txt file, 20 The Black Swan: The Impact of the Highly 4 Percent Rule, 239–240, 263 Improbable, 2nd Edition (Taleb), 263 A black swans, 245 blitting and blit() method, 280, 317 absolute paths, 329 block transfer, 280 abstractions, 21 block-level objects, 111 acceleration, 286 British brute-force , 54–55 actions, 223 brute-force method, 40, 54–55 actuarial life tables, 245 Building a Galactic Empire project, Adding Habitable Zones to Your 213–214, 380–382 Galaxy project, 215–216 Building a Rat Harem project, 144 aerobraking, 321–322 Alexander, Edward Porter, 87 All the Marbles problem, 264 Anaconda, 195 calculators, 240 anagrams, defined, 35 candidates, 175 ancestors, 229, 272 Canvas widget, 203 apoapsis, 290 Carnegie Mellon University apogee, 290 Pronouncing Dictionary application programming interfaces (CMUdict), 148–153 (), 267 case sensitive letters, 39 artists, 360 CCDs (charge-coupled devices), 325 assets, 296 CCDStack, 331 attributes, 225 challenge projects Audacity, 296 Adding Habitable Zones to Your Automatic Anagram Generator Galaxy, 215–216 project, 62 All the Marbles, 264 Automating Possible Keys project, 88, Automatic Anagram Generator, 62 371–372 Benfording the Battlegrounds, 366 Building a Rat Harem, 144 B With a Bullet, 284 Creating a Barred-Spiral Galaxy, Babbage, Charles, 107 214–215 bar_chart() function, 355, 359, 358–360 Creating a More Efficient Beating Benford project, 364–365, Safecracker, 144 387–388 The Fountainhead, 283 Belyayev, Dmitry, 134 To , or Not to Haiku, 186 Benford, Frank, 347 Just My Luck! 264 continue statements, 178 Markov Music, 186 control messages, 67 Mars Orbiter game modifications, corpus, 148 320–323 count_first_digits() function, 356 The Middle, 17 Counter module, 40–41, 56–57, 197 Mix and Match, 264 counting syllables, 147–159 New Word Generator, 184 cProfile, 20, 30 A Picture Is Worth a Thousand Cracking Codes with Python (Sweigart), Dollars, 264 61, 87, 119 Poor Foreign Man’s Bar Chart, Creating a Barred-Spiral Galaxy 16 –17 project, 214–215 Recursive Approach, 34 Creating a More Efficient Safecracker Route Encoder, 90 project, 144 Shock Canopy, 283 crossover , 128, 133 Three-, 90 , 51 Turing Test, 185 current working directory (cwd), 328 Unbelievable! This Is Unbelievable! c-v maps (consonant-vowel maps), 52–58 Unbelievable! 185–186 c v_ m a p _filte () function, 57 Using Monospace Font, 123 cv_words() function, 58 Vanishing Act, 344–345 While No One Was Looking, 366 D charge-coupled devices (CCDs), 325 check_keys() method, 301 data Checking the Number of Blank Lines loading, 355–356 project, 122–123, 377–378 object-oriented programming chi-square goodness-of-fit test, 352–353 (OOP) and, 223 Churchill, Winston, 3 data verfication, 77–78 cipherlist, 78 debugging, 169–171 , 78 Deep Sky Stacker, 331 Clark, Brooks, 330 defaultdict container type, 172–173, Class attributes, 269–270 355–357 classes, 223, 229, 298–299 default_input() function, 251–252 clea n _f old e r() function, 333 del_folders() function, 333 Clinton, Hillary, 353–354 designed development, 2 CMUdict (Carnegie Mellon University Dictionary Cleanup project, 33, 368–369 Pronouncing Dictionary), dictionary files, 20–22, 160 148–153 digrams, 52–54, 72 The Code Book: The Science of Secrecy from directionality, 211 Ancient Egypt to Quantum directory paths, 328–329 Cryptography (Singh), 87, 101 DirectX API, 267 coding conventions, 6 docstrings, 10–11 coding style, xxv Downey, Allen, 2 Colchester Catch project, 103, 376 Drake equation, 187, 190–191 collections module, 40 draw() method, 280 color tables, 297–298 duration variable, 254 command window, 7 DVDVideoSoft, 330 columnspan, 231 comparison operator (==), 36–37 E modeling, 194 computer performance evaluation, 162 eccentricity, 301 consonant-vowel maps (c-v maps), 52–58 eccentricity variable, 308–309, 314, 316 constant of proportionality, 307 Effective Python (Slatkin), 15

390 Index electronic ink, 108 game sketches, 268–269, 293–295 Elementary (television show), 105 , xxiv Emerson, Ralph Waldo, xxii generations, 128–129 encapsulations, 21 genetic algorithms, 125–142 enjambment, 167 Genetic Algorithms with Python enter , 5 (Sheppard), 143 Enthought Canopy, 195 get_expected_counts() function, 357 enumerate() function, 134 getdata() method, 339 epoch timestamps, 31 global minima and maxima, 140 errors. See also debugging global scope, 42 checking for, 253 Going the Distance project, 282, false, 8 385–387 events, 278 goodness-of-fit test, 352–353, 357–358 evolutionary algorithms, 125 Google exceptions, 21 PageRank algorithm, 162 exhaustive search engines, 126 style guide, 10–11, 14 expected counts, 357 graphical models, 199–201 graphical (GUI), 74 F gravity, 286–287, 307 groups, 314 Fermi’s paradox, 187–211 Guns of the South (Turtledove), 66 files and folders, 327–329, 333–334 Finding Digrams project, 61, 369 first digits, 356–357 H first-digit law. See Benford’s law Hacking Lincoln project, 87, 370 Flake8, 7 haiku, 146 –147 flipping and flip() method, 280 To Haiku, or Not to Haiku project, 186 font objects, 111 handwriting recognition, 162 fonts Hartman, Charles, 145–146, 167, color, 108 182, 184 types of, 109–110 Highlighter tool, 108 using, 119 hill-climbing algorithm, 139 Fooled by Randomness: The Hidden Role Hitchhiker’s Guide to Python, 15 of Chance in Life and in the Hohmann transfer orbit, 291 Markets, Revised Edition human error, 66 (Taleb), 263 for loops, 257 I Fountainhead project, 283 4 Percent Rule, 239–240, 263 Identifying Cipher Types project, 88, Frame class, 229 370–371 frames per second (fps), 278–280 IDLE , 3 Frame widget, 229 IDLE text editor, xxiv Free Studio, 330 image registration, 331 Free Video to JPG Converter tool, 330 image stacking, 325 Friedman, William, 65 images, 334–337 functions, defining, 250, 309–310 import statements, 250 inline-level objects, 111 instantiation, 314 G integrated development environment A Galaxy Far, Far Away project, 212–213, (IDE), xxiv 379–380 invisible electronic ink, 108 game assets, 227 ipadx option, 231 game loops, 299–300, 315–316 itertools module, 51

Index 391 J Microsoft DirectX API, 267 Japanese Haiku (Beilenson), 146 Office Suite, 107 JavaScript Object Notation (json), 152 Outlook, 121 Jumble, 36, 61 The Middle project, 15–16, 17 Just My Luck! problem, 264 Milky Way, 188–189 Mix and Match problem, 264 K model of order 0, 162 model of order 2, 162–163 Kepler, Johann, 287 modules, xxi, 21 kerning, 109–110 importing, 250, 355–356 KEYUP event, 316 matplotlib, 195–196, 198, 248, keys, 172 250, 355 pillow, 326–327 L pydocstyle, 11, 15 pygame, 267, 281–282, 296–319 .lower() method, 6 Lanczos filter, 337 Pylint, 7–13, 15 python-docx, 110–111 letter_pair_filter() function, 58–59 letter-transposition , 72 , 200–202, 212, 229–231 LibreOffice Writer, 107 modulo operator (%), 78, 255–256 line breaks, 110 monospace fonts, 109–110 list ciphers, 99–101 Monte Carlo simulation (MCS), 218–238 list comprehension, 22, 71 most_common() method, 56–57 local minima and maxima, 140 Mysterious Messages: A History of Codes logarithmic scale, 349 and Ciphers (Blackwood), logarithmic spiral, 199 87, 101 , 284 logging module, 170 –171 Myth Busters logic, 223 loops N game loops, 299–300, 315–316 natural language processing (NLP), 147 for loops, 257 Natural Language Processing with Python: while loops, 5, 135, 254, 318 Analyzing Text with the Natural lower case, displaying characters as, 39 Language Toolkit (Bird, Klein and Loper), 155–156 M Natural Language Toolkit (NLTK), machine translations, 147 148–149 New Word Generator project, 184 main() function, 42, 46–48, 60–61, 76–79, 258–259, 277–278, Newcomb, Simon, 347 278–280, 312–313, 361–363 Newton, Isaac, 286–287 Markov chain analysis, 147, 161–182 Nigrini, Mark, 364 Markov Music project, 186 null, defined, 92 Mars Climate Orbiter, 6 null ciphers Mars Orbiter game modifications defined, 91–92 project, 320–323 writing, 98–101 Mary (queen of Scotland), 91 NumPy docstring standards, 10 NumPy library, 195–196, 198 matplotlib library, 195–196, 198, 248, 250, 355 MCS (Monte Carlo simulation), 218–238 O median, 344 object-oriented programming (OOP), methods, 223, 225 223, 267 microphotography, 80

392 Index Ogg Vorbis format (.ogg), 296, 300 Poor Foreign Man’s Bar Chart project, O’Neill, Tip, 366 16 –17 One-Tangent Burn, 291 Poor Man’s Bar Chart project, OpenGL (Open Graphics Library), 267 15–16, 368 OpenOffice Writer, 107 pop() function, 71, 86, 155 OpenSource Computer Vision PowerShell, 7 (OpenCV), 343 practice projects operating systems, xxiv Automating Possible Keys, 88, optimization, 126, 139 371–372 orbital mechanics, 288–292 Beating Benford, 364–365, 387–388 os.chdir() method, 328–329 The Birthday Paradox, 238 os.getcwd() method, 328–329 Building a Galactic Empire, os.listdir() method, 333 213–214, 380–382 os.path.abspath() method, 329 Building a Rat Harem, 144 os.path.isdir() method, 333 Checking the Number of Blank os.path.join() method, 328–329 Lines, 122–123, 377–378 os.path.normpath() method, 328–329 The Colchester Catch, 103, 376 os.remove() method, 333–334 Creating a More Efficient Oxford English Dictionary, 2nd Edition, 51 Safecracker, 144 Dictionary Cleanup, 33, 368–369 P Finding Digrams, 61, 369 A Galaxy Far, Far Away, 212–213, PageRank algorithm, 162 379–380 palindromes, 19, 23–25 Going the Distance, 282, 385–387 palingrams, 19, 25–33 Hacking Lincoln, 87, 370 paragraph objects, 111 Identifying Cipher Types, 88, patches, 2 370–371 path() method, 303 Pig Latin, 15, 367–368 pathnames, 328–329 Poor Man’s Bar Chart, 15–16, 368 Peale, Stan, 265 A Roundabout Way to Predict PEP 8, 6, 14 Detectability, 214, 383–385 PEP 257, 10–11, 14 Route : periapsis, 290 Brute-Force Attack, 88–89, perigee, 290 372–374 permutations, 51 Saving Mary, 102–103, 375 permutations with repetition, 137 Storing a Key as a Dictionary, persistent data, 155 88, 371 phonemes, 150 Syllable Counter vs. Dictionary File, phrase anagrams, 39 160, 378–379 phrase_anagrams., 43–44 predictive text, 147 A Picture Is Worth a Thousand Dollars Preferred Installer Program (pip), 7, problem, 264 11, 110–111 Pig Latin project, 15, 367–368 prep_words()function, 55 pillow module, 326–327 print() function, 3 PIL (Python Imaging Library), 327 print statements, 5 pip (Preferred Installer Program), 7, probability theory, 161. See also Markov 11, 110–111 chain analysis plaintext, 63, 68 process_choice() function, 45–46 planetary motion, 287 product() iterator, 137–138 planning, importance of, 2 profiles, 29–31 polar coordinates, 200 prograde, 288–289 polynomial equations, 193

Index 393 project code, xxiv–xxv. See also pygame, 267, 281–282, 296–319 source code Pylint, 7–13, 15 Benford’s Law of Leading Digits, PyScripter, xxiv 355–363 Python Enhancement Proposals, 6 Breeding an Army of Super-Rats, Python IDLE text editor, xxiv 130–136 Python Imaging Library (PIL), 327 Counting Syllables Python Standard Library, 170 Count Syllables, 156–158 python-docx, 110–111 Missing Words, 151–156 Cracking a High-Tech Safe, 140–142 Q Finding Palindromes, 24–25 Finding Palingrams, 28–29 quadratic equation, 193 Finding Phrase Anagrams, 43–49 QUIT event, 280 Finding Single-Word Anagrams, 38–39 R Finding Voldemort British Brute-Force, 54–61 radians, 275 Gallic Gambit, 50 rail fence cipher, 80–86 Generating Pseudonyms, 4–6 random module, 5, 134 Hiding a Vigenère Cipher, 114–119 raw string, 82 Markov Chain Analysis, 171–181 Recursive Approach project, 34 Mars Orbiter Game, 297–319 RegiStar, 331 Modeling the Milky Way RegiStax, 331 Galaxy Simulator, 202–211 relative paths, 329 Probability-of-Detection, repeated random sampling, 218 194–199 reStructuredText, 10–11, 15 Monty Hall Game, 228–238 retrograde, 288–289 Plumes of Io, 271–280 rotate() method, 303, 305–306 Rail Fence Cipher Roundabout Way to Predict Decryption, 84–86 Detectability project, 214, , 82–84 383–385 Route Cipher Decryption, 69–79 Route Cipher Encoder project, 90 Simulating Retirement Lifetimes, route transposition cipher, 64–79 250–259 Route Transposition Cipher: Brute- Stacking Jupiter Force Attack project, 88–89, Cropping and Scaling, 331–337 372–374 Enhancing, 340–342 run objects, 111 Stacking, 337–340 Trevanion Cipher, 94–98 S Verify vos Savant, 221–223 Writing a Null Cipher, 99–101 Sagan, Carl, 211 proportional fonts, 109–110 Saving Mary project, 102–103, 375 prototype and patch, 2 scaffolding, 170 pseudocode scale invariant, 348 defined, 3–4 SciPy, 195 graphical example, 127–128 SDL (Simple DirectMedia Library), 267 pseudonym generator, 1–13 semilogarithmic plots, 349 pseudonyms.py, 4–5 semordnilap, 26 Psych, 1 sensitivity studies, 136 PyCharm, xxiv serialization, 155–156 pycodestyle, 7 Shakespeare, William, 186 pydocstyle, 11, 15 Shannon, Claude, 162, 184

394 Index SHARPEN filter, 342 speech recognition, 162 shell utilities module, 329 Sphinx, 11 Sheppard, Clinton, 143 spiral galaxies, 188–189 Shock Canopy project, 283 spiral transfer, 292 shutil module, 329 splat operator (*), 25 shutil.rmtree() method, 333 split() function, 70 Simple DirectMedia Library (SDL), 267 Sprite class, 272, 274, 298, 304, 314 Slatkin, Brett, 15 Stager, Anson, 64 slicing, 23–24 standardized names and procedures, 6 sorted() function, 37 statistics, 217 sounds, 315–316 , 91, 108 source code. See also project code stop conditions, 126 anagrams.py, 38 Storing a Key as a Dictionary project, benford.py, 355–361 88, 371 brute_force_cracker.py, 138 string format method, 210 count_syllables.py, 157–158 string.punctuation constant, 95–96 crop_n_scale_images.py, 332–334 strings, 5–6 elementary_ink.py, 114–116 strip() function, 65 enhance_image.py, 340–341 style guide, 6 galaxy_simulator.py, 202–209 styles, 111–112 load_dictionary.py, 22 statistical models of, 168 list_cipher.py, 99 , 65, 88, 106 markov_haiku.py, 171–180 superclasses, 229 mars_orbiter.py, 297–319 Syllable Counter vs. Dictionary File missing_words_finder.py, 151–156 project, 160, 378–379 monty_hall_gui.py, 229–237 syllables, counting, 147–159 monty_hall_mcs.py, 221–222 synchronous orbits, 292 nest_egg_mcs.py, 250–258 sys module, 5 nest_egg_mcs_1st_5yrs.py, 261–262 sys.exit(1), 21 null_cipher_finder.py, 95–98 palindromes.py, 24 T palingrams.py, 28–29 palingrams_optimized.py, 32 tableau, 106 phrase_anagrams.py, 44–47 Taleb, Nassim, 219, 263 probability_of_detection.py, 195–198 terminal window, 111 pseudonyms.py, 4–5 text editors, 20 pseudonyms_main_fixed.py, 12–13 “The Growing Importance of Natural Pylint, 7–13 Language Processing” rail_fence_cipher_decrypt.py, 84–86 (DeAngelis), 155–156 rail_fence_cipher_encrypt.py, 82–84 Think Python, 2nd Edition (Downey), 2, route_cipher_decrypt_prototype.py, 33, 61, 170 69–72 third-party modules safe_cracker.py, 140–142 pillow, 326–327 stack_images.py, 338 python-docx, 110–111 super_rats.py, 130–136 resources for, 15 test_count_syllables_w_full_corpus​ Three-Rail Fence Cipher project, 90 .py, 159 time.time(), 31 tvashtar.py, 271–279 widget, 231 voldemort_british.py, 54–61 tkinter, 200–202, 212, 229–231 spam detection, 147 tracking, 109–110 spam filtering, 162 training corpus, 148, 151

Index 395 transposition cipher, 65 variables, 136 Trevanion cipher, 91–98 duration variable, 254 trig r a m _filte r() function, 58 eccentricity variable, 308–309, trigrams, 52 314, 316 Trump, Donald, 185–186, 353–354 video, 330–331 try statement, 21 Vigenère cipher, 106–121 Turing, Alan, 185 Virtual Muse: Experiments in Computer Turing Test project, 185 Poetry (Hartman), 145, Turtledove, Harry, 66 167, 184 Twain, Mark, 239–240 vos Savant, Marilyn, 217–218 2of4brif.txt file, 20 W U while loops, 5, 135, 254, 318 Unbelievable! This Is Unbelievable! While No One Was Looking Unbelievable! project, project, 366 185–186 widget, defined, 203 unbreakable cipher. See Vigenère cipher With a Bullet project, 284 uncertainty, 245 with statement, 21 universal gravity, 286–287, 307 word association norms (WANs), 184 epoch, 31 word lists, 20 unpacking, 176 update() method, 277, 303 Y user interfaces, writing, 178–181 Using Monospace Font project, 123 Yahoo! Mail, 121

V Z validate_col_row() function, 78 Zatara, Zatanna, 19, 33 values, 172 Zen of Python, 6 Vanishing Act project, 344–345 zip() function, 133, 141 zip_longest() function, 85–86 zorder attribute, 360

396 Index