quiz: what does this program a: .block 2 do? · – the divide-and-conquer approach can be applied...

Post on 12-Apr-2020

3 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

QUIZ: What does this program do?

1

BR maina: .BLOCK 2main: DECI a, d

LDA a, dSUBA 0x002A, iBREQ yesDECO a, d

fin: STOPyes: CHARO 0x0065, i

CHARO 0x0071, iCHARO 0x0075, iCHARO 0x0061, iCHARO 0x006C, iSTOP.END

Solution

2

The hex ASCII codes are for the letters e, q, u, a and l.

If the number entered is not42, the number itself is output, otherwise the message equal is output.

Note: Instead of SUBA, the instruction CPA can be used – do you see why?

QUIZ on Ch.6:Write the pseudocode for adding two numbers

together; our target language is PEP/8 assembly.

3

4

•Input the first number - into memory location "A" •Input the second number - into memory location "B" •Load A from memory into Accumulator reg. •Add B from memory; result goes into Accumulator reg. •Store the Accumulator into memory location "C" •Output the value in memory location "C"

Solution

For more practice: Write the assembly code!

Problem Solving

How to Solve It: A New Aspect of Mathematical Method by George Polya, 1945

The book is written within the context of solving mathematical problems, but the methods described are applicable to problem solving in general.

6

We can use the methods described by Polya to solve any computer-related problem!

1. Understand the problem– What are the hypotheses? Data? Unknowns?

2. Devise a plan– Can we find a related problem? A sub-problem?– Can we strengthen or relax the hypotheses to obtain an

easier problem?

3. Carry out the plan— Prove that each step is correct!

4. Look back– Check result– Find shortcuts and alternate solutions– Generalize to related problems

7

Polya’s 4 steps

Polya’s StrategiesDo not reinvent the wheel!Similar problems come up again and again in different guises.A good programmer recognizes a task or subtask that has been solved before and reuses the solution.

Can you think of two similar problems we solved in Python?

8

Finding a character in a string and finding an element in a list

Polya’s StrategiesDivide and Conquer!Break up a large problem into smaller sub-problems

and solve each separately – Applies the concept of abstraction

– The divide-and-conquer approach can be applied over and over again until each subtask is manageable

9

Divide and Conquer!Break up a large problem into smaller sub-problems

and solve each separately

10Image source: http://teamapproach.ca/trouble/Methodology.htm

Polya’s 4 steps can be applied to computerproblem solving!

11

Analysis and Specification PhaseAsk questions to understand all the requirementsExplain in detail what needs to be achieved

Algorithm Development PhaseDevelop algorithmTest algorithm

Implementation PhaseCode algorithmTest the code in various ways

Maintenance PhaseUse the code, find bugsFix bugsCode new features, as requested by users

QUIZ: Match the steps in Polya’s method to the ones in the computer method for problem solving

12

Analysis and Specification

Implementation

Algorithm Development

Maintenance

Devise a plan

Look back

Understand

Carry out the plan

Interactions among the

phases

13

Algorithms

Algorithm = A set of unambiguous steps for solving a problem in a finite amount of time,using a finite amount of resources

14

Algorithm = A set of unambiguous steps for solving a problem in a finite amount of time, using a finite amount of data

15Image source: http://my-online-log.com/tech/archives/1214

Refining the steps, from ambiguous (abstract) to unambiguous (concrete)

Abstract Step An algorithmic step containing unspecified detailsConcrete StepAn algorithm step in which all details are specified

16

Whether the step is abstract or concrete depends on the programming language we’re using!

7.2 Algorithms with simple variables

Variable = a means of storing intermediate results from one task to the next.

At the hardware level, a simple variable is just one or several adjacent Bytes in the computer memory.

Q: How many Bytes does a simple variable have in PEP/8?

17

QUIZWe have two variables a and b.Create an algorithm to swap their values!

18

What is wrong with this algorithm?a = bb = a

19

save = aa = bb = save

SolutionThe algorithm proposed results in both a and b storing the value of b!The correct algorithm involves three steps and an extra variable:

Source: AP CS Principles – Course and Exam Descriptions

Similar problem (Do not reinvent the wheel!)

Algorithms with Selection Statements (a.k.a. decision or if)

21

Flowchart of if statement

Figure is not in text

