61a lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-strings_1pp.pdf · 61a lecture 13...

131
61A Lecture 13 Wednesday, October 2

Upload: others

Post on 10-Jul-2020

11 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

61A Lecture 13

Wednesday, October 2

Page 2: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Announcements

2

Page 3: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Announcements

• Homework 3 deadline extended to Wednesday 10/2 @ 11:59pm.

2

Page 4: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Announcements

• Homework 3 deadline extended to Wednesday 10/2 @ 11:59pm.

• Optional Hog strategy contest due Thursday 10/3 @ 11:59pm.

2

Page 5: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Announcements

• Homework 3 deadline extended to Wednesday 10/2 @ 11:59pm.

• Optional Hog strategy contest due Thursday 10/3 @ 11:59pm.

• Homework 4 due Tuesday 10/8 @ 11:59pm.

2

Page 6: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Announcements

• Homework 3 deadline extended to Wednesday 10/2 @ 11:59pm.

• Optional Hog strategy contest due Thursday 10/3 @ 11:59pm.

• Homework 4 due Tuesday 10/8 @ 11:59pm.

• Project 2 due Thursday 10/10 @ 11:59pm.

2

Page 7: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Announcements

• Homework 3 deadline extended to Wednesday 10/2 @ 11:59pm.

• Optional Hog strategy contest due Thursday 10/3 @ 11:59pm.

• Homework 4 due Tuesday 10/8 @ 11:59pm.

• Project 2 due Thursday 10/10 @ 11:59pm.

• Guerrilla Section 2 this Saturday 10/5 & Sunday 10/6 10am-1pm in Soda.

2

Page 8: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Announcements

• Homework 3 deadline extended to Wednesday 10/2 @ 11:59pm.

• Optional Hog strategy contest due Thursday 10/3 @ 11:59pm.

• Homework 4 due Tuesday 10/8 @ 11:59pm.

• Project 2 due Thursday 10/10 @ 11:59pm.

• Guerrilla Section 2 this Saturday 10/5 & Sunday 10/6 10am-1pm in Soda.

Topics: Data abstraction, sequences, and non-local assignment.

2

Page 9: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Announcements

• Homework 3 deadline extended to Wednesday 10/2 @ 11:59pm.

• Optional Hog strategy contest due Thursday 10/3 @ 11:59pm.

• Homework 4 due Tuesday 10/8 @ 11:59pm.

• Project 2 due Thursday 10/10 @ 11:59pm.

• Guerrilla Section 2 this Saturday 10/5 & Sunday 10/6 10am-1pm in Soda.

Topics: Data abstraction, sequences, and non-local assignment.

Please RSVP on Piazza!

2

Page 10: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Announcements

• Homework 3 deadline extended to Wednesday 10/2 @ 11:59pm.

• Optional Hog strategy contest due Thursday 10/3 @ 11:59pm.

• Homework 4 due Tuesday 10/8 @ 11:59pm.

• Project 2 due Thursday 10/10 @ 11:59pm.

• Guerrilla Section 2 this Saturday 10/5 & Sunday 10/6 10am-1pm in Soda.

Topics: Data abstraction, sequences, and non-local assignment.

Please RSVP on Piazza!

• Guest lecture on Wednesday 10/9, Peter Norvig on Natural Language Processing in Python.

2

Page 11: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Strings

Page 12: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Strings are an Abstraction

4

Page 13: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Strings are an Abstraction

Representing data:

'200' '1.2e-5' 'False' '(1, 2)'

4

Page 14: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Strings are an Abstraction

Representing data:

'200' '1.2e-5' 'False' '(1, 2)'

Representing language:

"""And, as imagination bodies forthThe forms of things to unknown, and the poet's penTurns them to shapes, and gives to airy nothingA local habitation and a name."""

4

Page 15: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Strings are an Abstraction

Representing data:

'200' '1.2e-5' 'False' '(1, 2)'

Representing language:

"""And, as imagination bodies forthThe forms of things to unknown, and the poet's penTurns them to shapes, and gives to airy nothingA local habitation and a name."""

Representing programs:

'curry = lambda f: lambda x: lambda y: f(x, y)'

4

Page 16: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Strings are an Abstraction

Representing data:

'200' '1.2e-5' 'False' '(1, 2)'

Representing language:

