Class 14 — Friday September 25

Reading is fundamental

You're so random

Look out my window — And see random acts of grace — By everyone


Look both ways


Agenda


Special download

  • Returns the contents of the web resource indicated by parameter link as a (normal) string.

Downloads for today


Downloads for interested students (and I hope you are all interested).


To do list


For the fun of it


Review

dataset = ... # get the dataset

... # set up dataset processing (if needed)

for row in dataset : # consider rows one by one

  # process current row of the dataset

  ... # process the row

... # finish off dataset (if needed)


dataset = ... # get the dataset

nbr_rows = len( dataset ) # determine number of rows

... # set up dataset processing (if needed)

for r in range( 0, nbr_rows) : # consider row indices one by one

  # process row r of the dataset

  row = dataset[ r ] # pick off the row of the dataset

  ... # process the row

... # finish off dataset (if needed)

dataset = ... # get the dataset

... # set up dataset processing (if needed)

for row in dataset : # consider rows one by one

  # process current row of the dataset

  ... # prepare to process the cells (if needed)

  for cell in row : # consider cells of the row one by one

  # process the current cell for the row

  ... # process the cell

  ... # finish off row (if needed )

... # finish off dataset (if needed)


dataset = ... # get the dataset

nbr_rows = len( dataset ) # determine number of rows

for r in range( 0, nbr_rows) : # consider row indices one by one

  # process row r of the dataset

  row = dataset[ r ] # pick off the row of the dataset

  nbr_columns = len( row ) # get its number of columns

  ... # prepare to process the cells (if needed)

  # process the cells of the current row

  for c in range( 0, nbr_columns ) : # consider row's column indices one by one

  # process cell at dataset[ r ][ c ]; that

  # is column c of row (i.e., row[ c ])

  cell = row[ c ] # pick off the cell of the row

  ... # process the cell

  ... # finish off row (if needed )

... # finish off dataset if neeed

dataset = ... # get the dataset

c = ... # get the column

column_copy = [] # need a cell accumulator

for row in dataset : # consider rows one by one

  # get cell from row' column c

  cell = row [ c ] # get the cell in column c

  column_copy.append( c ) # copy the cell into accumulator

... # analyze the column



Program lotta_books.py

Program run

header: ['Name', 'Author', 'Language', 'Date', 'Sales']

sales column: 4

name column: 0

date column: 3

books: [["Alice's Adventures in Wonderland", 'Carroll', 'English', 1865, 100000000], ['And Then There Were None', 'Christie', 'English', 1939, 100000000], ['Dream of the Red Chamber', 'Xueqin', 'Chinese', 1754, 100000000], ['Don Quixote', 'de Cervantes', 'Spanish', 1605, 500000000], ['Harry Potter', 'Rowling', 'English', 1997, 447000000], ['The Hobbit', 'Tolkien', 'English', 1937, 150000000], ['The Little Prince', 'de Saint-Exupery', 'French', 1943, 150000000], ['The Lord of the Rings', 'Tolkien', 'English', 1954, 150000000], ['A Tale of Two Cities', 'Dickens', 'English', 1859, 200000000]]

row: ["Alice's Adventures in Wonderland", 'Carroll', 'English', 1865, 100000000]

row: ['And Then There Were None', 'Christie', 'English', 1939, 100000000]

row: ['Dream of the Red Chamber', 'Xueqin', 'Chinese', 1754, 100000000]

row: ['Don Quixote', 'de Cervantes', 'Spanish', 1605, 500000000]

row: ['Harry Potter', 'Rowling', 'English', 1997, 447000000]

row: ['The Hobbit', 'Tolkien', 'English', 1937, 150000000]

row: ['The Little Prince', 'de Saint-Exupery', 'French', 1943, 150000000]

row: ['The Lord of the Rings', 'Tolkien', 'English', 1954, 150000000]

row: ['A Tale of Two Cities', 'Dickens', 'English', 1859, 200000000]

total sold: 1897000000

total sold: 1.897 billion

dates: [1865, 1939, 1754, 1605, 1997, 1937, 1943, 1954, 1859]

earliest: 1605

latest : 1997

average date: 1872

row with earliest book: 3

row with latest book : 4

info on earliest: ['Don Quixote', 'de Cervantes', 'Spanish', 1605, 500000000]

info on latest: ['Harry Potter', 'Rowling', 'English', 1997, 447000000]

name of earliest: Don Quixote

name of latest: Harry Potter



Being random


random number generator blackbox



Program chosen.py

Some program runs

Enter a string: abcdefghij

Four random characters from string abcdefghij : e a j d

Enter some words: th fo ju ov th la lo

Four random words from list ['th', 'fo', 'ju', 'ov', 'th', 'la', 'ol'] : ov lo la ov

Enter a string: abcdefghij

Four random characters from string abcdefghij : a i b h

Enter some words: th fo ju ov th la lo

Four random words from list ['th', 'fo', 'ju', 'ov', 'th', 'la', 'ol'] : th fo th ju

Enter a string: simultaneously

Four random characters from string simultaneously : i y m t

Enter some words: 31 41 597 2 71

Four random words from list ['31', '41', '597', '2', '71'] : 71 597 31 71



Program pick_a_number.py

Some program runs

Enter two integers: 10 100

Four random values from interval [ 0, 10 ): 7 4 3 5

Four random values from interval [ 0, 100 ): 15 28 64 87

Enter four integers: 10 20 75 100

Four random values from interval [ 0, 10 ): 4 4 8 8

Four random values from interval [ 0, 100 ): 22 64 2 80

Enter two integers: 10 100

Four random values from interval [ 0, 10 ): 1 2 8 6

Four random values from interval [ 0, 100 ): 57 63 81 18

Enter four integers: 10 20 75 100

Four random values from interval [ 10 , 20 ): 11 11 19 15

Four random values from interval [ 75 , 100 ): 89 85 76 99

Enter two integers: 10 100

Four random values from interval [ 0, 10 ): 3 5 3 6

Four random values from interval [ 0, 100 ): 29 12 32 5

Enter four integers: 10 20 75 100

Four random values from interval [ 10 , 20 ): 13 14 16 19

Four random values from interval [ 75 , 100 ): 92 98 75 75

Enter two integers: 2 10

Four random values from interval [ 0, 2 ): 1 0 0 1

Four random values from interval [ 0, 10 ): 6 6 2 8

Enter four integers: 0 8 31 39

Four random values from interval [ 0 , 8 ): 7 2 1 3

Four random values from interval [ 31 , 39 ): 37 36 32 37



Program bit_by_bit.py — generating random lists of values

Some possible program runs

How many bits: 8

[1, 0, 0, 0, 0, 0, 1, 1]

How many bits: 8

[1, 0, 0, 1, 1, 0, 0, 1]

How many bits: 28

[0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1]

How many bits: 28

[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1]



Program octane.py

Some possible program runs

Enter three numbers: 1112 12 1

octal digit list: [4, 1, 4, 0, 6, 1, 5, 2, 5, 1, 1, 2]

number of times value 1 in list: 4

Enter three numbers: 1112 12 5

octal digit list: [4, 1, 4, 0, 6, 1, 5, 2, 5, 1, 1, 2]

number of times value 5 in list: 2

Enter three numbers: 8 9 6

octal digit list: [3, 5, 6, 2, 3, 0, 1, 2, 3]

number of times value 6 in list: 1

Enter three numbers: 8 9 6

octal digit list: [3, 5, 6, 2, 3, 0, 1, 2, 3]

number of times value 3 in list: 3




remember to love

 


  © 2020 Jim Cohoon   Resources from previous semesters are available.