Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
8 views24 pages

Think Like A Programmer

Saad Bin Asif | Software Engineer | FAST | NUCES

Uploaded by

Saad bin Asif
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
8 views24 pages

Think Like A Programmer

Saad Bin Asif | Software Engineer | FAST | NUCES

Uploaded by

Saad bin Asif
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 24
STRATEGIES FOR PROBLEM SOLVING This book is about problem solving, but what is problem solving, exactly? When people use the term in ordinary conversation, they often mean something very different from what we mean here. If your 1997 Honda Civic has blue smoke coming from the tailpipe, is idling roughly, and has lost fuel efficiency, this is a problem that can be solved with automotive knowledge, diagnosis, replacement equipment, and common shop tools. If you tell your friends about your problem, though, one of them might say “Hey, you should trade that old Honda in for something new. Problem solved.” But your friend’s suggestion wouldn't really be a solution to the problem—it would be a way to avoid the problem, Problems include constraints, unbreakable rules about the problem or the way in which the problem must be solved. With the broken-down Civic, one of the constraints is that you want to fix the current car, not purchase a new car. The constraints might also include the overall cost of the repairs, how long the repair will take, or a requirement that no new tools can be pur- chased just for this repair 2 ‘When solving a problem with a program, you also have constraints. Com- mon constraints include the programming language, platform (does it run ona PG, or an iPhone, or what?), performance (a game program may require graphics to be updated at least 30 times a second, a business application might have a maximum time response to user input), or memory footprint. Sometimes the constraint involves what other code you can reference: Maybe the program can’t include certain open-source code, or maybe the opposite— maybe it can use only open source. For programmers, then, we can define problem solving as writing an original program that performs a particular set of tasks and meets all stated constraints. Beginning programmers are often so eager to accomplish the first part, of that definition—wniting a program to perform a certain task—that they fail on the second part of the definition, meeting the stated constraints. I call a program like that, one that appears to produce correct results but breaks one or more of the stated rules, a Kobayashi Maru. If that name is unfamiliar to you, it means you are insufficiently familiar with one of the touchstones of geek culture, the film Star Tiek Il: The Wrath of Khan. The film contains a sub- plot about an exercise for aspiring officers at Starflect Academy. The cadets are put aboard a simulated starship bridge and made to act as captain on a mission that involves an impossible choice. Innocent people will die on a wounded ship, the Kobayashi Maru, but to reach them requires starting a battle with the Klingons, a battle that can only end in the destruction of the captain's ship. The exercise is intended to test a cadets courage under fire ‘There's no way to win, and all choices lead to bad outcomes. Toward the end of the film, we discover that Captain Kirk modified the simulation to make it actually winnable. Kirk was clever, but he did not solve the dilemma of the Kobayashi Maru; he avoided it Fortunately, the problems you will face as a programmer are solvable, but many programmers still resort to Kirk's approach. In some cases, they do so accidentally. (“Oh, shoot! My solution only works if there are a hundred data items or fewer. It’s supposed to work for an unlimited data set. I'll have to rethink this.") In other cases, the removal of constraints is deliberate, a ploy to meet a deadline imposed by a boss or an instructor. In still other cases, the programmer just doesn’t know how to meet all of the constraints. In the worst cases I have seen, the programming student has paid someone else to write the program. Regardless of the motivations, we must always be diligent to avoid the Kobayashi Maru. Classic Puzzles Chapter As you progress through this book, you will notice that although the particu- lars of the source code change from one problem area to the next, certain patterns will emerge in the approaches we take. This is great news because this is what eventually allows us to confidently approach any problem, whether we have extensive experience in that problem area or not. Expert problem solvers are quick to recognize an analogy, an exploitable similarity between a solved problem and an unsolved problem. If we recognize that a feature of problem A is analogous to a feature of problem B and we have already solved problem B, we have a valuable insight into solving problem A. In this section, we'll discuss classic problems from outside the world of programming that have lessons we can apply to programming problems. The Fox, the Goose, and the Corn The first classic problem we will discuss is a riddle about a farmer who needs to cross a river, You have probably encountered it previously in one form or another PROBLEM: HOW TO CROSS THE RIVER? AA farmer with a fox, a goose, and a sack of corn needs to cross a river. The farmer has a rowboat, but there is room for only the farmer and one of his three items. Unfor- tunately, both the fox and the goose are hungry. The fox cannot be left alone with the goose, or the fox will eat the goose. Likewise, the goose cannot be left alone with the sack of com, or the goose will eat the corn. How does the farmer get everything across the river? The setup for this problem is shown in Figure 1-1. Ifyou have never encountered this problem before, stop here and spend a few minutes trying to solve it, If you have heard this riddle before, try to remember the solution and whether you were able to solve the riddle on your own, Far Shore NAL SS A_A_A_AW NALA r+ Near Shore Figure 1-1: The fox, the goose, and the sack of corn, The boat can carry one item at a time. The fox cannot be left on the same shore as the goose, and the goose cannot be left on the same shore as the sack of corn. Strategies lo: Proslen Solving 3 4 Chapter Few people are able to solve this riddle, at least without a hint. I know I wasn't. Here's how the reasoning usually goes. Since the farmer can take only one thing at a time, he'll need multiple trips to take everything to the far shore. On the first trip, if the farmer takes the fox, the goose would be left with the sack of com, and the goose would eat the com. Likewise, if the farmer took the sack of corn on the first trip, the fox would be left with the goose, and the fox would eat the goose. Therefore, the farmer must take the goose on the first trip, resulting in the configuration shown in Figure 1-2 we Far Shore AAA VAAL a oad Near Shore Figure 1-2: The required fist step for solving the problem of the fox, the goose, and the sack of corn. From this step, however, all further steps appear fo end in failure. So far, so good. But on the second trip, the farmer must take the fox or the corn, Whatever the farmer takes, however, must be left on the far shore with the goose while the farmer returns to the near shore for the remaining item, This means that either the fox and goose will be left together or the goose and corn will be left together. Because neither of these situations is acceptable, the problem appears unsolvable. Again, if you have seen this problem before, you probably remember the key element of the solution. The farmer has to take the goose on the first trip, as explained before. On the second trip, let's suppose the farmer takes the fox. Instead of leaving the fox with the goose, though, the farmer lakes the goose back to the near shore. Then the farmer takes the sack of corn across, leaving the fox and the corn on the far shore, while returning for a fourth trip with the goose. The complete solution is shown in Figure 1-3. This puzzle is difficult because most people never consider taking one of the items back from the far shore to the near shore. Some people will even suggest that the problem is unfair, saying something like, “You didn’t say I could take something back!” This is true, but it’s also true that nothing in the problem description suggests that taking something back is prohibited. Y The “Trick” Step é 7 8 Figure 1-3: Step-by-step solution to the fox, goose, and corn puzzle Think about how much easier the puzzle would be to solve if the possi- bility of taking one of the items back to the near shore was made explicit: The farmer has a rowhoat that can be used to transfer items in either direction, but there is room only for the farmer and one of his three items. With that suggestion in plain sight, more people would figure out the problem. This illustrates an important principle of problem solving: If you are unaware of all possible actions you could take, you may be unable to solve the problem. We can refer to these actions as operations. By enumerating all the possible opera- tions, we can solve many problems by testing every combination of opera- tions until we find one that works. More generally, by restating a problem in more formal terms, we can often uncover solutions that would have other- wise eluded us. Strategies lo: Proslen Solving 5

You might also like