cs10 final review by glenn sugden is licensed under a creative commons...

70
1 CS10 Final Review by Glenn Sugden is licensed under a Creative Com mons Attribution-NonCommercial-ShareAlike 3.0 Unported License . CS10 Final Review Programming

Upload: deborah-york

Post on 11-Jan-2016

220 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: CS10 Final Review by Glenn Sugden is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.Glenn Sugdenmons Attribution-NonCommercial-ShareAlike

1

CS10 Final Review by Glenn Sugden is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

CS10 Final Review

Programming

Page 2: CS10 Final Review by Glenn Sugden is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.Glenn Sugdenmons Attribution-NonCommercial-ShareAlike

2

Concept Review

• Loops & Variables

•Conditionals

• Lists

• Algorithms & Complexity

•Concurrency

•Recursion

•Data Structures

•Hash Tables

• Lamdas & HOFs

Page 3: CS10 Final Review by Glenn Sugden is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.Glenn Sugdenmons Attribution-NonCommercial-ShareAlike

3

Loops & Variables

Correct?

Multiplying operand1 by operand2

Page 4: CS10 Final Review by Glenn Sugden is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.Glenn Sugdenmons Attribution-NonCommercial-ShareAlike

4

Loops & Variables

No! Why?

Multiplying operand1 by operand2

Page 5: CS10 Final Review by Glenn Sugden is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.Glenn Sugdenmons Attribution-NonCommercial-ShareAlike

5

Loops & Variables

Uninitialized Variable

Multiplying operand1 by operand2

Page 6: CS10 Final Review by Glenn Sugden is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.Glenn Sugdenmons Attribution-NonCommercial-ShareAlike

6

Loops & Variables

Initialized Variable!

Multiplying operand1 by operand2

Page 7: CS10 Final Review by Glenn Sugden is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.Glenn Sugdenmons Attribution-NonCommercial-ShareAlike

7

Loops & Variables

Be sure that yourvariables are initialized to a correct / known / sane value!

Page 8: CS10 Final Review by Glenn Sugden is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.Glenn Sugdenmons Attribution-NonCommercial-ShareAlike

8

Loops & Variables

Correct now?

Multiplying operand1 by operand2

Page 9: CS10 Final Review by Glenn Sugden is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.Glenn Sugdenmons Attribution-NonCommercial-ShareAlike

9

Loops & Variables

What if operand1 is negative? Or a “real”

number like 2.3?

Multiplying operand1 by operand2

Page 10: CS10 Final Review by Glenn Sugden is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.Glenn Sugdenmons Attribution-NonCommercial-ShareAlike

10

Loops & Variables

Be sure that yourlooping conditions

are valid!

Page 11: CS10 Final Review by Glenn Sugden is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.Glenn Sugdenmons Attribution-NonCommercial-ShareAlike

11

Reports “+” for positive numbers, “-” for negative

numbers.

Correct?

Conditionals

Page 12: CS10 Final Review by Glenn Sugden is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.Glenn Sugdenmons Attribution-NonCommercial-ShareAlike

12

Reports “+” for positive numbers, “-” for negative

numbers.

No! Why?

Conditionals

Page 13: CS10 Final Review by Glenn Sugden is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.Glenn Sugdenmons Attribution-NonCommercial-ShareAlike

13

Conditionals

Reports “+” for positive numbers, “-” for negative

numbers.

What aboutnumber = 0?

Page 14: CS10 Final Review by Glenn Sugden is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.Glenn Sugdenmons Attribution-NonCommercial-ShareAlike

14

Conditionals

Loops & Variables

•Be sure that your conditionals handle all possible cases!

•Double-check edge cases, or inputs that fall on either side of your predicate.

Page 15: CS10 Final Review by Glenn Sugden is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.Glenn Sugdenmons Attribution-NonCommercial-ShareAlike

15

Lists

Duplicate wordsbeginning with ‘c’

Does this correctly loop over list?

Page 16: CS10 Final Review by Glenn Sugden is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.Glenn Sugdenmons Attribution-NonCommercial-ShareAlike

16

Lists

No! Why?

Duplicate wordsbeginning with ‘c’

Page 17: CS10 Final Review by Glenn Sugden is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.Glenn Sugdenmons Attribution-NonCommercial-ShareAlike

17

Lists

Index within conditional!

Duplicate wordsbeginning with ‘c’

Page 18: CS10 Final Review by Glenn Sugden is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.Glenn Sugdenmons Attribution-NonCommercial-ShareAlike

