61A Lecture 11 Friday, September 21 Midterm 1 Recap 2 Midterm 1 - - PowerPoint PPT Presentation

61a lecture 11
SMART_READER_LITE
LIVE PREVIEW

61A Lecture 11 Friday, September 21 Midterm 1 Recap 2 Midterm 1 - - PowerPoint PPT Presentation

61A Lecture 11 Friday, September 21 Midterm 1 Recap 2 Midterm 1 Recap The exam was more difficult than the Fall 2011 Midterm 1 2 Midterm 1 Recap The exam was more difficult than the Fall 2011 Midterm 1 Typically, more than 75% of students


slide-1
SLIDE 1

61A Lecture 11

Friday, September 21

slide-2
SLIDE 2

Midterm 1 Recap

2

slide-3
SLIDE 3

Midterm 1 Recap

The exam was more difficult than the Fall 2011 Midterm 1

2

slide-4
SLIDE 4

Midterm 1 Recap

The exam was more difficult than the Fall 2011 Midterm 1 Typically, more than 75% of students receive A’s & B’s in 61A

2

slide-5
SLIDE 5

Midterm 1 Recap

The exam was more difficult than the Fall 2011 Midterm 1 Typically, more than 75% of students receive A’s & B’s in 61A Problem 4(c): through doesn’t rhyme with cough , and 20 (twenty) doesn’t rhyme with 10 (ten) Sight rhyme: A pair of words that don’t rhyme, but look like they should

2

slide-6
SLIDE 6

Midterm 1 Recap

The exam was more difficult than the Fall 2011 Midterm 1 Typically, more than 75% of students receive A’s & B’s in 61A Problem 4(c): through doesn’t rhyme with cough , and 20 (twenty) doesn’t rhyme with 10 (ten) Sight rhyme: A pair of words that don’t rhyme, but look like they should

2

1X WX vs

slide-7
SLIDE 7

Midterm 1 Recap

The exam was more difficult than the Fall 2011 Midterm 1 Typically, more than 75% of students receive A’s & B’s in 61A Problem 4(c): through doesn’t rhyme with cough , and 20 (twenty) doesn’t rhyme with 10 (ten) Sight rhyme: A pair of words that don’t rhyme, but look like they should

2

1X WX vs

slide-8
SLIDE 8

Midterm 1 Recap

The exam was more difficult than the Fall 2011 Midterm 1 Typically, more than 75% of students receive A’s & B’s in 61A Problem 4(c): through doesn’t rhyme with cough , and 20 (twenty) doesn’t rhyme with 10 (ten) Sight rhyme: A pair of words that don’t rhyme, but look like they should

2

1X WX vs

twelve twenty-two

slide-9
SLIDE 9

Midterm 1 Recap

The exam was more difficult than the Fall 2011 Midterm 1 Typically, more than 75% of students receive A’s & B’s in 61A Problem 4(c): through doesn’t rhyme with cough , and 20 (twenty) doesn’t rhyme with 10 (ten) Sight rhyme: A pair of words that don’t rhyme, but look like they should

2

1X WX vs if first_tens(p)==1: return second_tens(p)!=1

twelve twenty-two

slide-10
SLIDE 10

Midterm 1 Recap

The exam was more difficult than the Fall 2011 Midterm 1 Typically, more than 75% of students receive A’s & B’s in 61A Problem 4(c): through doesn’t rhyme with cough , and 20 (twenty) doesn’t rhyme with 10 (ten) Sight rhyme: A pair of words that don’t rhyme, but look like they should

2

1X WX vs if first_tens(p)==1: return second_tens(p)!=1

twelve twenty-two

slide-11
SLIDE 11

Midterm 1 Recap

The exam was more difficult than the Fall 2011 Midterm 1 Typically, more than 75% of students receive A’s & B’s in 61A Problem 4(c): through doesn’t rhyme with cough , and 20 (twenty) doesn’t rhyme with 10 (ten) Sight rhyme: A pair of words that don’t rhyme, but look like they should

2

1X WX vs if first_tens(p)==1: return second_tens(p)!=1

twelve twenty-two

slide-12
SLIDE 12

Midterm 1 Recap

The exam was more difficult than the Fall 2011 Midterm 1 Typically, more than 75% of students receive A’s & B’s in 61A Problem 4(c): through doesn’t rhyme with cough , and 20 (twenty) doesn’t rhyme with 10 (ten) Sight rhyme: A pair of words that don’t rhyme, but look like they should

2

1X WX vs X0 Y0 vs if first_tens(p)==1: return second_tens(p)!=1