22

Algorithm with SelectionProblem: Write the appropriate dress for a given temperature.

Write "Enter temperature"Read temperatureDetermine Dress

Which statements are concrete?Which statements are abstract?

Algorithm Determine Dress v.1

Computer language is Python from now on!

23

Write “Enter temperature”Read temperatureIF (temperature > 90)

Write “Texas weather: wear shorts”ELSE IF (temperature > 70)

Write “Ideal weather: short sleeves are fine”ELSE IF (temperature > 50)

Write “A little chilly: wear a light jacket”ELSE IF (temperature > 32)

Write “Philadelphia weather: wear a heavy coat”ELSE

Write “Stay inside”

Algorithm Determine Dress v.2

Is this concrete enough for implementation in Python?

Algorithms with Loops (a.k.a. repetition)

24

Flow of control of while loop (decision at beginning of loop)

Figure is not in text

QUIZ: There are loops that can execute 0 times and loops that must execute at least 1 time.Which type is this?

25

Answer: It depends on whether the decision (diamond) is at the beginning or at the end of the loop!

26

Figure is not in text

Loops in Python

27

Not in text

Extra-credit question:

28

Event-controlled Loops, a.k.a. WHILE loops

29

They are the most general type of loops!

Set sum to 0Set allPositive to trueWHILE (allPositive)

Read numberIF (number > 0)

Set sum to sum + numberELSE

Set allPositive to falseWrite "Sum is " + sum

Counter-controlled Loops

30

They are a particular case of event-controlled loops: the event is that a counter variable has reached a predetermined limit

Set sum to 0Set limit to 42Set count to 1While (count <= limit)

Read numberSet sum to sum + numberIncrement count

Write "Sum is " + sum

For loops are counter-

controlled!

31

Important application of looping: Successive approximation algorithms

Read in squareCalculate the square rootWrite out square and the square root

Algorithm Calculate Square Root v.1

Which steps are abstract and which concrete?

32

Set epsilon to 1WHILE (epsilon > 0.001)

Calculate new guessSet epsilon to abs(square - guess * guess)

Which steps are abstract and which concrete?

Algorithm Calculate Square Root v.2

In Python usemath.fabs(…)

A more appropriate name for guess would be approximation

33

Set newGuess to(guess + (square/guess)) / 2.0

Set guess to square/4Set epsilon to 1

What’s the mathematical formula for the new approximation?

How do we get the loop started?

Algorithm Calculate Square Root - Refinements in v.2

34

Read in squareSet guess to square/4Set epsilon to 1WHILE (epsilon > 0.001)

Set guess to (guess + (square/guess)) / 2.0Set epsilon to abs(square - guess * guess)

Write out square and guess

Which steps are abstract and which concrete?

Algorithm Calculate Square Root v.3

35

Set newGuess to(guess + (square/guess)) / 2.0

QUIZ: Square root approximation alg.

We want to calculate = 2.236…

Set your initial guess x0 = 1 and show the next 3 approximations x1, x2, x3

5

36

SolutionWe want to calculate = 2.236…5

37

Set newGuess to(guess + (square/guess)) / 2.0

Quick work for next time (in notebook):

We want to calculate = 2.236…

Set your initial guess x0 = 42 and show the next 3 approximations x1, x2, x3

5

38

QUIZ: Square root algorithm

We want to calculate = 2.645751…

Set your initial guess to x0 = 7/4 = 1.75

and show the next 3 approximations x1, x2, x3

7

39

QUIZ: Square root algorithm

We want to calculate = 2.645751…7

40

QUIZ: Square root algorithm

We want to calculate = 2.645751…7

41

QUIZ: Square root algorithm

We want to calculate = 2.645751…7

42

Remember: The algorithm converges irrespective of the initial guess x0!

= 2.645751…7

43

QUIZ:

Re-write this program using a for loop!

Solution

Problem for lab:

Re-write this program using a while loop!

Hint: What condition should we use to control the loop?

EoHF 1

QUIZ True/FalseA. Count-controlled loops repeat a specific task a

number of times.B. Event-controlled loops repeat a specific

number of timesC. Count-controlled loops are controlled by a

counterD. Count-controlled loops are more general than

event-controlled loops

46

47

Set newGuess to(guess + (square/guess)) / 2.0