18

Duplicate wordsbeginning with ‘c’

Lists

Correct now?

Page 19: CS10 Final Review by Glenn Sugden is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.Glenn Sugdenmons Attribution-NonCommercial-ShareAlike

19

Lists

No! Why?

Duplicate wordsbeginning with ‘c’

Page 20: CS10 Final Review by Glenn Sugden is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.Glenn Sugdenmons Attribution-NonCommercial-ShareAlike

20

Lists

Off by 1!

Duplicate wordsbeginning with ‘c’

Page 21: CS10 Final Review by Glenn Sugden is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.Glenn Sugdenmons Attribution-NonCommercial-ShareAlike

21

Lists

Correct now?

Duplicate wordsbeginning with ‘c’

Page 22: CS10 Final Review by Glenn Sugden is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.Glenn Sugdenmons Attribution-NonCommercial-ShareAlike

22

Lists

No! Why?

Duplicate wordsbeginning with ‘c’

Page 23: CS10 Final Review by Glenn Sugden is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.Glenn Sugdenmons Attribution-NonCommercial-ShareAlike

23

Duplicate wordsbeginning with ‘c’

Lists

The list keeps changing size!

Page 24: CS10 Final Review by Glenn Sugden is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.Glenn Sugdenmons Attribution-NonCommercial-ShareAlike

24

Lists

The list keeps changing size!

Duplicate wordsbeginning with ‘c’

Page 25: CS10 Final Review by Glenn Sugden is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.Glenn Sugdenmons Attribution-NonCommercial-ShareAlike

25

Lists

How do you correct it?Here is one solution...

Page 26: CS10 Final Review by Glenn Sugden is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.Glenn Sugdenmons Attribution-NonCommercial-ShareAlike

26

Duplicate wordsbeginning with ‘c’

Lists

Push the index past the inserted item...

Page 27: CS10 Final Review by Glenn Sugden is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.Glenn Sugdenmons Attribution-NonCommercial-ShareAlike

27

Lists

Seems pretty “kludgy” though...Here is a much, much better solution...

http://en.wikipedia.org/wiki/Kludge

Page 28: CS10 Final Review by Glenn Sugden is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.Glenn Sugdenmons Attribution-NonCommercial-ShareAlike

28

Duplicate wordsbeginning with ‘c’

Lists

Make a new, resulting list!

Page 29: CS10 Final Review by Glenn Sugden is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.Glenn Sugdenmons Attribution-NonCommercial-ShareAlike

29

Lists

Much better! And our original list is

still intact!

Duplicate wordsbeginning with ‘c’

Page 30: CS10 Final Review by Glenn Sugden is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.Glenn Sugdenmons Attribution-NonCommercial-ShareAlike

30

•Be sure indexes are correct during the entire loop.

•BYOB starts list indexing at 1

•If the input list changes size during the loop, your index will be off!

Check-Lists

Page 31: CS10 Final Review by Glenn Sugden is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.Glenn Sugdenmons Attribution-NonCommercial-ShareAlike

31

Algorithms & Complexity

Order of growth?

Page 32: CS10 Final Review by Glenn Sugden is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.Glenn Sugdenmons Attribution-NonCommercial-ShareAlike

32

Constant: O(c)Reason: No matter what “n” is, the loop always repeats

1000 times.

Algorithms & Complexity

Page 33: CS10 Final Review by Glenn Sugden is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.Glenn Sugdenmons Attribution-NonCommercial-ShareAlike

33

Algorithms & Complexity

Order of growth?

Page 34: CS10 Final Review by Glenn Sugden is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.Glenn Sugdenmons Attribution-NonCommercial-ShareAlike

34

Still Constant: O(c)Reason: No matter what the

list is, the loop always repeats 1000 times.

Algorithms & Complexity

Page 35: CS10 Final Review by Glenn Sugden is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.Glenn Sugdenmons Attribution-NonCommercial-ShareAlike

35

Algorithms & Complexity

Order of growth?

Page 36: CS10 Final Review by Glenn Sugden is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.Glenn Sugdenmons Attribution-NonCommercial-ShareAlike

36

Linear: O(n)Reason: Number of operations proportional to the input size

(n)

Algorithms & Complexity

Page 37: CS10 Final Review by Glenn Sugden is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.Glenn Sugdenmons Attribution-NonCommercial-ShareAlike

37

Algorithms & Complexity

Order of growth?

Page 38: CS10 Final Review by Glenn Sugden is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.Glenn Sugdenmons Attribution-NonCommercial-ShareAlike