"""And, as imagination bodies forthThe forms of things to unknown, and the poet's penTurns them to shapes, and gives to airy nothingA local habitation and a name."""

Representing programs:

'curry = lambda f: lambda x: lambda y: f(x, y)'

(Demo)

4

Page 17: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

String Literals Have Three Forms

>>> 'I am string!''I am string!'

>>> "I've got an apostrophe""I've got an apostrophe"

>>> '您好''您好'

5

Page 18: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

String Literals Have Three Forms

>>> 'I am string!''I am string!'

>>> "I've got an apostrophe""I've got an apostrophe"

>>> '您好''您好'

Single-quoted and double-quoted strings are equivalent

5

Page 19: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

String Literals Have Three Forms

>>> 'I am string!''I am string!'

>>> "I've got an apostrophe""I've got an apostrophe"

>>> '您好''您好'

>>> """The Zen of Pythonclaims, Readability counts.Read more: import this."""'The Zen of Python\nclaims, Readability counts.\nRead more: import this.'

Single-quoted and double-quoted strings are equivalent

5

Page 20: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

String Literals Have Three Forms

>>> 'I am string!''I am string!'

>>> "I've got an apostrophe""I've got an apostrophe"

>>> '您好''您好'

>>> """The Zen of Pythonclaims, Readability counts.Read more: import this."""'The Zen of Python\nclaims, Readability counts.\nRead more: import this.'

A backslash "escapes" the following character

Single-quoted and double-quoted strings are equivalent

5

Page 21: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

String Literals Have Three Forms

>>> 'I am string!''I am string!'

>>> "I've got an apostrophe""I've got an apostrophe"

>>> '您好''您好'

>>> """The Zen of Pythonclaims, Readability counts.Read more: import this."""'The Zen of Python\nclaims, Readability counts.\nRead more: import this.'

"Line feed" character represents a new line

A backslash "escapes" the following character

Single-quoted and double-quoted strings are equivalent

5

Page 22: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Strings are Sequences

6

Page 23: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Strings are Sequences

Length. A sequence has a finite length.

Element selection. A sequence has an element corresponding to any non-negative integer index less than its length, starting at 0 for the first element.

6

Page 24: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Strings are Sequences

>>> city = 'Berkeley'>>> len(city)8>>> city[3]'k'

Length. A sequence has a finite length.

Element selection. A sequence has an element corresponding to any non-negative integer index less than its length, starting at 0 for the first element.

6

Page 25: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Strings are Sequences

>>> city = 'Berkeley'>>> len(city)8>>> city[3]'k'

Length. A sequence has a finite length.

Element selection. A sequence has an element corresponding to any non-negative integer index less than its length, starting at 0 for the first element.

An element of a string is itself a string, but with only one character!

6

Page 26: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Strings are Sequences

>>> city = 'Berkeley'>>> len(city)8>>> city[3]'k'

Length. A sequence has a finite length.

Element selection. A sequence has an element corresponding to any non-negative integer index less than its length, starting at 0 for the first element.

An element of a string is itself a string, but with only one character!

6

(Demo)

Page 27: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

String Membership Differs from Other Sequence Types

7

Page 28: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

String Membership Differs from Other Sequence Types

The "in" and "not in" operators match substrings

7

Page 29: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

String Membership Differs from Other Sequence Types

The "in" and "not in" operators match substrings

>>> 'here' in "Where's Waldo?"True>>> 234 in (1, 2, 3, 4, 5)False

7

Page 30: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

String Membership Differs from Other Sequence Types

The "in" and "not in" operators match substrings

>>> 'here' in "Where's Waldo?"True>>> 234 in (1, 2, 3, 4, 5)False

Why? Working with strings, we usually care about words more than characters

7

Page 31: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

String Membership Differs from Other Sequence Types

The "in" and "not in" operators match substrings

>>> 'here' in "Where's Waldo?"True>>> 234 in (1, 2, 3, 4, 5)False

The count method also matches substrings

Why? Working with strings, we usually care about words more than characters

7

Page 32: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

String Membership Differs from Other Sequence Types

The "in" and "not in" operators match substrings

>>> 'here' in "Where's Waldo?"True>>> 234 in (1, 2, 3, 4, 5)False

>>> 'Mississippi'.count('i')4>>> 'Mississippi'.count('issi')1

The count method also matches substrings

Why? Working with strings, we usually care about words more than characters

