+ All Categories
Home > Documents > List Paradigms - Reed Collegepeople.reed.edu/~esroberts/csci121/handouts/24-ListParadigms.pdf ·...

List Paradigms - Reed Collegepeople.reed.edu/~esroberts/csci121/handouts/24-ListParadigms.pdf ·...

Date post: 21-Jul-2020
Category:
Upload: others
View: 5 times
Download: 0 times
Share this document with a friend
3
Eric Roberts Handout #24 CSCI 121 March 2, 2020 List Paradigms List Paradigms Eric Roberts CSCI 121 March 2, 2020 List comprehensions were initially implemented in the programming language SETL, designed by NYU computer scientist Jacob Schwartz in the late 1960s. Guido van Rossum notes that SETL was a major source of inspiration when he was designing Python. The SETL Programming Language Jacob Schwartz (1930–2009) One of Schwartz’s longtime computer science collaborators (to whom he was also married) was Frances Allen, the first woman to win the ACM Turing Award. Using Arrays for Tabulation Arrays turn out to be useful when you have a set of data values and need to count how many values fall into each of a set of ranges. This process is called tabulation. Tabulation uses arrays in a different way from applications that use them to store a list of data. When you implement a tabulation program, you use each data value to compute an index into an array of integers that keeps track of how many values fall into that category. The example of tabulation used in the text is a program that counts how many times each of the 26 letters appears in a sequence of text lines. Such a program is useful in decoding letter-substitution ciphers, which is one aspect of the topic of cryptography you’ll be working with on Project #4. Cryptograms A cryptogram is a puzzle in which a message is encoded by replacing each letter in the original text with some other letter. The substitution pattern remains the same throughout the message. Your job in solving a cryptogram is to figure out this correspondence. In this story, Poe describes the technique of assuming that the most common letters in the coded message correspond to the most common letters in English, which are E, T, A, O, I, N, S, H, R, D, L, and U. One of the most famous cryptograms was written by Edgar Allan Poe in his short story “The Gold Bug.” Edgar Allan Poe (1809-1849) Poe’s Cryptogram Puzzle 53‡‡†305))6*;4826)4‡•)4‡);806*;48†8¶ 60))85;1‡(;:‡*8†83(88)5*†;46(;88*96* ?;8)*‡(;485);5*†2:*‡(;4956*2(5*–4)8¶ 8*;4069285);)6†8)4‡‡;1(‡9;48081;8:8‡ 1;48†85;4)485†528806*81(‡9;48;(88;4( ‡?34;48)4‡;161;:188;‡?; 8 33 ; 26 4 19 16 ) 16 * 13 5 12 6 11 ( 10 8 1 8 0 6 9 5 2 5 : 4 3 4 ? 3 2 1 1 AGOODGLASSINTHEBISHOPSHOSTELINTHEDEV ILSSEATFORTYONEDEGREESANDTHIRTEENMIN UTESNORTHEASTANDBYNORTHMAINBRANCHSEV ENTHLIMBEASTSIDESHOOTFROMTHELEFTEYEO FTHEDEATHSHEADABEELINEFROMTHETREETHR OUGHTHESHOTFIFTYFEETOUT Implementation Strategy The basic idea behind the program to count letter frequencies is to use an array with 26 elements to keep track of how many times each letter appears. As the program reads the text, it increments the array element that corresponds to each letter. 0 1 0 2 0 3 0 4 0 5 6 0 7 8 0 9 0 10 11 0 12 0 13 0 14 0 15 0 16 17 18 19 0 20 0 21 22 0 23 0 24 0 25 G I L L I R B S A W T 1 1 1 1 1 1 2 2 1 G I L L I R B S A W T
Transcript
Page 1: List Paradigms - Reed Collegepeople.reed.edu/~esroberts/csci121/handouts/24-ListParadigms.pdf · • List comprehensions were initially implemented in the programming language SETL,

Eric Roberts Handout #24CSCI 121 March 2, 2020

List Paradigms

List Paradigms

Eric Roberts CSCI 121

March 2, 2020

•  List comprehensions were initially implemented in the programming language SETL, designed by NYU computer scientist Jacob Schwartz in the late 1960s.

•  Guido van Rossum notes that SETL was a major source of inspiration when he was designing Python.

The SETL Programming Language

Jacob Schwartz (1930–2009)

•  One of Schwartz’s longtime computer science collaborators (to whom he was also married) was Frances Allen, the first woman to win the ACM Turing Award.

Using Arrays for Tabulation •  Arrays turn out to be useful when you have a set of data

values and need to count how many values fall into each of a set of ranges. This process is called tabulation.

•  Tabulation uses arrays in a different way from applications that use them to store a list of data. When you implement a tabulation program, you use each data value to compute an index into an array of integers that keeps track of how many values fall into that category.

•  The example of tabulation used in the text is a program that counts how many times each of the 26 letters appears in a sequence of text lines. Such a program is useful in decoding letter-substitution ciphers, which is one aspect of the topic of cryptography you’ll be working with on Project #4.

Cryptograms •  A cryptogram is a puzzle in which a message is encoded by

replacing each letter in the original text with some other letter. The substitution pattern remains the same throughout the message. Your job in solving a cryptogram is to figure out this correspondence.

