Problem Solving for Programming Session 2 Languages of Description and Problem Solving Strategies.

33
Problem Solving for Programming Session 2 Languages of Description and Problem Solving Strategies

Transcript of Problem Solving for Programming Session 2 Languages of Description and Problem Solving Strategies.

Page 1: Problem Solving for Programming Session 2 Languages of Description and Problem Solving Strategies.

Problem Solving for Programming

Session 2 Languages of Description

and Problem Solving Strategies

Page 2: Problem Solving for Programming Session 2 Languages of Description and Problem Solving Strategies.

How do we solve problems?

• There are many different kinds of problems we need to solve as programmers.

• Some are complex and very difficult to solve. • In order to solve complex and difficult problems,

we need:– an appropriate way of describing the problem

(a language of description)– an appropriate way of solving the problem

(a problem solving strategy).

Page 3: Problem Solving for Programming Session 2 Languages of Description and Problem Solving Strategies.

Languages of description – natural language

• The most accessible tool at our disposal as problem solvers is natural language.

• Natural language allows us to describe problems in a way that can easily be understood by everybody.

• However, natural language is often imprecise and ambiguous (e.g. He eats shoots and leaves as opposed to He eats, shoots and leaves), which is of little use when we are writing computer programs, which need to be clear, precise and unambiguous.

• Because it can be imprecise and ambiguous, natural language should be only one tool amongst many that we use when problem solving. Natural language needs to be augmented with more precise ways of representing problems.

Page 4: Problem Solving for Programming Session 2 Languages of Description and Problem Solving Strategies.

Languages of description – visual representation

• An alternative to natural language, and an important tool in a problem solver’s armoury, is visual or graphical representation.

• Visual representation enables us to make complex problems easier to understand and solve.

• Take the following example:

Page 5: Problem Solving for Programming Session 2 Languages of Description and Problem Solving Strategies.

Languages of description – visual representation

One morning at eight o’clock you set off in your car to visit a friendwho lives some distance away. You encounter the odd traffic delayand your driving speed varies as the speed limits on the differentstretches of road change. It is a long drive, so you stop twice torefresh yourself and to eat and drink something. You arrive at 6.00p.m. and decide to stay overnight. The next morning you set off again at 8.00 a.m. You take the same route back, but encounter heavy traffic in different places. Because one traffic jam was so large you only stop once for refreshments. Still, you arrive back home at six o’clock.

Question: Is there a point on the road that you pass atexactly the same time of day on the outward and return journeys?

• Trying to make sense of this problem on the basis a natural language description like this is difficult. There is a lot of detail to store in your head.

Page 6: Problem Solving for Programming Session 2 Languages of Description and Problem Solving Strategies.

Languages of description – visual representation

• A visual representation of the problem, on the other hand, allows us to see the problem and the solution more clearly.

• (Assuming the same average speed),we can see that the point is reached at 2pm. Six hours into the journey.

Home Friend

8am 6pm

Going

9 10 11 12 1 2pm 3 4 5

6pm 8am5 4 3 2pm 1 12 11 10 9

Coming

Page 7: Problem Solving for Programming Session 2 Languages of Description and Problem Solving Strategies.

Languages of description – visual representation

• The same problem can often be represented in many different ways.

• For example, you could represent the journey problem numerically like this.

• The best representation is the way that suits you (And, of course, the way that produces a correct solution!).

8 9 10 11 12 1 2 3 4 5 66 5 4 3 2 1 12 11 10 9 8

1

1

2

2

3

3

4

4

5

5

6

6

They only cross after 6 hours travel, which is the point they are at 2pm.

Page 8: Problem Solving for Programming Session 2 Languages of Description and Problem Solving Strategies.

Languages of description – visual representation

• Problems involving two or three dimensional space are very difficult to solve without some form of visual representation. Take the following:

There is a large square room whose walls are twenty-four feet long. The ceiling is eight feet high. On the floor in a corner is a bowl of sugar. In the opposite corner by the ceiling is an ant.

Question: What is the shortest path the ant can take to get to thesugar?

• The problem appears to be maths/geometry based, but even a somebody with a good head for maths might struggle with this one without some form of picture to assist him/her.

Page 9: Problem Solving for Programming Session 2 Languages of Description and Problem Solving Strategies.

Languages of description – visual representation

• The quickest route would appear to be straight down the wall and diagonally across the room to the sugar (A). However, we need to consider other possibilities.

• What about (B)? Is this shorter? How can we test if it is or not?

Ant

Sugar24ft

8ft

A

Ant

Sugar24ft

8ftB

Page 10: Problem Solving for Programming Session 2 Languages of Description and Problem Solving Strategies.

Languages of description – taking a different view

• The first one (A) is easy to test using Pythagoras.

• Now, what about the second one (B)? It appears we do not have sufficient information at our disposal to calculate the distance of the route.

• But what if we take a different view of the problem? • What if we represent the problem two-dimensionally

instead of three-dimensionally?

d = (√ l2 + w2) + 8√ 242 + 242 = 33.94133.941 + 8 = 41.941ft