7

Page 33: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

String Membership Differs from Other Sequence Types

The "in" and "not in" operators match substrings

>>> 'here' in "Where's Waldo?"True>>> 234 in (1, 2, 3, 4, 5)False

>>> 'Mississippi'.count('i')4>>> 'Mississippi'.count('issi')1

The count method also matches substrings

the number of non-overlapping occurrences of a

substring

Why? Working with strings, we usually care about words more than characters

7

Page 34: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

String Membership Differs from Other Sequence Types

The "in" and "not in" operators match substrings

>>> 'here' in "Where's Waldo?"True>>> 234 in (1, 2, 3, 4, 5)False

>>> 'Mississippi'.count('i')4>>> 'Mississippi'.count('issi')1

The count method also matches substrings

the number of non-overlapping occurrences of a

substring

Why? Working with strings, we usually care about words more than characters

7

Page 35: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Encoding Strings

Page 36: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Representing Strings: the ASCII Standard

American Standard Code for Information Interchange

9

Page 37: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Representing Strings: the ASCII Standard

American Standard Code for Information Interchange8

rows

: 3

bits

9

Page 38: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Representing Strings: the ASCII Standard

American Standard Code for Information Interchange8

rows

: 3

bits

16 columns: 4 bits

9

Page 39: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Representing Strings: the ASCII Standard

American Standard Code for Information Interchange8

rows

: 3

bits

16 columns: 4 bits

• Layout was chosen to support sorting by character code

9

Page 40: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Representing Strings: the ASCII Standard

American Standard Code for Information Interchange8

rows

: 3

bits

16 columns: 4 bits

• Layout was chosen to support sorting by character code• Rows indexed 2-5 are a useful 6-bit (64 element) subset

9

Page 41: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Representing Strings: the ASCII Standard

American Standard Code for Information Interchange8

rows

: 3

bits

16 columns: 4 bits

• Layout was chosen to support sorting by character code• Rows indexed 2-5 are a useful 6-bit (64 element) subset• Control characters were designed for transmission

9

Page 42: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Representing Strings: the ASCII Standard

American Standard Code for Information Interchange8

rows

: 3

bits

16 columns: 4 bits

• Layout was chosen to support sorting by character code• Rows indexed 2-5 are a useful 6-bit (64 element) subset• Control characters were designed for transmission

"Line feed" (\n)

9

Page 43: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Representing Strings: the ASCII Standard

American Standard Code for Information Interchange8

rows

: 3

bits

16 columns: 4 bits

• Layout was chosen to support sorting by character code• Rows indexed 2-5 are a useful 6-bit (64 element) subset• Control characters were designed for transmission

"Line feed" (\n)"Bell" (\a)

9

Page 44: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Representing Strings: the ASCII Standard

American Standard Code for Information Interchange8

rows

: 3

bits

16 columns: 4 bits

• Layout was chosen to support sorting by character code• Rows indexed 2-5 are a useful 6-bit (64 element) subset• Control characters were designed for transmission

"Line feed" (\n)"Bell" (\a)

9

(Demo)

Page 45: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Representing Strings: the Unicode Standard

10

Page 46: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Representing Strings: the Unicode Standard

http://ian-albert.com/unicode_chart/unichart-chinese.jpg

10

Page 47: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Representing Strings: the Unicode Standard

http://ian-albert.com/unicode_chart/unichart-chinese.jpg

• 109,000 characters

10

Page 48: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Representing Strings: the Unicode Standard

http://ian-albert.com/unicode_chart/unichart-chinese.jpg

• 109,000 characters• 93 scripts (organized)

10

Page 49: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Representing Strings: the Unicode Standard

http://ian-albert.com/unicode_chart/unichart-chinese.jpg

• 109,000 characters• 93 scripts (organized)• Enumeration of character properties, such as case

10

Page 50: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Representing Strings: the Unicode Standard

http://ian-albert.com/unicode_chart/unichart-chinese.jpg

• 109,000 characters• 93 scripts (organized)• Enumeration of character properties, such as case

• Supports bidirectional display order

10

Page 51: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Representing Strings: the Unicode Standard

http://ian-albert.com/unicode_chart/unichart-chinese.jpg

• 109,000 characters• 93 scripts (organized)• Enumeration of character properties, such as case

• Supports bidirectional display order• A canonical name for every character

