a problem solver’s introduction to programming

20
A Problem Solver’s Introduction to Programming The Principles of Discovery D Rajeev, Schonweiss

Upload: dilip-rajeev

Post on 08-Mar-2016

216 views

Category:

Documents


0 download

DESCRIPTION

On Heuristics and Problem Solving. A Programmer's Perspective, by Dilip Rajeev.

TRANSCRIPT

Page 1: A Problem Solver’s Introduction to Programming

A Problem Solver’s Introduction to Programming The Principles of Discovery

D Rajeev, Schonweiss

Page 2: A Problem Solver’s Introduction to Programming

In any field, accomplishing a task involves solving problems – a process of discovery – of finding a solution to a problem.

Problem solving, like any art, can be taught. The principles are broad, and allows for application to any field.

Like any art – it requires practice, diligence, and effort to gain mastery in it.

Page 3: A Problem Solver’s Introduction to Programming

The Process of Discovery

What drives discovery? Interest in solving a problem, a genuine want to know

more, exploration, experimentation.

Page 4: A Problem Solver’s Introduction to Programming

Learning to Explore

Problem solving is about learning to explore intelligently. Explore in a confident manner.

“The typical novice climber sensibly hugs the rock face that he is attempting to climb, for it is not intuitive to push away from the rock. Yet once he grits his teeth and pushes out his rear end, a miracle happens: the component of gravity that is perpendicular tothe rock rises, which increases the frictional force on his feet and immediately produces a more secure stance. “ – Paul Zietz, Mathematician, On Problem Solving

Page 5: A Problem Solver’s Introduction to Programming