38

Still Linear: O(n)Reason: Number of operations proportional to the input size

(length)

Algorithms & Complexity

Page 39: CS10 Final Review by Glenn Sugden is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.Glenn Sugdenmons Attribution-NonCommercial-ShareAlike

39

Algorithms & Complexity

Order of growth?

Page 40: CS10 Final Review by Glenn Sugden is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.Glenn Sugdenmons Attribution-NonCommercial-ShareAlike

40

Reason: Number of operations proportional to the square of the size of the input data (n)

Algorithms & Complexity

Quadratic: O(n2)

Page 41: CS10 Final Review by Glenn Sugden is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.Glenn Sugdenmons Attribution-NonCommercial-ShareAlike

41

Algorithms & Complexity

Quadratic: O(n2)

Input size

# of operations

Page 42: CS10 Final Review by Glenn Sugden is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.Glenn Sugdenmons Attribution-NonCommercial-ShareAlike

42

Algorithms & Complexity

Order of growth?

Page 43: CS10 Final Review by Glenn Sugden is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.Glenn Sugdenmons Attribution-NonCommercial-ShareAlike

43

Algorithms & Complexity

Still Quadratic: O(n2)Reason: Number of operations proportional to

the square of the size of the input data (length)

Page 44: CS10 Final Review by Glenn Sugden is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.Glenn Sugdenmons Attribution-NonCommercial-ShareAlike

44

Algorithms & Complexity

Order of growth?

Page 45: CS10 Final Review by Glenn Sugden is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.Glenn Sugdenmons Attribution-NonCommercial-ShareAlike

45

Algorithms & Complexity

Reason: the recursive call is run twice for each value of n.

Exponential: O(cn)

Page 46: CS10 Final Review by Glenn Sugden is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.Glenn Sugdenmons Attribution-NonCommercial-ShareAlike

46

Algorithms & Complexity

Exponential: O(2n)

Input size

# of operations

Page 47: CS10 Final Review by Glenn Sugden is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.Glenn Sugdenmons Attribution-NonCommercial-ShareAlike

47

Algorithms & Complexity

Order of growth?

Page 48: CS10 Final Review by Glenn Sugden is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.Glenn Sugdenmons Attribution-NonCommercial-ShareAlike

48

Algorithms & Complexity

Reason: the number of times the loop runs grows far more slowly

(square root) than “n”

Logarithmic: O(Logcn)

Page 49: CS10 Final Review by Glenn Sugden is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.Glenn Sugdenmons Attribution-NonCommercial-ShareAlike

49

# of operations

Algorithms & Complexity

2

Input size

Note that

these have

traded places!

Logarithmic: O(Log n)

Page 50: CS10 Final Review by Glenn Sugden is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.Glenn Sugdenmons Attribution-NonCommercial-ShareAlike

50

Algorithms & Complexity

2

2

Compare!

Note that linear growth doesn’t even register on this graph!

Page 51: CS10 Final Review by Glenn Sugden is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.Glenn Sugdenmons Attribution-NonCommercial-ShareAlike

51

Consider the number of times the calculation repeats, rather than

specific inputs.

Algorithms & Complexity

Page 52: CS10 Final Review by Glenn Sugden is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.Glenn Sugdenmons Attribution-NonCommercial-ShareAlike

52

Algorithms & ComplexityExamples

Constant

Linear

Quadratic

Exponential

Logarithmic

Hash Table Lookup

Text or List Length

Square Matrix

Calcs.(Naïve) Sorting

Recursive Fractals

Binary Searches

Page 53: CS10 Final Review by Glenn Sugden is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.Glenn Sugdenmons Attribution-NonCommercial-ShareAlike

53

Concurrency

List adds upto 200?

53

Page 54: CS10 Final Review by Glenn Sugden is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.Glenn Sugdenmons Attribution-NonCommercial-ShareAlike

54

Concurrency

List adds upto 200?

No! Why?

54

Page 55: CS10 Final Review by Glenn Sugden is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.Glenn Sugdenmons Attribution-NonCommercial-ShareAlike

55

Concurrency

List adds upto 200?

No! Why?

These might choose

the same number at the same

time!

55

Page 56: CS10 Final Review by Glenn Sugden is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.Glenn Sugdenmons Attribution-NonCommercial-ShareAlike

56

Concurrency

List adds upto 200?

Anything else?

56

Page 57: CS10 Final Review by Glenn Sugden is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.Glenn Sugdenmons Attribution-NonCommercial-ShareAlike