twelve twenty-two

slide-13
SLIDE 13

Midterm 1 Recap

The exam was more difficult than the Fall 2011 Midterm 1 Typically, more than 75% of students receive A’s & B’s in 61A Problem 4(c): through doesn’t rhyme with cough , and 20 (twenty) doesn’t rhyme with 10 (ten) Sight rhyme: A pair of words that don’t rhyme, but look like they should

2

1X WX vs X0 Y0 vs if first_tens(p)==1: return second_tens(p)!=1

twelve twenty-two zero twenty

slide-14
SLIDE 14

Midterm 1 Recap

The exam was more difficult than the Fall 2011 Midterm 1 Typically, more than 75% of students receive A’s & B’s in 61A Problem 4(c): through doesn’t rhyme with cough , and 20 (twenty) doesn’t rhyme with 10 (ten) Sight rhyme: A pair of words that don’t rhyme, but look like they should

2

1X WX vs X0 Y0 vs if first_tens(p)==1: return second_tens(p)!=1

twelve twenty-two zero twenty twenty zero

slide-15
SLIDE 15

Midterm 1 Recap

The exam was more difficult than the Fall 2011 Midterm 1 Typically, more than 75% of students receive A’s & B’s in 61A Problem 4(c): through doesn’t rhyme with cough , and 20 (twenty) doesn’t rhyme with 10 (ten) Sight rhyme: A pair of words that don’t rhyme, but look like they should

2

1X WX vs X0 Y0 vs if first_tens(p)==1: return second_tens(p)!=1

twelve twenty-two zero twenty twenty zero

if first_tens(p)==0: return second_tens(p)!=0 else: return second_tens(p)==0

slide-16
SLIDE 16

Midterm 1 Recap

The exam was more difficult than the Fall 2011 Midterm 1 Typically, more than 75% of students receive A’s & B’s in 61A Problem 4(c): through doesn’t rhyme with cough , and 20 (twenty) doesn’t rhyme with 10 (ten) Sight rhyme: A pair of words that don’t rhyme, but look like they should

2

1X WX vs X0 Y0 vs if first_tens(p)==1: return second_tens(p)!=1

twelve twenty-two zero twenty twenty zero

if first_tens(p)==0: return second_tens(p)!=0 else: return second_tens(p)==0

slide-17
SLIDE 17

Midterm 1 Recap

The exam was more difficult than the Fall 2011 Midterm 1 Typically, more than 75% of students receive A’s & B’s in 61A Problem 4(c): through doesn’t rhyme with cough , and 20 (twenty) doesn’t rhyme with 10 (ten) Sight rhyme: A pair of words that don’t rhyme, but look like they should

2

1X WX vs X0 Y0 vs if first_tens(p)==1: return second_tens(p)!=1

twelve twenty-two zero twenty twenty zero

if first_tens(p)==0: return second_tens(p)!=0 else: return second_tens(p)==0

✓ ✓

slide-18
SLIDE 18

Midterm 1 Recap

The exam was more difficult than the Fall 2011 Midterm 1 Typically, more than 75% of students receive A’s & B’s in 61A Problem 4(c): through doesn’t rhyme with cough , and 20 (twenty) doesn’t rhyme with 10 (ten) Sight rhyme: A pair of words that don’t rhyme, but look like they should

2

1X WX vs X0 Y0 vs if first_tens(p)==1: return second_tens(p)!=1

twelve twenty-two

WX 1X vs

twenty-two twelve zero twenty twenty zero

if first_tens(p)==0: return second_tens(p)!=0 else: return second_tens(p)==0

✓ ✓

slide-19
SLIDE 19

Midterm 1 Recap

The exam was more difficult than the Fall 2011 Midterm 1 Typically, more than 75% of students receive A’s & B’s in 61A Problem 4(c): through doesn’t rhyme with cough , and 20 (twenty) doesn’t rhyme with 10 (ten) Sight rhyme: A pair of words that don’t rhyme, but look like they should

2

1X WX vs X0 Y0 vs if first_tens(p)==1: return second_tens(p)!=1

twelve twenty-two

WX 1X vs

twenty-two twelve zero twenty twenty zero

if first_tens(p)==0: return second_tens(p)!=0 else: return second_tens(p)==0

✓ ✓

else: return second_tens(p)==1

slide-20
SLIDE 20

Midterm 1 Recap