10

Page 52: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Representing Strings: the Unicode Standard

http://ian-albert.com/unicode_chart/unichart-chinese.jpg

• 109,000 characters• 93 scripts (organized)• Enumeration of character properties, such as case

• Supports bidirectional display order• A canonical name for every character

U+0058 LATIN CAPITAL LETTER X

10

Page 53: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Representing Strings: the Unicode Standard

http://ian-albert.com/unicode_chart/unichart-chinese.jpg

• 109,000 characters• 93 scripts (organized)• Enumeration of character properties, such as case

• Supports bidirectional display order• A canonical name for every character

U+0058 LATIN CAPITAL LETTER X

U+263a WHITE SMILING FACE

10

Page 54: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Representing Strings: the Unicode Standard

http://ian-albert.com/unicode_chart/unichart-chinese.jpg

• 109,000 characters• 93 scripts (organized)• Enumeration of character properties, such as case

• Supports bidirectional display order• A canonical name for every character

U+0058 LATIN CAPITAL LETTER X

U+263a WHITE SMILING FACE

U+2639 WHITE FROWNING FACE

10

Page 55: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Representing Strings: the Unicode Standard

http://ian-albert.com/unicode_chart/unichart-chinese.jpg

• 109,000 characters• 93 scripts (organized)• Enumeration of character properties, such as case

• Supports bidirectional display order• A canonical name for every character

U+0058 LATIN CAPITAL LETTER X

U+263a WHITE SMILING FACE

U+2639 WHITE FROWNING FACE

'☺'10

Page 56: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Representing Strings: the Unicode Standard

http://ian-albert.com/unicode_chart/unichart-chinese.jpg

• 109,000 characters• 93 scripts (organized)• Enumeration of character properties, such as case

• Supports bidirectional display order• A canonical name for every character

U+0058 LATIN CAPITAL LETTER X

U+263a WHITE SMILING FACE

U+2639 WHITE FROWNING FACE

'☺' '☹'10

Page 57: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Representing Strings: the Unicode Standard

http://ian-albert.com/unicode_chart/unichart-chinese.jpg

• 109,000 characters• 93 scripts (organized)• Enumeration of character properties, such as case

• Supports bidirectional display order• A canonical name for every character

U+0058 LATIN CAPITAL LETTER X

U+263a WHITE SMILING FACE

U+2639 WHITE FROWNING FACE

'☺' '☹'10

(Demo)

Page 58: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Representing Strings: UTF-8 Encoding

11

Page 59: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Representing Strings: UTF-8 Encoding

UTF (UCS (Universal Character Set) Transformation Format)

11

Page 60: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Representing Strings: UTF-8 Encoding

UTF (UCS (Universal Character Set) Transformation Format)

Unicode: Correspondence between characters and integers

11

Page 61: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Representing Strings: UTF-8 Encoding

UTF (UCS (Universal Character Set) Transformation Format)

Unicode: Correspondence between characters and integers

UTF-8: Correspondence between those integers and bytes

11

Page 62: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Representing Strings: UTF-8 Encoding

UTF (UCS (Universal Character Set) Transformation Format)

Unicode: Correspondence between characters and integers

UTF-8: Correspondence between those integers and bytes

A byte is 8 bits and can encode any integer 0-255.

11

Page 63: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Representing Strings: UTF-8 Encoding

UTF (UCS (Universal Character Set) Transformation Format)

Unicode: Correspondence between characters and integers

UTF-8: Correspondence between those integers and bytes

A byte is 8 bits and can encode any integer 0-255.

bytes

11

Page 64: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Representing Strings: UTF-8 Encoding

UTF (UCS (Universal Character Set) Transformation Format)

Unicode: Correspondence between characters and integers

UTF-8: Correspondence between those integers and bytes

A byte is 8 bits and can encode any integer 0-255.

bytes integers

11

Page 65: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Representing Strings: UTF-8 Encoding

UTF (UCS (Universal Character Set) Transformation Format)

Unicode: Correspondence between characters and integers

UTF-8: Correspondence between those integers and bytes

A byte is 8 bits and can encode any integer 0-255.

00000000 0

bytes integers

11

Page 66: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Representing Strings: UTF-8 Encoding

UTF (UCS (Universal Character Set) Transformation Format)

Unicode: Correspondence between characters and integers