Page 11: Problem Solving for Programming Session 2 Languages of Description and Problem Solving Strategies.

Languages of description – taking a different view

24ft

24ft

Sugar

8ft

AntAnt

24ft

24ft

8ft

Three dimensional

Two dimensional

• Can we solve the problem now? How?

Page 12: Problem Solving for Programming Session 2 Languages of Description and Problem Solving Strategies.

Languages of description – taking a different view

• It is now possible to draw a straight line from ant to sugar to create a triangle whose width and length are known, meaning, with a little help from Pythagoras, we can easily work out the distance we need.

• As a result, we can see that B is, in fact, the shortest path.

• This solution was only possible, because we took an alternative view of the problem.

24

ft

24ft

Ant

Sugar

8ft

d = √ 242 + 322 = 40ft

Page 13: Problem Solving for Programming Session 2 Languages of Description and Problem Solving Strategies.

Other problem solving strategies – solving a simpler analogous problem

• Another approach to solving complex problems may be to solve a simpler analogous problem first. Recall the problem about meeting on the road.

• In this problem, one person makes two journeys on two different days. What about if two people make the same journey (in reverse) on the same day. Of course they will meet, and of course they will meet at the same time.

• By rephrasing a problem in simpler terms, we can often see the solution more clearly.

Going Coming

Page 14: Problem Solving for Programming Session 2 Languages of Description and Problem Solving Strategies.

Other problem solving strategies – finding a pattern

• Consider the following problem:

How many angles are formed by ten distinct rays with a common end point?

Page 15: Problem Solving for Programming Session 2 Languages of Description and Problem Solving Strategies.

Other problem solving strategies – finding a pattern

• Let’s start simply (again solving a simpler analogous problem). How many angles are formed by one ray? Two rays? Three?

• The answer is clear here because there is little detail to consider (0, 1, 3).

Page 16: Problem Solving for Programming Session 2 Languages of Description and Problem Solving Strategies.

Other problem solving strategies – finding a pattern

• If we represent this data in a table, we get the following. Can you see a pattern emerging?

• What about four or five rays?

• Can you see a pattern emerging now?

No. rays 1 2 3

No. angles 0 1 3

No. rays 1 2 3 4 5

No. angles 0 1 3 6 ?

Page 17: Problem Solving for Programming Session 2 Languages of Description and Problem Solving Strategies.

Other problem solving strategies – working backwards

• Consider the following problem:

Helen has an 11-litre can and a 5-litre can. How can she measure out exactly seven litres of water?

11-litres

5-litres

7-litres

Page 18: Problem Solving for Programming Session 2 Languages of Description and Problem Solving Strategies.

Other problem solving strategies – working backwards

• We could try to measure out the 7-litres using a combination of cans through trial and error.

• However, if we consider where we are at the end of the problem, we can see that our can has 7-litres of water and 4-empty litres.

• Can we measure off four litres then? The answer is, yes. • If we empty the 11-litre can into the 5-litre can twice, we

know we have 1-litre left. If we empty this into a 5-litre can, we know the empty part is four litres.

• Now if we refill the 11-litre can, and pour off 4-litres into the 5-litre can, we are left with 7-litres in the 11-litre can.

Page 19: Problem Solving for Programming Session 2 Languages of Description and Problem Solving Strategies.

Other problem solving strategies – intelligent guessing

• Guessing solutions is not usually a good strategy, but intelligent guessing can be

• Consider the following problem:

The one mile relay team has four runners: Bob, Ian, Len and Sid. They run in alphabetical order. Each runner runs his quarter-mile 2 sec faster than the next runner. They finishin exactly 3mins 40 sec.

Question: How fast did each runner run his part?

Page 20: Problem Solving for Programming Session 2 Languages of Description and Problem Solving Strategies.

Other problem solving strategies – intelligent guessing

• It will help if we start by converting 3 min 40 sec to seconds (e.g. 220 sec).

• As a starting point for our guessing, we could imagine the runners are evenly matched, and run at the same speed. They would then run their respective parts of the race at 220/4 = 55 sec.

• We can use this for our initial guess for our slowest runner, Sid.

• But this is obviously wrong as 208<220. We know Sid has to run slower than 55 sec. So we need to increase his time.

Runner: Bob Ian Len Sid Total

Time: 49 51 53 55 208

Page 21: Problem Solving for Programming Session 2 Languages of Description and Problem Solving Strategies.

Other problem solving strategies – intelligent guessing

• If we up our guess by one, we are still wrong. • However, with a couple more (intelligent) guesses

we eventually arrive at the correct solution.

Bob Ian Len Sid Total

49 51 53 55 208

50 52 54 56 212

51 53 55 57 216

52 54 56 58 220

Page 22: Problem Solving for Programming Session 2 Languages of Description and Problem Solving Strategies.

Other problem solving strategies – logical reasoning

• Consider the following problem:

Place the numbers 1 – 9 in the following grid so that the sum of all the rows, columns and diagonals is the same.

Page 23: Problem Solving for Programming Session 2 Languages of Description and Problem Solving Strategies.

Other problem solving strategies – logical reasoning