57

No “wait” here means these might access

the same indexes (or not access them at

all), by interrupting a

broadcast script that is already

running!

Concurrency

List adds upto 200?

No! Why?

57

Page 58: CS10 Final Review by Glenn Sugden is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.Glenn Sugdenmons Attribution-NonCommercial-ShareAlike

58

Recursion

n=0

n=1

Page 59: CS10 Final Review by Glenn Sugden is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.Glenn Sugdenmons Attribution-NonCommercial-ShareAlike

59

n=4

n=∞

n=2

n=3

Recursion

Page 60: CS10 Final Review by Glenn Sugden is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.Glenn Sugdenmons Attribution-NonCommercial-ShareAlike

60

n=4

n=∞

n=2

n=3

Recursion

Page 61: CS10 Final Review by Glenn Sugden is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.Glenn Sugdenmons Attribution-NonCommercial-ShareAlike

61

RecursionAn algorithmic technique where a function, in order to accomplish a task, calls itself with some part of the task.

•Recursive solutions involve two major parts:1.Base case(s), in which the problem is simple enough to be solved directly,2.Recursive case(s). A recursive case has three components:

1.Divide the problem into one or more simpler or smaller parts of the problems,

2.Invoke the function (recursively) on each part, and3.Combine the solutions of the parts into a solution for the

problem.

•Depending on the problem, any of these may be trivial or complex.

http://inst.eecs/~cs3l/sp09/lectures/L06/2009SpCS3L06.html

Page 62: CS10 Final Review by Glenn Sugden is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.Glenn Sugdenmons Attribution-NonCommercial-ShareAlike

62

Data Structure : Hash Tables

Can we use “MapReduce” to build a

hash table?

(we’ll come back to that...)

Page 63: CS10 Final Review by Glenn Sugden is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.Glenn Sugdenmons Attribution-NonCommercial-ShareAlike

63

Lambdas & HOFs

•What is a Lambda?

•What is a Higher-Order Function (HOF)?

Page 64: CS10 Final Review by Glenn Sugden is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.Glenn Sugdenmons Attribution-NonCommercial-ShareAlike

64

Lambdas & HOFs

•A “First Class” Procedure

•“Function as Data”

Page 65: CS10 Final Review by Glenn Sugden is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.Glenn Sugdenmons Attribution-NonCommercial-ShareAlike

65

Lambdas & HOFs•What is a Higher-Order Function

(HOF)?

•In mathematics and computer science, higher-order functions, functional forms, or functionals are functions which do at least one of the following:

■ take one or more functions as an input■ output a function

http://en.wikipedia.org/wiki/Higher-order_function

Page 66: CS10 Final Review by Glenn Sugden is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.Glenn Sugdenmons Attribution-NonCommercial-ShareAlike

66

Lambdas & HOFs Useful HOFs (you can build your own!) map Reporter over List

Report a new list, every element E of List becoming Reporter(E)

keep items such that Predicate from ListReport a new list, keeping only elements E of List if Predicate(E)

combine with Reporter over ListCombine all the elements of List with Reporter(E)This is also known as “reduce”

Acronym example keep map combine

http://inst.eecs.berkeley.edu/~cs10/sp11/lec/17/src/2011-03-30-CS10-L17-DG-HOF-I.pptx

Page 67: CS10 Final Review by Glenn Sugden is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.Glenn Sugdenmons Attribution-NonCommercial-ShareAlike

67

Data Structure : Hash Tables

Page 68: CS10 Final Review by Glenn Sugden is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.Glenn Sugdenmons Attribution-NonCommercial-ShareAlike

68

Data Structure : Hash Tables

•Determine size of hash table.

•Hashing function (algorithm) to generate keys (index) from values (items).

•Modulo key (index) by hash table size.

•Store key and value in hash table at the resulting index.

•Find key in table using above algorithms (reading value instead of storing it).

http://en.wikipedia.org/wiki/Hash_table

Page 69: CS10 Final Review by Glenn Sugden is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.Glenn Sugdenmons Attribution-NonCommercial-ShareAlike

69

Data Structure : HOF/Hash Tables

Can we use “MapReduce” to

build a hash table?

Page 70: CS10 Final Review by Glenn Sugden is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.Glenn Sugdenmons Attribution-NonCommercial-ShareAlike

70

•Try to program one on your own... it’s great practice with HOFs, Lists, etc.!

•I will post the solution to Piazzza...

Yes!

Data Structure : HOF/Hash Tables