UTF-8: Correspondence between those integers and bytes

A byte is 8 bits and can encode any integer 0-255.

00000000 000000001 1bytes integers

11

Page 67: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Representing Strings: UTF-8 Encoding

UTF (UCS (Universal Character Set) Transformation Format)

Unicode: Correspondence between characters and integers

UTF-8: Correspondence between those integers and bytes

A byte is 8 bits and can encode any integer 0-255.

00000000 000000001 100000010 2

bytes integers

11

Page 68: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Representing Strings: UTF-8 Encoding

UTF (UCS (Universal Character Set) Transformation Format)

Unicode: Correspondence between characters and integers

UTF-8: Correspondence between those integers and bytes

A byte is 8 bits and can encode any integer 0-255.

00000000 000000001 1

00000011 300000010 2

bytes integers

11

Page 69: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Representing Strings: UTF-8 Encoding

UTF (UCS (Universal Character Set) Transformation Format)

Unicode: Correspondence between characters and integers

UTF-8: Correspondence between those integers and bytes

A byte is 8 bits and can encode any integer 0-255.

Variable-length encoding: integers vary in the number of bytes required to encode them.

00000000 000000001 1

00000011 300000010 2

bytes integers

11

Page 70: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Representing Strings: UTF-8 Encoding

UTF (UCS (Universal Character Set) Transformation Format)

Unicode: Correspondence between characters and integers

UTF-8: Correspondence between those integers and bytes

A byte is 8 bits and can encode any integer 0-255.

Variable-length encoding: integers vary in the number of bytes required to encode them.

00000000 000000001 1

00000011 300000010 2

bytes integers

In Python: string length is measured in characters, bytes length in bytes.

11

Page 71: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Representing Strings: UTF-8 Encoding

UTF (UCS (Universal Character Set) Transformation Format)

Unicode: Correspondence between characters and integers

UTF-8: Correspondence between those integers and bytes

A byte is 8 bits and can encode any integer 0-255.

Variable-length encoding: integers vary in the number of bytes required to encode them.

00000000 000000001 1

00000011 300000010 2

bytes integers

In Python: string length is measured in characters, bytes length in bytes.

11

(Demo)

Page 72: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Sequence Processing

Page 73: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Sequence Processing

13

Page 74: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Consider two problems:

Sequence Processing

13

Page 75: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Consider two problems:

Sum the even members of the first n Fibonacci numbers.

Sequence Processing

13

Page 76: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Consider two problems:

Sum the even members of the first n Fibonacci numbers.

List the letters in the acronym for a name, which includes the first letter of each capitalized word.

Sequence Processing

13

Page 77: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Consider two problems:

Sum the even members of the first n Fibonacci numbers.

List the letters in the acronym for a name, which includes the first letter of each capitalized word.

Sequence Processing

13

Page 78: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Consider two problems:

Sum the even members of the first n Fibonacci numbers.

List the letters in the acronym for a name, which includes the first letter of each capitalized word.

Sequence Processing

enumerate naturals:

13

Page 79: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Consider two problems:

Sum the even members of the first n Fibonacci numbers.

List the letters in the acronym for a name, which includes the first letter of each capitalized word.

Sequence Processing

1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11.enumerate naturals:

13

Page 80: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Consider two problems:

Sum the even members of the first n Fibonacci numbers.

List the letters in the acronym for a name, which includes the first letter of each capitalized word.

Sequence Processing

1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11.enumerate naturals:

map fib:

13

Page 81: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Consider two problems:

Sum the even members of the first n Fibonacci numbers.

List the letters in the acronym for a name, which includes the first letter of each capitalized word.

Sequence Processing

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55.

1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11.enumerate naturals:

map fib:

13

Page 82: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Consider two problems:

Sum the even members of the first n Fibonacci numbers.

List the letters in the acronym for a name, which includes the first letter of each capitalized word.

Sequence Processing

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55.

1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11.enumerate naturals:

map fib:

filter even:

13

Page 83: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Consider two problems:

Sum the even members of the first n Fibonacci numbers.

List the letters in the acronym for a name, which includes the first letter of each capitalized word.

Sequence Processing

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55.

1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11.enumerate naturals:

map fib:

filter even:

13

Page 84: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Consider two problems:

Sum the even members of the first n Fibonacci numbers.

List the letters in the acronym for a name, which includes the first letter of each capitalized word.

