Best of Ruby Quiz, Volume 1 (2006).Pdf

Best of Ruby Quiz, Volume 1 (2006).Pdf

Best of Ruby Quiz Volume One James Edward Gray II The Pragmatic Bookshelf Raleigh, North Carolina Dallas, Texas P r a g m a t i c B o o k s h e l f Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and The Pragmatic Programmers, LLC was aware of a trademark claim, the designations have been printed in initial capital letters or in all capitals. The Pragmatic Starter Kit, The Pragmatic Programmer, Pragmatic Programming, Pragmatic Bookshelf and the linking g device are trademarks of The Pragmatic Programmers, LLC. Every precaution was taken in the preparation of this book. However, the publisher assumes no responsibility for errors or omissions, or for damages that may result from the use of information (including program listings) contained herein. Our Pragmatic courses, workshops, and other products can help you and your team create better software and have more fun. For more information, as well as the latest Pragmatic titles, please visit us at http://www.pragmaticprogrammer.com Copyright © 2006 The Pragmatic Programmers LLC. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmit- ted, in any form, or by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior consent of the publisher. Printed in the United States of America. ISBN 0-9766940-7-7 Printed on acid-free paper with 85% recycled, 30% post-consumer content. First printing, February 2006 Version: 2006-3-14 Contents 1 Introduction 1 I The Quizzes 5 1. Mad Libs ............................ 6 2. LCD Numbers ......................... 8 3. GEDCOM Parser ....................... 9 4. Animal Quiz .......................... 11 5. Scrabble Stems ........................ 13 6. Regexp.build() ......................... 14 7. HighLine ............................ 16 8. Roman Numerals ....................... 18 9. Rock Paper Scissors ..................... 20 10. Knight’s Travails ....................... 25 11. Sokoban ............................ 27 12. Crosswords .......................... 29 13. 1-800-THE-QUIZ ....................... 31 14. Texas Hold’em ........................ 33 15. Solitaire Cipher ........................ 36 16. English Numerals ...................... 41 17. Code Cleaning ........................ 42 18. Banned Words ........................ 44 19. Secret Santas ......................... 46 20. Barrel of Monkeys ...................... 48 21. Amazing Mazes ........................ 50 22. Learning Tic-Tac-Toe .................... 52 23. Countdown .......................... 53 24. Solving Tactics ........................ 55 25. Cryptograms ......................... 57 CONTENTS v II Answers and Discussion 60 1. Mad Libs ............................ 61 Custom Templating ..................... 62 Mini Libs .......................... 66 Additional Exercises .................... 67 2. LCD Numbers ......................... 68 Using Templates ...................... 68 On and Off Bits ....................... 70 Using a State Machine ................... 72 Additional Exercises .................... 75 3. GEDCOM Parser ....................... 76 Optimizing the Read and Write Cycles ........... 77 Additional Exercises .................... 80 4. Animal Quiz .......................... 81 Arrays Instead of Custom Objects ............. 84 Leaving the Trees ...................... 87 Additional Exercises .................... 88 5. Scrabble Stems ........................ 89 Eating Less RAM ...................... 90 Additional Exercises .................... 92 6. Regexp.build() ......................... 93 Shrinking a Regexp ..................... 94 Speeding Up the Build ................... 97 Timing the Solutions .................... 99 Additional Exercises .................... 100 7. HighLine ............................ 101 A Class-Based Solution ................... 101 Testing I/O ......................... 104 The Official HighLine .................... 106 Additional Exercises .................... 111 8. Roman Numerals ....................... 112 Saving Some Memory .................... 113 Romanizing Ruby ...................... 115 Additional Exercises .................... 120 9. Rock Paper Scissors ..................... 121 Outthinking a Random Player ............... 122 Cheat to Win ........................ 124 Psychic Players ....................... 125 Thinking Outside the Box ................. 126 Additional Exercises .................... 126 Report erratum CONTENTS vi 10. Knight’s Travails ....................... 127 Or with Less Abstraction .................. 131 Additional Exercises .................... 132 11. Sokoban ............................ 134 Objectified Sokoban .................... 136 Saving Your Fingers .................... 142 Additional Exercises .................... 143 12. Crosswords .......................... 145 Passive Building ...................... 148 Additional Exercises .................... 152 13. 1-800-THE-QUIZ ....................... 153 Word Signatures ...................... 153 The Search ......................... 155 Cleaning Up and Showing Results ............. 157 Additional Exercises .................... 159 14. Texas Hold’em ........................ 160 Ruby’s Sorting Tricks .................... 160 Sorting Cards ........................ 161 Name the Hand ....................... 162 Additional Exercises .................... 165 15. Solitaire Cipher ........................ 166 Testing a Cipher ...................... 166 A Deck of Letters ...................... 170 A Test Suite and Solution ................. 173 Additional Exercises .................... 175 16. English Numerals ...................... 176 Grouping Numbers ..................... 176 Coding an Idea ....................... 177 Proper Grammar ...................... 179 Additional Exercises .................... 182 17. Code Cleaning ........................ 183 Instant Web Serving .................... 183 Finding the Hidden Wiki .................. 184 The Other Program ..................... 188 Additional Exercises .................... 190 18. Banned Words ........................ 191 Doing Even Fewer Checks ................. 193 Additional Exercises .................... 194 Report erratum CONTENTS vii 19. Secret Santas ......................... 195 Using a Random Sort .................... 197 A Ring of Players ...................... 197 Grouping .......................... 198 Climbing a Hill ....................... 200 Additional Exercises .................... 201 20. Barrel of Monkeys ...................... 203 Fancy Searching ...................... 207 Additional Exercises .................... 213 21. Amazing Mazes ........................ 214 The Internal Bits ...................... 214 Making a Maze ....................... 219 Solving a Maze ....................... 220 Interface .......................... 222 Additional Exercises .................... 223 22. Learning Tic-Tac-Toe .................... 225 The History of MENACE .................. 232 Filling a Matchbox Brain .................. 232 Ruby’s MENACE ...................... 236 Additional Exercises .................... 238 23. Countdown .......................... 239 Pruning Code ........................ 240 Coding Different Strategies ................. 244 Additional Exercises .................... 247 24. Solving Tactics ........................ 249 From Playing to Solving .................. 252 Proof through Unit Testing ................. 255 Additional Exercises .................... 258 25. Cryptograms ......................... 259 Using Word Signatures ................... 259 Building the Map ...................... 261 Assembling a Solution ................... 264 A Look at Limitations .................... 269 Additional Exercises .................... 269 A Resources 270 A.1 Bibliography ......................... 270 Report erratum Chapter 1 Introduction If you stop and think about it, programming knowledge is nearly use- less by itself. What exactly are you going to create with all that expert programming skill, if it’s all you have? The world needs only so many text editors. What makes the craft interesting is how we apply it. Combine program- ming prowess with accounting practices or even just a need to reunite hurricane victims with their scattered family members, and you have the makings of a real, and potentially useful, application. Practical programming experience can be surprisingly hard to come by. There are classes and books to give us theory and syntax. If you’ve been a programmer for any amount of time, you will have read plenty of those books. Then what? I think most of us inherently know that the next step is to write something, but many of us struggle to find a topic. I love games. I’m always playing something, and struggling to put together a winning strategy never quite feels like work to me. I use that to make myself a better programmer. I play games with my code. I assign myself a task I’ve never tried before, perhaps to get more famil- iar with an algorithm or a library. Or sometimes I’ll give myself a com- pletely routine task but add an unusual twist: implement this full- featured trivial program in one hour or less. This is my form of practice for the big game. I find what works and even what doesn’t.1 I memorize idioms I like, just in case I run into a 1True story: I’m still struggling with one programming problem I’ve been playing with for about ten years now. I’ve never found a solution

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    285 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us