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
- Dataset access and analysis
- Introduce randomness using Python module
random
Special download
- Module url.py
- Defines function
url.get_contents(
link
)
- Returns the contents of the web resource indicated by parameter
link
as a (normal) string.
- In the future more capabilities will be added.
Downloads for today
- Program lotta_books.py
- Program pick_a_number.py
- Program chosen.py
Downloads for interested students (and I hope you are all interested).
- Program bit_by_bit.py
- Program octance.py
To do list
- Review class artifacts.
- Complete homeworks
- Prepare for Test 1 (10 / 2 / 2020)
For the fun of it
- Reveal one of your super power(s).
- Share a selfie.
Review
- A dataset is a collection of informations arranged as list of rows, where each row is a list of cells (dataset value).
- There are two basic ways of accessing the rows in the dataset
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)
- There are two basic ways of accessing the individual cells in a dataset
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
- Create a copy of a column of a dataset
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
- Examines a literal dataset based on the web dataset best_sellers.csv
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
- Python has a standard module random with extensive capabilities for generating pseudo-random values.
- Why say pseudo-random? Since we are using algorithm there is some determinism involved.
- There are algorithms that can generate values that appear to be so random that can fool everyone (at least for now).
- The module section of our website explains a subset of that module.
- There are three
random
module functions of particular interest to us now.
- Function
choice()
: choose value from a string, list, set, or tuple
- Function
seed()
: specifically prime the random number generator
- Function
randrange()
: choose value from a range interval
Program chosen.py
- Demonstrates some simple usage of
random
module functionchoice()
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
- Demonstrates some simple usage of
random
module functionrandrange()
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
- Generates a list of
n
bits (binary digits) for a user-suppliedn
.
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
- Prompts and gets three user-supplied integers
s
,n
, andd
. Uses integers
as a seed to the Python random number generator.
- Accumulates a list of
n
octal digits (base 8 digits; that is digits from the interval 0 to 7) one by one. Afterwards, prints the list of octal digits.
- Determines and prints the number of occurrences of
d
in the list of octals (check out the list module for a helpful function.
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
© 2020 Jim Cohoon | Resources from previous semesters are available. |