•  In this story, Poe describes the technique of assuming that the most common letters in the coded message correspond to the most common letters in English, which are E, T, A, O, I, N, S, H, R, D, L, and U.

•  One of the most famous cryptograms was written by Edgar Allan Poe in his short story “The Gold Bug.”

Edgar Allan Poe (1809-1849)

Poe’s Cryptogram Puzzle 53‡‡†305))6*;4826)4‡•)4‡);806*;48†8¶60))85;1‡(;:‡*8†83(88)5*†;46(;88*96*?;8)*‡(;485);5*†2:*‡(;4956*2(5*–4)8¶8*;4069285);)6†8)4‡‡;1(‡9;48081;8:8‡1;48†85;4)485†528806*81(‡9;48;(88;4(‡?34;48)4‡;161;:188;‡?;

8 33 ; 26 4 19 ‡ 16 ) 16 * 13 5 12 6 11 ( 10 † 8 1 8 0 6 9 5 2 5 : 4 3 4 ? 3 ¶ 2 – 1 • 1

AGOODGLASSINTHEBISHOPSHOSTELINTHEDEVILSSEATFORTYONEDEGREESANDTHIRTEENMINUTESNORTHEASTANDBYNORTHMAINBRANCHSEVENTHLIMBEASTSIDESHOOTFROMTHELEFTEYEOFTHEDEATHSHEADABEELINEFROMTHETREETHROUGHTHESHOTFIFTYFEETOUT

Implementation Strategy •  The basic idea behind the program to count letter frequencies

is to use an array with 26 elements to keep track of how many times each letter appears. As the program reads the text, it increments the array element that corresponds to each letter.

0 0

0 1

0 2

0 3

0 4

0 5

0 6

0 7

0 8

0 9

0 10

0 11

0 12

0 13

0 14

0 15

0 16

0 17

0 18

0 19

0 20

0 21

0 22

0 23

0 24

0 25

GI L L I RBSA WT

1 1 1 1 1 1 1 1 2 2 1 G

I L L I RB SA WT

Page 2: List Paradigms - Reed Collegepeople.reed.edu/~esroberts/csci121/handouts/24-ListParadigms.pdf · • List comprehensions were initially implemented in the programming language SETL,

– 2 –

Code for CountLetterFrequencies Code for CountLetterFrequencies

Exercise: Display a Histogram •  Write a function showHistogram(scores) that takes a list of

scores in the range 0 to 100 and then displays on the console a histogram of those scores divided into ranges 0-9, 10-19, and so on.

•  Histograms are usually presented vertically. The one in this exercise is drawn horizontally because that program is so much easier to write.

Image Histograms •  The statistical notions behind the histogram program are

important in image processing of the sort you’ll undertake in Project #3.

•  One part of the ImageShop program uses histograms to adjust a photograph with insufficient contrast, as illustrated below:

low-contrast image� the same image after equalization�

•  The steps in the process are are illustrated on the next slide.

Equalization

cumulative histogram�standard histogram�

Initializing Lists Using Loops •  Python lists are usually initialized using the following syntax:

evenDigits = [ 0, 2, 4, 6, 8 ]

•  In some cases, it is easier to specify the elements of a list using a sequence of values generated by a for loop, as follows:

evenDigits = [ ] for i in range(0, 10, 2): evenDigits.append(i)

•  Python offers a convenient shorthand for initializing such lists in which the for loop appears inside the brackets like this:

evenDigits = [ i for i in range(0, 10, 2) ]

This syntactic form is called a list comprehension.

Page 3: List Paradigms - Reed Collegepeople.reed.edu/~esroberts/csci121/handouts/24-ListParadigms.pdf · • List comprehensions were initially implemented in the programming language SETL,

– 3 –

List Comprehensions •  The simplest syntax for a list comprehension is

[ expression iterator ]

•  The iterator component of the list comprehension can be followed by any number of additional modifiers, each of which is the header line for either a for statement to specify a nested loop or an if statement to select specific values.

•  As an example, the following list comprehension generates a list of all prime numbers less that 1000, assuming the existence of a function isPrime that tests for primality:

[ i for i in range(1000) if isPrime(i) ]

where expression is any Python expression and iterator is a for loop header that enumerates a sequence of values.

The Euler Totient Function

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 1 2 3 4 5 6

•  Modern cryptography uses mathematics that spans more than two millennia.

•  One important result was established by the Swiss mathematician Euler (1707–1783). The Euler totient function φ (n) is defined to be the number of positive integers less than n that are relatively prime to n. For example, φ (18) is 6:

•  Comprehensions make the totient function easy to implement: def totient(n): return len([ i for i in range(1, n) if gcd(i, n) == 1 ])

Exercise: List Comprehensions •  What are the values of the following Python expressions:

(b) [ chr(ord("A") + i) for i in range(26) ]

(c) [ x ** 2 for x in range(10) ]

(d) [ w for w in ENGLISH_WORDS if len(w) == 1 ]

(a) [ d for d in range(10) ]

•  How would you express these values using comprehension:

(b) [ 1, 10, 100, 1000, 10000, 100000, 1000000 ]

(c) [ all English palindromes, which read the same forward and backward ]

(a) [ "0", "1", "2", "3", "4", "5", "6", "7" ]


Recommended