QUIZ: Square root algorithm (approximations)

We want to calculate = 3.16227…

Set your initial guess x0 = 10/4 and show the next 3 approximations x1, x2, x3

10

48

Solution

We want to calculate = 3.16227…

Set your initial guess x0 = 10/4 and show the next 3 approximations x1, x2, x3

10

x0 = 2.5 x1 = 3.25 x2 = 3.16346… x3 = 3.16227…

7.3 Composite Data Types and their Algorithms

They can be classified according to many criteria:

• homogeneous vs. heterogeneous

• accessed by index vs. accessed by name

• mutable vs. immutable

• linear vs. non-linear

• etc.

49

Composite Data Types

RecordsA named heterogeneous collection of items in which individual items are accessed by name. For example, we could bundle name, age and hourly wage items into a record named Employee

ArraysA named homogeneous collection of items in which an individual item is accessed by its position (index) within the collection

50

Are these the lists from Python? Why not?

Python strings are arrays of characters

Composite Data Types (contd.)

Lists (will be covered in next chapter)A named heterogeneous collection of items in which individual items are accessed by position (index).

We have them in Python, e.g.>>> myList = [“dog”, 42, 51.375, [1,2]]>>> myList[1]42

51

Composite Data Types - RecordsEmployee

nameagehourly/Wage

Algorithm to store values into the fields of record:

Employee employee // Declare an Employee variableSet employee.name to “Frank Jones”Set employee.age to 32Set employee.hourlyWage to 27.50

52

Composite Data Types - Arrays

53

numbers[0]

numbers[4]

numbers

Some items in an array may be unused at a given time

54

first

last

numbers

??

??

??

??

Useful Algorithms on Arrays

• Initializing all items• Printing all items• Searching for an item• Sorting the array

55

56

Algorithm for initializing an array

integer data[20]Write “How many values?”Read lengthSet index to 0WHILE (index < length)

Read data[index]Set index to index + 1

Task: Fill an array numbers with length values that are being input from the keyboard.

57

QUIZ

integer data[20]Write “How many values?”Read lengthSet index to 0WHILE (index < length)

Read data[index]Set index to index + 1

Modify this pseudocode to print the values after initializing them.

An Unsorted

Array

58

data

A Sorted Array

59

data

Sorted Arrays

• The values stored in an array have unique keysof a type for which the relational operators are defined.

• Sorting rearranges the elements into either ascending or descending order within the array.

60

Reality check: In a real-life problem it’s very common

to encounter repeated keys!

7.4 Search algorithms

61

Sequential Search in anUnsorted Array

A sequential search examines each item in turn and compares it to the one we are searching for.

If it matches, we have found the item. If not, we examine the next item in the array.

We stop either when we have found the item or when we have looked at all the items and not found a match.

Thus, we have a loop with two ending conditions.

62

Set position to 0Set found to FALSEWHILE (position < length AND NOT found )

IF (numbers[position] equals searchItem)Set found to TRUE

ELSE Set position to position + 1

63

The array’s name is numbersThe value we’re searching for is stored in searchItem

Set position to 0Set found to FALSEWHILE (position < length AND NOT found )

IF (numbers[position] equals searchItem)Set found to TRUE

ELSE Set position to position + 1

64

QUIZ: When the loop exits, what do we need to do?

Set index to 0Set found to FALSEWHILE (index < length AND NOT found )

IF (data[index] equals searchItem)Set found to TRUE

ELSE Set index to index + 1

65

QUIZ: Desk-check this algorithm for the arrayThe item searched is:A. 35B. 43

42100351

Sequential Search in a Sorted ArrayIdea:If items in an array are sorted, we can stop

looking when we pass the place where the item would be if it were present in the array

66

Is this (always) better?

67

Sequential Search in a Sorted ArraySet index to 0Set found to FALSEWHILE (index < length AND NOT found)

IF (data[index] equals searchItem)Set found to TRUE

ELSE IF (data[index] > searchItem)Set index to length

ELSESet index to index + 1

This is the new part!

(Compare with previous alg. for unsorted array)

This alg. is called sequential search with early termination

QUIZ: End-of-chapter question 66 a

68

SearchItem = 4SearchItem = 49SearchItem = 50

Binary Search in Sorted Array