Sequence Processing

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55.

1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11.

0, 2, 8, 34, .

enumerate naturals:

map fib:

filter even:

13

Page 85: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Consider two problems:

Sum the even members of the first n Fibonacci numbers.

List the letters in the acronym for a name, which includes the first letter of each capitalized word.

Sequence Processing

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55.

1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11.

0, 2, 8, 34, .

enumerate naturals:

map fib:

filter even:

accumulate sum:

13

Page 86: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Consider two problems:

Sum the even members of the first n Fibonacci numbers.

List the letters in the acronym for a name, which includes the first letter of each capitalized word.

Sequence Processing

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55.

1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11.

0, 2, 8, 34, .

enumerate naturals:

map fib:

filter even:

accumulate sum: ., ., ., ., =44

13

Page 87: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Sequence Processing

Consider two problems:

Sum the even members of the first n Fibonacci numbers.

List the letters in the acronym for a name, which includes the first letter of each capitalized word.

14

Page 88: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Sequence Processing

Consider two problems:

Sum the even members of the first n Fibonacci numbers.

List the letters in the acronym for a name, which includes the first letter of each capitalized word.

14

Page 89: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Sequence Processing

Consider two problems:

Sum the even members of the first n Fibonacci numbers.

List the letters in the acronym for a name, which includes the first letter of each capitalized word.

enumerate words:

14

Page 90: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Sequence Processing

Consider two problems:

Sum the even members of the first n Fibonacci numbers.

List the letters in the acronym for a name, which includes the first letter of each capitalized word.

'University', 'of', 'California', 'Berkeley'enumerate words:

14

Page 91: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Sequence Processing

Consider two problems:

Sum the even members of the first n Fibonacci numbers.

List the letters in the acronym for a name, which includes the first letter of each capitalized word.

'University', 'of', 'California', 'Berkeley'enumerate words:

filter capitalized:

14

Page 92: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Sequence Processing

Consider two problems:

Sum the even members of the first n Fibonacci numbers.

List the letters in the acronym for a name, which includes the first letter of each capitalized word.

'University', 'of', 'California', 'Berkeley'enumerate words:

filter capitalized:

14

Page 93: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Sequence Processing

Consider two problems:

Sum the even members of the first n Fibonacci numbers.

List the letters in the acronym for a name, which includes the first letter of each capitalized word.

'University', 'of', 'California', 'Berkeley'enumerate words:

filter capitalized: 'University', 'California', 'Berkeley'

14

Page 94: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Sequence Processing

Consider two problems:

Sum the even members of the first n Fibonacci numbers.

List the letters in the acronym for a name, which includes the first letter of each capitalized word.

'University', 'of', 'California', 'Berkeley'enumerate words:

filter capitalized:

map first:

'University', 'California', 'Berkeley'

14

Page 95: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Sequence Processing

Consider two problems:

Sum the even members of the first n Fibonacci numbers.

List the letters in the acronym for a name, which includes the first letter of each capitalized word.

'University', 'of', 'California', 'Berkeley'enumerate words:

filter capitalized:

map first:

'University', 'California', 'Berkeley'

'U', 'C', 'B'

14

Page 96: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Sequence Processing

Consider two problems:

Sum the even members of the first n Fibonacci numbers.

List the letters in the acronym for a name, which includes the first letter of each capitalized word.

'University', 'of', 'California', 'Berkeley'enumerate words:

filter capitalized:

map first:

accumulate tuple:

'University', 'California', 'Berkeley'

'U', 'C', 'B'

14

Page 97: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Sequence Processing

Consider two problems:

Sum the even members of the first n Fibonacci numbers.

List the letters in the acronym for a name, which includes the first letter of each capitalized word.

'University', 'of', 'California', 'Berkeley'enumerate words:

filter capitalized:

map first:

accumulate tuple:

'University', 'California', 'Berkeley'

'U', 'C', 'B'

( 'U', 'C', 'B' )

14

Page 98: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Mapping a Function over a Sequence

Apply a function to each element of the sequence

15

Page 99: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Mapping a Function over a Sequence

>>> alternates = (-1, 2, -3, 4, -5)

Apply a function to each element of the sequence

15

Page 100: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Mapping a Function over a Sequence

>>> alternates = (-1, 2, -3, 4, -5)

>>> tuple(map(abs, alternates))

