CS10 Final Review by Glenn Sugden is licensed under a Creative Commons...

Preview:

Citation preview

1

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

CS10 Final Review

Programming

2

Concept Review

• Loops & Variables

•Conditionals

• Lists

• Algorithms & Complexity

•Concurrency

•Recursion

•Data Structures

•Hash Tables

• Lamdas & HOFs

3

Loops & Variables

Correct?

Multiplying operand1 by operand2

4

Loops & Variables

No! Why?

Multiplying operand1 by operand2

5

Loops & Variables

Uninitialized Variable

Multiplying operand1 by operand2

6

Loops & Variables

Initialized Variable!

Multiplying operand1 by operand2

7

Loops & Variables

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

8

Loops & Variables

Correct now?

Multiplying operand1 by operand2

9

Loops & Variables

What if operand1 is negative? Or a “real”

number like 2.3?

Multiplying operand1 by operand2

10

Loops & Variables

Be sure that yourlooping conditions

are valid!

11

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

numbers.

Correct?

Conditionals

12

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

numbers.

No! Why?

Conditionals

13

Conditionals

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

numbers.

What aboutnumber = 0?

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.

15

Lists

Duplicate wordsbeginning with ‘c’

Does this correctly loop over list?

16

Lists

No! Why?

Duplicate wordsbeginning with ‘c’

17

Lists

Index within conditional!

Duplicate wordsbeginning with ‘c’

18

Duplicate wordsbeginning with ‘c’

Lists

Correct now?

19

Lists

No! Why?

Duplicate wordsbeginning with ‘c’

20

Lists

Off by 1!

Duplicate wordsbeginning with ‘c’

21

Lists

Correct now?

Duplicate wordsbeginning with ‘c’

22

Lists

No! Why?

Duplicate wordsbeginning with ‘c’

23

Duplicate wordsbeginning with ‘c’

Lists

The list keeps changing size!

24

Lists

The list keeps changing size!

Duplicate wordsbeginning with ‘c’

25

Lists

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

26

Duplicate wordsbeginning with ‘c’

Lists

Push the index past the inserted item...

27

Lists

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

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

28

Duplicate wordsbeginning with ‘c’

Lists

Make a new, resulting list!

29

Lists

Much better! And our original list is

still intact!

Duplicate wordsbeginning with ‘c’

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

31

Algorithms & Complexity

Order of growth?

32

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

1000 times.

Algorithms & Complexity

33

Algorithms & Complexity

Order of growth?

34

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

list is, the loop always repeats 1000 times.

Algorithms & Complexity

35

Algorithms & Complexity

Order of growth?

36

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

(n)

Algorithms & Complexity

37

Algorithms & Complexity

Order of growth?

38

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

(length)

Algorithms & Complexity

39

Algorithms & Complexity

Order of growth?

40

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

Algorithms & Complexity

Quadratic: O(n2)

41

Algorithms & Complexity

Quadratic: O(n2)

Input size

# of operations

42

Algorithms & Complexity

Order of growth?

43

Algorithms & Complexity

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

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

44

Algorithms & Complexity

Order of growth?

45

Algorithms & Complexity

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

Exponential: O(cn)

46

Algorithms & Complexity

Exponential: O(2n)

Input size

# of operations

47

Algorithms & Complexity

Order of growth?

48

Algorithms & Complexity

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

(square root) than “n”

Logarithmic: O(Logcn)

49

# of operations

Algorithms & Complexity

2

Input size

Note that

these have

traded places!

Logarithmic: O(Log n)

50

Algorithms & Complexity

2

2

Compare!

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

51

Consider the number of times the calculation repeats, rather than

specific inputs.

Algorithms & Complexity

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

53

Concurrency

List adds upto 200?

53

54

Concurrency

List adds upto 200?

No! Why?

54

55

Concurrency

List adds upto 200?

No! Why?

These might choose

the same number at the same

time!

55

56

Concurrency

List adds upto 200?

Anything else?

56

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

58

Recursion

n=0

n=1

59

n=4

n=∞

n=2

n=3

Recursion

60

n=4

n=∞

n=2

n=3

Recursion

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

62

Data Structure : Hash Tables

Can we use “MapReduce” to build a

hash table?

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

63

Lambdas & HOFs

•What is a Lambda?

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

64

Lambdas & HOFs

•A “First Class” Procedure

•“Function as Data”

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

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

67

Data Structure : Hash Tables

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

69

Data Structure : HOF/Hash Tables

Can we use “MapReduce” to

build a hash table?

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

Recommended