Search begins at the middle and finds the item or eliminates half of the unexamined items; the process is then repeated on the half where the item might be

69

24 30 31 42 44 90 92 94 99

Example: searchItem = 42

Binary Search

Algorithm

70

Set first to 0Set last to length-1Set found to FALSEWHILE (first <= last AND NOT found)

Set middle to (first + last)/ 2IF (item equals data[middle]))

Set found to TRUEELSE

IF (item < data[middle])Set last to middle – 1

ELSESet first to middle + 1

RETURN found

Grok why integerdivision works

here!

Binary Search example

71Figure 7.10 Trace of the binary search

rat

QUIZ Binary Search

72

Search for deer

Search for the key 42 in this array, using binary search:

73

Trick QUIZ

EoHW 2

0] Define arrays

1] Define records

2] How are arrays and records:

(a) similar?

(b) different?

74

QUIZ

3] What are the 4 fundamental types of algorithms used to manipulate arrays?

4] What control structure is normally used to access all the elements of an array?

75

QUIZ

5] Explain the following search algorithms in your

own words:

(a) Sequential search

(b) Sequential search with early termination

(c) Binary/logarithmic search

76

QUIZ

QUIZ Binary Search

-50 -28 -23 -10 -4 0 3 41 28 37 60 155 200

How many comparisons are needed to find the key 60 in this array?Show first, last, and middle for each iteration.

7.5 Sorting algorithms

78

Sorting

SortingArranging items in a collection so that there is an ordering on one (or more) of the fields in the items

Sort KeyThe field (or fields) on which the ordering is based

Sorting algorithmsAlgorithms that order the items in the collection based on the sort key

79

Selection Sort

80

Figure 7.11 Example of a selection sort (sorted elements are shaded)

81

10010111235200132

Show the swapped elements with arrows.Show the sorted elements with shading.

QUIZ Selection SortHandout at the end

Solution

82

83

24101112352012

Show the swapped elements with arrows.Show the sorted elements with shading.

Quick work for next timeHandout at the end

84

Solution

85

Selection Sort PseudocodeSelection SortSet firstUnsorted to 0WHILE (not sorted yet)

Find smallest unsorted itemSwap firstUnsorted item with the smallestSet firstUnsorted to firstUnsorted + 1

Not sorted yetcurrent < length – 1

Red steps are abstract (not concrete)

86

Find smallest unsorted itemSet indexOfSmallest to firstUnsortedSet index to firstUnsorted + 1WHILE (index <= length – 1)

IF (data[index] < data[indexOfSmallest])Set indexOfSmallest to index

Set index to index + 1Set index to indexOfSmallest

Swap firstUnsorted with smallestSet tempItem to data[firstUnsorted]Set data[firstUnsorted] to data[indexOfSmallest]Set data[indexOfSmallest] to tempItem

87

SKIP Bubble Sort and Insertion Sort, just remember that they are as fast

(efficient) as Selection Sort

88

Is it possible to devise a more efficient sorting algorithm?

89

Is it possible to devise a more efficient sorting algorithm?

Yes, but, in order to do this, we have to understand subprograms and

recursion.

7.6 Recursive Algorithms

To understand Quick Sort, we need:

• a new control structure: subprogram

• a new concept: recursion

90

Subprogram StatementsWe can give a section of code a name and use that name as a statement in another part of the programWhen the name is encountered, the processing in the other part of the program halts while the named code is executedWhen execution is finished, the first part of the program resumes execution

That section of code is called a subprogram

91

92

Figure 7.14 Subprogram flow of control

We have already used subprograms in Python!

Ee called them

• Functions– int() float() ord() chr() str() etc.

• Methods– list.append() string.upper() string.find() etc.

93

Do you remember what each of them

does?

Input to SubprogramsWhat if the subprogram needs data from the calling unit? This data is called input.

ParametersIdentifiers listed in parentheses beside the subprogram declaration; sometimes called formal parametersArgumentsIdentifiers listed in parentheses on the subprogram call; sometimes called actual parameters

94

Formal and actual params in Python

def disp(a, b):print a, 'and', b

x, y = 1, 2disp(x, y)

95

What if the subprogram needs to give data back to the calling unit? This data is called output.

Void subprograms They do not return a value, just perform certain actions>>> print(“Enter a positive integer”)>>> printer(3, 4)