The exam was more difficult than the Fall 2011 Midterm 1 Typically, more than 75% of students receive A’s & B’s in 61A Problem 4(c): through doesn’t rhyme with cough , and 20 (twenty) doesn’t rhyme with 10 (ten) Sight rhyme: A pair of words that don’t rhyme, but look like they should

2

1X WX vs X0 Y0 vs if first_tens(p)==1: return second_tens(p)!=1

twelve twenty-two

WX 1X vs

twenty-two twelve zero twenty twenty zero

if first_tens(p)==0: return second_tens(p)!=0 else: return second_tens(p)==0 “You may not use boolean operator or”

✓ ✓

else: return second_tens(p)==1

slide-21
SLIDE 21

Midterm 1 Recap

The exam was more difficult than the Fall 2011 Midterm 1 Typically, more than 75% of students receive A’s & B’s in 61A Problem 4(c): through doesn’t rhyme with cough , and 20 (twenty) doesn’t rhyme with 10 (ten) Sight rhyme: A pair of words that don’t rhyme, but look like they should

2

1X WX vs X0 Y0 vs if first_tens(p)==1: return second_tens(p)!=1

twelve twenty-two

WX 1X vs

twenty-two twelve zero twenty twenty zero

if first_tens(p)==0: return second_tens(p)!=0 else: return second_tens(p)==0 “You may not use boolean operator or”

✓ ✓

else: return second_tens(p)==1 Demo

slide-22
SLIDE 22

Mapping a Function over a Sequence

3

Apply a function to each element of the sequence

slide-23
SLIDE 23

Mapping a Function over a Sequence

3

>>> alternates = (-1, 2, -3, 4, -5) Apply a function to each element of the sequence

slide-24
SLIDE 24

Mapping a Function over a Sequence

3

>>> alternates = (-1, 2, -3, 4, -5) >>> tuple(map(abs, alternates)) Apply a function to each element of the sequence

slide-25
SLIDE 25

Mapping a Function over a Sequence

3

>>> alternates = (-1, 2, -3, 4, -5) >>> tuple(map(abs, alternates)) (1, 2, 3, 4, 5) Apply a function to each element of the sequence

slide-26
SLIDE 26

Mapping a Function over a Sequence

3

>>> alternates = (-1, 2, -3, 4, -5) >>> tuple(map(abs, alternates)) (1, 2, 3, 4, 5) Apply a function to each element of the sequence The returned value of map is an iterable map object

slide-27
SLIDE 27

Mapping a Function over a Sequence

3

>>> alternates = (-1, 2, -3, 4, -5) >>> tuple(map(abs, alternates)) (1, 2, 3, 4, 5) Apply a function to each element of the sequence The returned value of map is an iterable map object A constructor for the built-in map type

slide-28
SLIDE 28

Mapping a Function over a Sequence

3

>>> alternates = (-1, 2, -3, 4, -5) >>> tuple(map(abs, alternates)) (1, 2, 3, 4, 5) Apply a function to each element of the sequence The returned value of map is an iterable map object A constructor for the built-in map type The returned value of filter is an iterable filter object

slide-29
SLIDE 29

Mapping a Function over a Sequence

3

>>> alternates = (-1, 2, -3, 4, -5) >>> tuple(map(abs, alternates)) (1, 2, 3, 4, 5) Apply a function to each element of the sequence Demo The returned value of map is an iterable map object A constructor for the built-in map type The returned value of filter is an iterable filter object

slide-30
SLIDE 30

Accumulation and Iterable Values

4

slide-31
SLIDE 31

Accumulation and Iterable Values

Iterable objects give access to some elements in order.

4

slide-32
SLIDE 32

Accumulation and Iterable Values

Iterable objects give access to some elements in order. However, you may only be able to access the elements once!

4

slide-33
SLIDE 33

Accumulation and Iterable Values

Iterable objects give access to some elements in order. However, you may only be able to access the elements once! Many built-in functions take iterable objects as argument.

4

slide-34
SLIDE 34

Accumulation and Iterable Values

Iterable objects give access to some elements in order. However, you may only be able to access the elements once! Many built-in functions take iterable objects as argument.

4

tuple Return a tuple containing the elements

slide-35
SLIDE 35

Accumulation and Iterable Values

Iterable objects give access to some elements in order. However, you may only be able to access the elements once! Many built-in functions take iterable objects as argument.

4

tuple Return a tuple containing the elements sum Return the sum of the elements

slide-36
SLIDE 36

Accumulation and Iterable Values

Iterable objects give access to some elements in order. However, you may only be able to access the elements once! Many built-in functions take iterable objects as argument.

4