Apply a function to each element of the sequence

15

Page 101: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Mapping a Function over a Sequence

>>> alternates = (-1, 2, -3, 4, -5)

>>> tuple(map(abs, alternates))(1, 2, 3, 4, 5)

Apply a function to each element of the sequence

15

Page 102: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Mapping a Function over a Sequence

>>> 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

15

Page 103: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Mapping a Function over a Sequence

>>> 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

15

Page 104: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Mapping a Function over a Sequence

>>> 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

15

Page 105: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Mapping a Function over a Sequence

>>> 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

15

(Demo)

Page 106: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Iteration and Accumulation

Page 107: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Iterable Values and Accumulation

17

Page 108: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Iterable Values and Accumulation

Iterable objects give access to their elements in order.

17

Page 109: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Iterable Values and Accumulation

Iterable objects give access to their elements in order.

Similar to a sequence, but does not always allow element selection or have finite length.

17

Page 110: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Iterable Values and Accumulation

Iterable objects give access to their elements in order.

Similar to a sequence, but does not always allow element selection or have finite length.

Many built-in functions take iterable objects as argument.

17

Page 111: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Iterable Values and Accumulation

Iterable objects give access to their elements in order.

Similar to a sequence, but does not always allow element selection or have finite length.

Many built-in functions take iterable objects as argument.

tuple Return a tuple containing the elements

17

Page 112: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Iterable Values and Accumulation

Iterable objects give access to their elements in order.

Similar to a sequence, but does not always allow element selection or have finite length.

Many built-in functions take iterable objects as argument.

tuple Return a tuple containing the elements

sum Return the sum of the elements

17

Page 113: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Iterable Values and Accumulation

Iterable objects give access to their elements in order.

Similar to a sequence, but does not always allow element selection or have finite length.

Many built-in functions take iterable objects as argument.

tuple Return a tuple containing the elements

sum Return the sum of the elements

min Return the minimum of the elements

17

Page 114: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Iterable Values and Accumulation

Iterable objects give access to their elements in order.

Similar to a sequence, but does not always allow element selection or have finite length.

Many built-in functions take iterable objects as argument.

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

17

Page 115: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Iterable Values and Accumulation

Iterable objects give access to their elements in order.

Similar to a sequence, but does not always allow element selection or have finite length.

Many built-in functions take iterable objects as argument.

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.

17

Page 116: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Reducing a Sequence

18

Page 117: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Reducing a Sequence

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

18

Page 118: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Reducing a Sequence

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

>>> from operator import mul

18

Page 119: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Reducing a Sequence

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

>>> from operator import mul

>>> from functools import reduce

18

Page 120: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Reducing a Sequence

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

>>> from operator import mul

>>> from functools import reduce

>>> reduce(mul, (1, 2, 3, 4, 5))

18

Page 121: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Reducing a Sequence

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

>>> from operator import mul

>>> from functools import reduce

>>> reduce(mul, (1, 2, 3, 4, 5))120

18

Page 122: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Reducing a Sequence

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

>>> from operator import mul

>>> from functools import reduce

>>> reduce(mul, (1, 2, 3, 4, 5))120

First argument:A two-argument function

18

Page 123: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Reducing a Sequence

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

>>> 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

18

Page 124: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Reducing a Sequence

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

>>> from operator import mul

>>> from functools import reduce

>>> reduce(mul, (1, 2, 3, 4, 5))120

Similar to accumulate from Homework 2, but with iterable objects.

First argument:A two-argument function

Second argument: an iterable object

18

Page 125: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Generator Expressions

One large expression that evaluates to an iterable object

19

Page 126: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Generator Expressions

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

One large expression that evaluates to an iterable object

19

Page 127: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Generator Expressions

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

One large expression that evaluates to an iterable object

•Evaluates to an iterable object.

19

Page 128: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Generator Expressions

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

One large expression that evaluates to an iterable object

•Evaluates to an iterable object.

•<iter exp> is evaluated when the generator expression is evaluated.

19

Page 129: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Generator Expressions

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

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.

19

Page 130: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Generator Expressions

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

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.

19

Page 131: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa13/slides/13-Strings_1pp.pdf · 61A Lecture 13 Wednesday, October 2. Announcements 2. Announcements •Homework 3 deadline extended

Generator Expressions

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

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.

19

(Demo)