• Start by placing all the numbers in the grid sequentially 1 – 9. The sum of all the cells is 45.

• If each row is to have the same sum, then it must be 15 (45/3).

• The next step is to determine which number should be entered into the centre cell. Start by trying extreme cases.

• If we entered 9, then 9 would appear in a row, column or diagonal with 8, 7 and 6, giving a total > 15.

• If we entered 1, then 1 would appear in a row, column or diagonal with 2, 3 and 4, giving a total < 15.

• Having accounted for all the possibilities, we can see now that only 5 can occupy the centre cell.

Page 24: Problem Solving for Programming Session 2 Languages of Description and Problem Solving Strategies.

Other problem solving strategies – logical reasoning

• Now we can use intelligent guessing. We could try

• The problem here, however, is that with nine in one column, the other two numbers in that column must total 6 (e.g. 2 & 4). But if 2 and 4 are in the column, then the row with 1 in it can never total 15.

• If we move our 1, 5 and 9 to the central column, however, we do not have the same problem.

• The rest is easy.

1

5

9

1 4

5 2

9

1

5

4 9 2

8 1 6

3 5 7

4 9 2

Page 25: Problem Solving for Programming Session 2 Languages of Description and Problem Solving Strategies.

Mathematical reasoning

• Mathematical reasoning underlies several of the problem solving strategies we have already looked at. For example:

• You do not need to be brilliant at maths to be a competent problem solver. But being good at maths will certainly help you to solve programming problems faster and more effectively.

d = √ 242 + 322 = 40ft

Page 26: Problem Solving for Programming Session 2 Languages of Description and Problem Solving Strategies.

Algorithms

• After we have solved a problem, we can start to develop algorithms to express our solutions in general terms.

• If you recall, in session 1 we developed a solution for the following problem:

In a room with ten people, everyone shakes hands with everyone else exactly once. In total, how many handshakes are there?

• The solution to the problem was as follows:

grid size = 10x10 = 100handshakes = (100-10)/2 = 45

• The solution was correct, but it would be of little use as a computer program, as it is specific to a particular number of participants (10).

• We saw that it would be much more useful to develop a general formula that is applicable to all instances of the problem: for example

h = ((p2) – p)/2

Page 27: Problem Solving for Programming Session 2 Languages of Description and Problem Solving Strategies.

Algorithms

• Algorithms are like this formula. They are generalisable to other instances of the same problem (e.g. 20, or 100, 10,000, n participants).

Page 28: Problem Solving for Programming Session 2 Languages of Description and Problem Solving Strategies.

Algorithms

• Imagine we have a Smartphone and we want to develop an app for it that will solve the handshakes problem we encountered in session 2. Would it be enough to tell the device:h = (p2 – p)/2

• Almost certainly not. There is simply not enough information available for a computer to know what to do. For example, we have not said what p is, or for that matter h, or where these values come from.

• Also, what does p2 mean to a computer?• We need to provide more detail and to express the algorithm

in a way a computer can understand in order for our program to work effectively.

• We need to provide a detailed, step-by-step set of instructions: in other words a concise and complete algorithm.

Page 29: Problem Solving for Programming Session 2 Languages of Description and Problem Solving Strategies.

Algorithms and pseudo code

• Algorithm development is the intermediate stage between problem solving and coding. It is where we specify the solution to a problem in a generalisable and structured format somewhere between natural language and programming code.

• The standard way that programmers have of creating algorithms is to use pseudo code (sometimes called structured English).

• Pseudo code is a high-level description of a computer program that uses the structural conventions of a programming language, but is intended for human reading rather than machine reading.

• Importantly, it is programming language independent, which means you do not have to know a particular programming language to understand how it works.

Page 30: Problem Solving for Programming Session 2 Languages of Description and Problem Solving Strategies.

Algorithms and pseudo code

1. Request input ;

2. Check input valid ;

3. IF (input valid) 3.1 persons input ;

ENDIF

4. handshakes = ((persons * persons)- persons)/2 ;

5. Display handshakes ;

Task number

Line terminator

Tasks or steps in the algorithm

Page 31: Problem Solving for Programming Session 2 Languages of Description and Problem Solving Strategies.

Sequence

• As we have seen, an algorithm is a basic representation of the steps involved in a problem.

• When we are developing algorithms, it is important that the sequence of the steps is represented correctly. Consider the following example of an ATM:

1. Get password ;

2. Get amountNeeded ;

3. Dispense amountNeeded ;

4. IF (enteredPassword is NOT equal to storedPassword)

4.1 Do not dispense amountNeeded ;

END IF

• What is wrong?

Page 32: Problem Solving for Programming Session 2 Languages of Description and Problem Solving Strategies.

Summary

• We need to represent our problems in the most appropriate language of description.

• We need to select the best problem-solving strategy.

• The difficulty, however, is learning which language of description and problem solving strategy to chose for a particular problem scenario.

• This will come with experience and practice.

Page 33: Problem Solving for Programming Session 2 Languages of Description and Problem Solving Strategies.

Session reading

• Vickers– Chapter 3