所需积分/C币:9 2013-08-08 11:01:11 1.38MB PDF
收藏 收藏

Pragmatic Bookshelf 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 Copyright o 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 ISBN0-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 I The quizzes 1. Mad Libs 2. LCD Numbers 3. GEDCOM Parser 56891 4. Animal Quiz 5. Scrabble stems 13 6. Regexp build 7. HighLine 46 8. Roman numerals 18 9. Rock Paper Scissors 20 10. Knight,'s Travails 25 11. Sokoban 27 12. Crosswords 29 13.1-800-THE-gU1z 31 14. Texas hold'em 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 II Answers and discussion 60 1. Mad Libs 61 Custom Templating 62 Mini libs 66 Additional exercises 67 2. LCD Numbers 68 ing Templa 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 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 CONTENTS< vi 10. Knight,'s Travails 127 Or with Less abstraction 131 Additional exercises 132 11. Sokoban 134 Objectified Sokoban 136 Saving Your Fingers 42 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 holdem 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 l70 A Test Suite and solution 173 Additional exercises 175 16. English Numerals 176 Grouping numbers 176 Coding an Idea l77 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 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 Rubys 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 exercise 258 25. Cryptograms 259 Using Word Signatures 259 Building the Map 261 Assembling a Solution 264 A Look at limitations 269 Additional exercises 269 Resources 270 A1 Bibliography ,,,,,,,,270 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 doesnt. I memorize idioms I like, just in case I run into a ITrue 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 I like, though I know others ha solved it. (I havent peeked! I also havent made it a ruby quiz yet, because I'm not ready to be embarrassed. Ill get it eventually CHAPTER 1. INTRODUCTION< 2 similar problem down the road. All the while, I'm getting more familiar with languages, libraries, and frameworks I may need to work with someday The set of weekly programming challenges for the Ruby programming language called Ruby quiz- was born out of my desire to share this with the rest of the world. This book holds some highlights from the first year of its run What's Inside In these pages, you will find a collection of problems contributed by myself and others to enhance your programming knowledge. The great thing about working with these problems is that they come with dis cussions on some of their interesting points and sample solutions from other programmers. You can solve the challenges and then compare and contrast your code with the solutions provided There is not yet a way to download all of these programming idioms directly into your brain. Let me forewarn you, solving these problems is work. We try to have fun with the Ruby Quiz, but it doesnt come without the price of a little effort. The problems vary in difficulty, but I believe there's something to be learned from all of them How to Use This Book This book isn't meant for passive readers! Get those brain cells moving You will learn a lot more by giving a quiz your best shot, even if it doesn,t blossom into a solution, and then reading the discussions. It,s the context you gain from the attempt that allows you to internalize what you learn, and that,s the whole point May this teach you half of what it has taught me Finding Your Way Around The front of this book is a collection of twenty-five programming chal lenges. In the back of the book, you can find discussions and solutions 2 Yes, I'm one of the guys who skips the "Additional Exercises"in almost all program ming books. However, I must admit that Ive learned the most when I actually did them CHAPTER 1. INTRODUCTION< 3 for these problems. The separation is there to allow you to scan prob lems and find something you want to try without accidentally running into a spoiler. At the beginning of each quiz, you will find a pointer to the page the relevant discussion begins on Along the way you will find Live code Most of the code snippets shown within come from full-length running examples, which you can download. 4 To help you find your way, if code can be found in the download, there'll be a marker line like the one that follows at the top of the listing in the book. madlibs/parsed_madlib. rb Ordinary prose class string Anything is acceptable def self parse?( token, replacements new(token) end end If you're reading the PDF version of this book and if your PDF viewer supports hyperlinks, you can click the marker, and the code should appear in a browser window. Some browsers(such as Safari) might mistakenly try to interpret some of the code as HTML. If this happens, view the source of the page to see the real source code Joe asks Joe, the mythical developer. sometimes pops up to ask questions about stuff we talk about in the text. We try to answer these as we go along Spring Cleaning Solutions in this text are just as they were submitted originally, with the following exceptions Tabs have been replaced with the ruby standard practice of two spaces Method and variable names were adjusted to Ruby,'s snake_case style convention From

试读 127P Best.of.Ruby.Quiz
立即下载 身份认证VIP会员低至7折
关注 私信
Best.of.Ruby.Quiz 9积分/C币 立即下载

试读结束, 可继续阅读

9积分/C币 立即下载