“You are standing at the base of a mountain, hoping to climb to the summit. Your first strategy may be to take several small trips to various easier peaks nearby, so as to observe the target mountain from different angles. After this, you may consider a somewhat more focused strategy, perhaps to try climbing the mountain via a particular ridge. Now the tactical considerations begin: how to actually achieve the chosen strategy. For example, suppose that strategy suggests climbing the south ridge of the peak, but there are snowfields and rivers in our path. Different tactics are needed to negotiate each of these obstacles. For the snowfield, our tactic may be to travel early in the morning, while the snow is hard. For the river, our tactic may be scouting the banks for the safest crossing. Finally, we move onto the most tightly focused level, that of tools: specific techniques to accomplish specialized tasks. For example, to cross the snowfield we may set up a particular system of ropes for safety and walk with ice axes…. As we climb a mountain, we may encounter obstacles. Some of these obstacles are easy to negotiate, for they are mere exercises (of course this depends on the climber's ability and experience). But one obstacle may present a difficult miniature problem, whose solution clears the way for the entire climb. For example, the path to the summit may be easy walking, except for one lO-foot section of steep ice. Climbers call negotiating the key obstacle the crux move.” – Paul Zietz

Learning to Explore

Page 6: A Problem Solver’s Introduction to Programming

The Problem Solver

“The problem solver gets hot, cold, wet, tired, and hungry. The problem solver gets lost and has to find his or her way. The problem solver gets blisters. The problem solver climbs to the top of mountains, sees hitherto undreamed of vistas. The problem solver arrives at places of amazing beauty, and experiences ecstasy that is amplified by the effort expended to get there. When the problem solver returns home, he or she is energized by the adventure, and cannot stop gushing about the wonderful experience.” - Zietz

Page 7: A Problem Solver’s Introduction to Programming

Understand the Problem State the problem in your own words. Restate it in clearer terms. Ask yourself what the relevance of each given, or each part

is. Can you boil the question down to sub-statements or sub-

parts? Analyze each part. What is the relevance of the data provided? Is the data

given insufficient? If found insufficient can you show that it is the case? How does the given relate to the mechanics of the problem?

Does the problem resemble one you have seen before? Can it be broken down to a problem you have already seen?

Page 8: A Problem Solver’s Introduction to Programming

Experimentation

“The result of the mathematician's creative work is demonstrative reasoning, a proof, but the proof is discovered by plausible reasoning, by guessing''

--George Polya, Celebrated Mathematician ( In `Mathematics and Plausible Reasoning', 1953.)

Searching for patterns. The conjecture – prove cycle.

Page 9: A Problem Solver’s Introduction to Programming

Conjecture

con·jec·ture/kənˈjekCHər (noun.) An opinion or conclusion formed on the basis of incomplete information.(verb.) Form an opinion or supposition about (something) on the basis of incomplete information.

Page 10: A Problem Solver’s Introduction to Programming

Pizza Puzzle

Into how many parts can you divide a pizza with n cuts across it?

Understand the problem clearly. State it in simpler terms, leaving out unnecessary parts. Experiment and extract patterns. Can you experiment and extract patterns? What are the variables involved? What is the relation between the variables? How does changing one affect another? How does a unit change in a variable affect another?

Page 11: A Problem Solver’s Introduction to Programming

Proof Phase –Domino(e)s Technique

Arrange the blocks such that if u push a block over to the right, the next one on the right falls as well.

What happens when you push the first block? Can you use the idea to prove out conjecture? Mathematicians call this technique “The Principle of

Mathematical Induction.”

Page 12: A Problem Solver’s Introduction to Programming

A little further

In how many parts can an n-1 dimensional knife divide an n dimensional cube with k cuts? Can you experiment and discover a pattern?

Page 13: A Problem Solver’s Introduction to Programming

More experimentation 1/(1*2) + 1/(2*3) +1/(3*4)…. until n terms There are 100 doors, all initially closed. In a nearby cage are 100

monkeys. The first monkey is let out, and runs along the doors opening every one. The second monkey is then let out, and runs along the doors: closing the 2nd, 4th, 6th,... all the even-numbered[multiple of two] doors. The third monkey is let out. He attends only to the 3rd, 6th, 9th,... doors (every third door, in other words), closing any that is open and opening any that is closed, and so on. The nth monkey attends to every multiple of n door – closing if open, opening it if already closed. After all 100 monkeys have done their work in this way, what state are the doors in after the last pass, which doors are left open and which are closed?

Page 14: A Problem Solver’s Introduction to Programming

Of Mice and Men – George PolyaThe landlady hurried into the backyard, put the mousetrap on the ground (it was an old-fashioned trap, a cage with a trapdoor) and called to her daughter to fetch the cat. The mouse in the trap seemed to understand the gist of these proceedings; he raced frantically in his cage, threw himself violently against the bars, now on this side and then on the other, and in the last moment he succeeded in squeezing himself through and disappeared in the neighbour's field. There must have been on that side one slightly wider opening between the bars of the mousetrap ... I silently congratulated the mouse. He solved a great problem, and gave a great example.

That is the way to solve problems. We must try and try again until eventually we recognize the slight difference between the various openings on which everything depends. We must vary our trials so that we may explore all sides of the problem. Indeed, we cannot know in advance on which side is the only practicable opening where we can squeeze through.

The fundamental method of mice and men is the same: to try, try again, and to vary the trials so that we do not miss the few favorable possibilities. It is true that men are usually better in solving problems than mice. A man need not throw himself bodily against the obstacle, he can do so mentally; a man can vary his trials more and learn more from the failure of his trials than a mouse.

Page 15: A Problem Solver’s Introduction to Programming

Problem-Sub Problem

Heaviest odd one of n pearls. With a beam balance. Discuss among yourself. Start by experimenting with a few pearls.

A shopkeeper can only place the weights in one side of the common balance. For example if shopkeeper has weights 1 and 3 then he can measure 1,3 and 4 only. Now the question is how many minimum weights and name the weights you will need to measure all weights from 1 to 10000.

What if he could place weights on both sides of the balance ? Start by experimenting from smaller cases – understand the

problem.

Page 16: A Problem Solver’s Introduction to Programming

Recursion: The art of making a problem solve itself. Can you write an algorithm 2 generate this picture? … going a n

times…In simple words..

DrawMonalisaWithFrameinHand(int n, Position StartPos, size MonaLisaSize, size FrameSize ){If (n!=0) { // Draw a monalisa at ; // Draw a frame in her hand of frameSize; //Draw MonaLisa(n-1, frame.topLeft, FrameSize, FrameSize/MonaLisaSize*FrameSize); }

if (n==0) { // Draw a simple Monalisa who does not hold a frame }

}

Page 17: A Problem Solver’s Introduction to Programming

Generate Recursive Triangles

Page 18: A Problem Solver’s Introduction to Programming

A Sample Solution public void Draw(int n, Point TriangleFirstCorner, Point TriangleSecondCorner, Point

TriangleThirdCorner) { DrawLine(TriangleFirstCorner, TriangleSecondCorner); DrawLine(TriangleSecondCorner, TriangleThirdCorner); DrawLine(TriangleThirdCorner, TriangleFirstCorner);

if (n-- > 0) { Draw(n, TriangleFirstCorner.GetMidPoint(TriangleSecondCorner),

TriangleSecondCorner.GetMidPoint(TriangleThirdCorner), TriangleThirdCorner.GetMidPoint(TriangleFirstCorner));

}

}

Page 19: A Problem Solver’s Introduction to Programming

Recursion – Thinking About

Problems can be expressed in terms of itself. Or can be solved by reducing it to a problem on a smaller size set.

int Fib (int n) { if ((n==1)||(n==0))return 1; else return Fib(n-1) + Fib(n-2); }

A tail scenario – where the function is told how to solve itself. A reduction scenario, where the function reduces itself to simpler instances of the same problem.

Page 20: A Problem Solver’s Introduction to Programming

Pack of Coins

The number of ways to change amount a using n kinds of coins equals

the number of ways to change amount a using all but the first kind of coin, plus

the number of ways to change amount a - d using all n kinds of coins, where d is the denomination of the first kind of coin.