tuple Return a tuple containing the elements sum Return the sum of the elements min Return the minimum of the elements

slide-37
SLIDE 37

Accumulation and Iterable Values

Iterable objects give access to some elements in order. However, you may only be able to access the elements once! Many built-in functions take iterable objects as argument.

4

tuple Return a tuple containing the elements sum Return the sum of the elements min Return the minimum of the elements max Return the maximum of the elements

slide-38
SLIDE 38

Accumulation and Iterable Values

Iterable objects give access to some elements in order. However, you may only be able to access the elements once! Many built-in functions take iterable objects as argument.

4

tuple Return a tuple containing the elements sum Return the sum of the elements min Return the minimum of the elements max Return the maximum of the elements For statements also operate on iterable values.

slide-39
SLIDE 39

Reducing a Sequence

5

slide-40
SLIDE 40

Reducing a Sequence

Reduce is a higher-order generalization of max, min, & sum.

5

slide-41
SLIDE 41

Reducing a Sequence

Reduce is a higher-order generalization of max, min, & sum.

5

>>> from operator import mul

slide-42
SLIDE 42

Reducing a Sequence

Reduce is a higher-order generalization of max, min, & sum.

5

>>> from operator import mul >>> from functools import reduce

slide-43
SLIDE 43

Reducing a Sequence

Reduce is a higher-order generalization of max, min, & sum.

5

>>> from operator import mul >>> from functools import reduce >>> reduce(mul, (1, 2, 3, 4, 5))

slide-44
SLIDE 44

Reducing a Sequence

Reduce is a higher-order generalization of max, min, & sum.

5

>>> from operator import mul >>> from functools import reduce >>> reduce(mul, (1, 2, 3, 4, 5)) 120

slide-45
SLIDE 45

Reducing a Sequence

Reduce is a higher-order generalization of max, min, & sum.

5

>>> from operator import mul >>> from functools import reduce >>> reduce(mul, (1, 2, 3, 4, 5)) 120 First argument: A two-argument function

slide-46
SLIDE 46

Reducing a Sequence

Reduce is a higher-order generalization of max, min, & sum.

5

>>> from operator import mul >>> from functools import reduce >>> reduce(mul, (1, 2, 3, 4, 5)) 120 First argument: A two-argument function Second argument: an iterable object

slide-47
SLIDE 47

Reducing a Sequence

Reduce is a higher-order generalization of max, min, & sum.

5

>>> from operator import mul >>> from functools import reduce >>> reduce(mul, (1, 2, 3, 4, 5)) 120 Like accumulate from Homework 2, but with iterable objects First argument: A two-argument function Second argument: an iterable object

slide-48
SLIDE 48

Generator Expressions

6

One large expression that evaluates to an iterable object

slide-49
SLIDE 49

Generator Expressions

(<map exp> for <name> in <iter exp> if <filter exp>)

6

One large expression that evaluates to an iterable object

slide-50
SLIDE 50

Generator Expressions

(<map exp> for <name> in <iter exp> if <filter exp>)

6

One large expression that evaluates to an iterable object

  • Evaluates to an iterable object.
slide-51
SLIDE 51

Generator Expressions

(<map exp> for <name> in <iter exp> if <filter exp>)

6

One large expression that evaluates to an iterable object

  • Evaluates to an iterable object.
  • <iter exp> is evaluated when the generator expression

is evaluated.

slide-52
SLIDE 52

Generator Expressions

(<map exp> for <name> in <iter exp> if <filter exp>)

6

One large expression that evaluates to an iterable object

  • Evaluates to an iterable object.
  • <iter exp> is evaluated when the generator expression

is evaluated.

  • Remaining expressions are evaluated when elements are

accessed.

slide-53
SLIDE 53

Generator Expressions

(<map exp> for <name> in <iter exp> if <filter exp>)

6

One large expression that evaluates to an iterable object Short version: (<map exp> for <name> in <iter exp>)

  • Evaluates to an iterable object.
  • <iter exp> is evaluated when the generator expression

is evaluated.

  • Remaining expressions are evaluated when elements are

accessed.

slide-54
SLIDE 54

Generator Expressions

(<map exp> for <name> in <iter exp> if <filter exp>)

6

One large expression that evaluates to an iterable object Short version: (<map exp> for <name> in <iter exp>)

  • Evaluates to an iterable object.
  • <iter exp> is evaluated when the generator expression

is evaluated.

  • Remaining expressions are evaluated when elements are

accessed. Precise evaluation rule introduced in Chapter 4.

slide-55
SLIDE 55

Generator Expressions

