DOC PREVIEW
Berkeley COMPSCI 61A - Lecture 10

This preview shows page 1 out of 3 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 3 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 3 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

61A Lecture 10Wednesday, September 21Strings are an Abstraction2Representing data:'200' '1.2e-5' 'False' '(1, 2)'Representing language:"""O! methinks how slow This old moon wanes; she lingers my desires ,Like to a step dame, or a dowager Long withering out a young man's revenue."""Representing programs:'curry = lambda f: lambda x: lambda y: f(x, y)'Representing Strings: the ASCII Standard3American Standard Code for Information Interchange3 bits4 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""Bell"Representing Strings: the Unicode Standard4http://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•32 bits per character number •A canonical name for every characterU+0058 LATIN CAPITAL LETTER XU+263a WHITE SMILING FACEU+2639 WHITE FROWNING FACE'!''"'DemoBonusMaterialRepresenting Strings: UTF-8 EncodingUTF: (UCS (Universal Character Set) Transformation Format)Unicode: Correspondence between characters and 32 bit numbersUTF-8: Correspondence between numbers and bytesA byte is 8 bits, and can encode any integer 0-2555Variable-length encoding: integers vary in the number of bytes required to encode them!00000000 000000001 100000011 300000010 2bytes integersIn Python: string length in characters, bytes length in bytesDemoBonusMaterialStrings are Sequences>>> city = 'Berkeley'>>> len(city)8>>> city[3]'k'6Length. 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!>>> 'Berkeley' + ', CA''Berkeley, CA'>>> 'Shabu ' * 2'Shabu Shabu 'String arithmetic is like tuple arithmeticString Membership Differs from Other SequencesThe "in" and "not in" operators match substrings7>>> 'here' in "Where's Waldo?"True>>> 'Mississippi'.count('i')4>>> 'Mississippi'.count('issi')1The "count" method also matches substringsthe number of non-overlapping occurrences of a substringWhy? Working with strings, we care about words, not charactersString Literals Have Three Forms8>>> '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 lineA backslash "escapes" the following characterSingle- and double-quoted strings are equivalentString CoercionAny object can be "coerced" into a string.Coercion doesn't change an object; it produces a corresponding object of a different type.9>>> digits(1, 8, 2, 8)>>> 2 in digitsTrue>>> str(2) + ' is an element of ' + str(digits)'2 is an element of (1, 8, 2, 8)'The constructor for a string can take any object as its argumentHow is string coercion implemented? October 10Methods on Strings10>>> '1234'.isnumeric()True>>> 'rOBERT dE nIRO'.swapcase()'Robert De Niro'>>> 'snakeyes'.upper().endswith('YES')TrueDemoSequences as Conventional InterfacesConsider 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.110, 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 iseven:accumulate sum: 44Sequences as Conventional InterfacesConsider 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.12'University', 'of', 'California', 'Berkeley'enumerate words:filter iscap:map first:accumulate tuple:'University', 'California', 'Berkeley' 'U', 'C', 'B'( 'U', 'C', 'B' )Mapping a Function over a Sequence13>>> alternates = (-1, 2, -3, 4, -5)>>> tuple(map(abs, alternates))(1, 2, 3, 4, 5)Apply a function to each element of the sequenceDemoThe returned value of map is an iterable map objectA constructor for the built-in map typeThe returned value of filter is an iterable filter objectAccumulation and Iterable ValuesIterable objects give access to some elements in order.Many built-in functions take iterable objects as argument.14tuple Return a tuple containing the elementssum Return the sum of the elementsmin Return the minimum of the elementsmax Return the maximum of the elementsFor statements also operate on iterable values.DemoGenerator Expressions(<map exp> for <name> in <iter exp> if <filter exp>)15One large expression that evaluates to an iterable object(<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 accessedPrecise evaluation rule introduced in Chapter 4.Reducing a SequenceReduce is a higher-order generalization of max, min, & sum.16>>> from operator import mul>>> from functools import reduce>>> reduce(mul, (1, 2, 3, 4, 5))120Similar to accumulate from Homework


View Full Document

Berkeley COMPSCI 61A - Lecture 10

Documents in this Course
Lecture 1

Lecture 1

68 pages

Midterm

Midterm

5 pages

Midterm

Midterm

6 pages

Lecture 35

Lecture 35

250 pages

Lecture 14

Lecture 14

125 pages

Lecture 2

Lecture 2

159 pages

Lecture 6

Lecture 6

113 pages

Lecture 3

Lecture 3

162 pages

Homework

Homework

25 pages

Lecture 13

Lecture 13

117 pages

Lecture 29

Lecture 29

104 pages

Lecture 11

Lecture 11

173 pages

Lecture 7

Lecture 7

104 pages

Midterm

Midterm

6 pages

Midterm

Midterm

6 pages

Lecture 8

Lecture 8

108 pages

Lab 4

Lab 4

4 pages

Lecture 7

Lecture 7

52 pages

Lecture 20

Lecture 20

129 pages

Lecture 15

Lecture 15

132 pages

Lecture 9

Lecture 9

95 pages

Lecture 30

Lecture 30

108 pages

Lecture 17

Lecture 17

106 pages

Load more
Download Lecture 10
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 10 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 10 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?