Value-returning subprograms >>> a = input(“Enter a positive integer”)The keyword RETURN is used in many programming languages

96

What if the subprogram needs to give data back to the calling unit? This data is called output.

Void subprograms They do not return a value, just perform certain actions>>> print(“Enter a positive integer”)>>> printer(3, 4)

Value-returning subprograms >>> a = input(“Enter a positive integer”)The keyword RETURN is used in many programming languages

97

Do not confuse returning values with printing or displaying values. Returning means that a value is

given back to the main program!

Value-returning function in Python

def sum(a, b):return a + b

x, y = 1, 2print sum(x, y)

98

Compare to this previous function, which returns void (does not return anything)

Value-returning vs. void Subprograms

99

Source: AP CS Principles –Course and Exam Descriptions

QUIZIs MoveAndTurn a value-returning or void subprogram?

Subprograms are very important tools for abstraction

Other popular names for subprograms:• sub• subroutine• function• procedure• module• method

101

Recursion RecursionThe ability of a subprogram to call itselfBase caseThe case to which we have an answer General caseThe case that expresses the solution in terms of a call to itself with a smaller version of the problem

102

Recursion – Factorial function The factorial of a positive integer is defined (non-recursively) as the integer times the product of all the integers between itself and 1:

N! = 1⋅2⋅3⋅ … ⋅NBut an alternate recursive definition is possible:

N! = N⋅(N − 1)!

103

Two definitions for the factorial Iterative: N! = 1⋅2⋅3⋅ … ⋅NRecursive: N! = N⋅(N − 1)!

Base caseFact(0) = 1 (0! is 1)

General CaseFact(N) = N ⋅ Fact(N-1) (for N ≥ 1)

104

Is this a valid recursion?

Source: http://www.urbandictionary.com/define.php?term=recursion105

Base caseFact(1) = 1

General CaseFact(N) = N ⋅ Fact(N-1)

106

How does the genie calculate Fact(3)?

Your turn!N! = N * (N − 1)!Base case

Facto(0) = 1 (0! is 1)General Case

Fact(N) = N * Fact(N-1) (for N ≥ 1)

Calculate:0! = 1! = 2! = 5! =

107

108

Recursive Factorial algorithm

Write “Enter n”Read nSet result to Factorial(n)Write result + “ is the factorial of “ + n

Factorial(n)IF (n equals 0)

RETURN 1ELSE

RETURN n * Factorial(n-1)

109

QUIZ: Write this recursive function in Python

Write “Enter n”Read nSet result to Factorial(n)Write result + “ is the factorial of “ + n

Factorial(n)IF (n equals 0)

RETURN 1ELSE

RETURN n * Factorial(n-1)

solution

110

111

Recursive Binary Search BinarySearch (first, last)IF (first > last)

RETURN FALSEELSE

Set middle to (first + last)/ 2IF (item equals data[middle])

RETURN TRUEELSE

IF (item < data[middle])BinarySearch (first, middle – 1)

ELSEBinarySearch (middle + 1, last)

SKIP QUICKSORT …Just remember that it is faster

(more efficient) than Selection Sort

112

SKIP 7.7 Important Threads

Read and take notes: Who am I?

113

I am a mathematician.Why is my picture in abook about computerscience?

Read and take notes:Ethical Issues - Open-Source Software

What are the advantages and disadvantages of open-source software?

What are the FSF and the GPL? Are they related?What does the success of Linux suggest about the

future of open-source software?Is open-source software exempt from copyright laws?

(Hint: What happened in 2008?)

114

Homework, due Tue, Nov.13:

End-of-chapter questions 30, 31, 34, 35, 36, 66 b, c

Not from text: Calculate 7! by hand, showing all the recursive steps, as done in the lecture examples.

115

Chapter review questions• Describe the computer problem-solving process and relate it

to Polya’s How to Solve It list• Distinguish between a simple type and a composite type• Distinguish between a void subprogram and a value-returning

subprogram• Recognize a recursive problem and write a recursive algorithm

to solve it • Distinguish between an unsorted array and a sorted array• Describe the Quicksort algorithm • Apply the linear search, binary search, selection sort and

Quicksort to an array of items by hand• What are the advantages and disadvantages of open-source

software?

116

top related