(<map exp> for <name> in <iter exp> if <filter exp>)

6

One large expression that evaluates to an iterable object Short version: (<map exp> for <name> in <iter exp>)

  • Evaluates to an iterable object.
  • <iter exp> is evaluated when the generator expression

is evaluated.

  • Remaining expressions are evaluated when elements are

accessed. Precise evaluation rule introduced in Chapter 4.

Demo

slide-56
SLIDE 56

Python Lists

['Demo']

7

http://docs.python.org/py3k/library/stdtypes.html#mutable-sequence-types

slide-57
SLIDE 57

List Comprehensions

8

slide-58
SLIDE 58

List Comprehensions

8

[<map exp> for <name> in <iter exp> if <filter exp>]

slide-59
SLIDE 59

List Comprehensions

8

[<map exp> for <name> in <iter exp> if <filter exp>] Short version: [<map exp> for <name> in <iter exp>]

slide-60
SLIDE 60

List Comprehensions

8

[<map exp> for <name> in <iter exp> if <filter exp>] Short version: [<map exp> for <name> in <iter exp>] Unlike generator expressions, the map expression is evaluated when the list comprehension is evaluated.

slide-61
SLIDE 61

List Comprehensions

8

>>> suits = ['heart', 'diamond', 'spade', 'club']

[<map exp> for <name> in <iter exp> if <filter exp>] Short version: [<map exp> for <name> in <iter exp>] Unlike generator expressions, the map expression is evaluated when the list comprehension is evaluated.

slide-62
SLIDE 62

List Comprehensions

8

>>> suits = ['heart', 'diamond', 'spade', 'club'] >>> from unicodedata import lookup

[<map exp> for <name> in <iter exp> if <filter exp>] Short version: [<map exp> for <name> in <iter exp>] Unlike generator expressions, the map expression is evaluated when the list comprehension is evaluated.

slide-63
SLIDE 63

List Comprehensions

8

>>> suits = ['heart', 'diamond', 'spade', 'club'] >>> from unicodedata import lookup >>> [lookup('WHITE ' + s.upper() + ' SUIT') for s in suits]

[<map exp> for <name> in <iter exp> if <filter exp>] Short version: [<map exp> for <name> in <iter exp>] Unlike generator expressions, the map expression is evaluated when the list comprehension is evaluated.

slide-64
SLIDE 64

List Comprehensions

8

>>> suits = ['heart', 'diamond', 'spade', 'club'] >>> from unicodedata import lookup >>> [lookup('WHITE ' + s.upper() + ' SUIT') for s in suits]

['♡', '♢', '♤', '♧']

[<map exp> for <name> in <iter exp> if <filter exp>] Short version: [<map exp> for <name> in <iter exp>] Unlike generator expressions, the map expression is evaluated when the list comprehension is evaluated.

slide-65
SLIDE 65

Dictionaries

{'Dem': 0}

9

slide-66
SLIDE 66

Limitations on Dictionaries

10

slide-67
SLIDE 67

Limitations on Dictionaries

Dictionaries are unordered collections of key-value pairs.

10

slide-68
SLIDE 68

Limitations on Dictionaries

Dictionaries are unordered collections of key-value pairs. Dictionary keys do have two restrictions:

10

slide-69
SLIDE 69

Limitations on Dictionaries

Dictionaries are unordered collections of key-value pairs. Dictionary keys do have two restrictions:

  • A key of a dictionary cannot be an object of a mutable

built-in type.

10

slide-70
SLIDE 70

Limitations on Dictionaries

Dictionaries are unordered collections of key-value pairs. Dictionary keys do have two restrictions:

  • A key of a dictionary cannot be an object of a mutable

built-in type.

  • Two keys cannot be equal. There can be at most one value for

a given key.

10

slide-71
SLIDE 71

Limitations on Dictionaries

Dictionaries are unordered collections of key-value pairs. Dictionary keys do have two restrictions:

  • A key of a dictionary cannot be an object of a mutable

built-in type.

  • Two keys cannot be equal. There can be at most one value for

a given key. This first restriction is tied to Python's underlying implementation of dictionaries.

10

slide-72
SLIDE 72

Limitations on Dictionaries

Dictionaries are unordered collections of key-value pairs. Dictionary keys do have two restrictions:

  • A key of a dictionary cannot be an object of a mutable

built-in type.

  • Two keys cannot be equal. There can be at most one value for

a given key. This first restriction is tied to Python's underlying implementation of dictionaries. The second restriction is an intentional consequence of the dictionary abstraction.

10