Class 10 – Wednesday, September 18

Being loopy / That's so random

You did not expect – The repetition to come – You did not expect


Look both ways


Agenda


Warning


Hoos got your back — food insecurity


Downloads







To do list


Proof dogs are not as smart as they think they are

csi dog



Answer accumulation

Some possible program runs

Enter word: hannah

hannah

Enter word: reverse

esrever

Enter word: abcdefghijklmnopqrstuvwxyz

zyxwvutsrqponmlkjihgfedcba



List basics

s = "we are in it together"

values = [ ]

stuff = [ 'abc', 1112, 2.71, ]

digits = [ 3, 1, 4, 1, 5, 9, 2, 6 ]

words = s.split()

print( "values =", values )

print( "stuff =", stuff )

print( "digits =", digits )

print( "words =", words )

Program run

values = []

stuff = ['abc', 1112, 2.71]

digits = [3, 1, 4, 1, 5, 9, 2, 6]

words = ['we', 'are', 'in', 'it', 'together']


s = "we are in it together"

values = [ ]

stuff = [ 'abc', 1112, 2.71, ]

digits = [ 3, 1, 4, 1, 5, 9, 2, 6 ]

words = s.split()

vlen = len( values )

slen = len( stuff )

dlen = len( digits )

wlen = len( words )

print( "size of", values, "=", vlen )

print( "size of", stuff, "=", slen )

print( "size of", digits, "=", dlen )

print( "size of", words, "=", wlen )

dmax = max( digits )

dmin = min( digits )

wmax = max( words )

wmin = min( words )

print( "max of", digits, "=", dmax )

print( "min of", digits, "=", dmin )

print( "max of", words, "=", wmax )

print( "min of", words, "=", wmin )

Program run

size of [] = 0

size of ['abc', 1112, 2.71] = 3

size of [3, 1, 4, 1, 5, 9, 2, 6] = 8

size of ['we', 'are', 'in', 'it', 'together'] = 5

max of [3, 1, 4, 1, 5, 9, 2, 6] = 9

min of [3, 1, 4, 1, 5, 9, 2, 6] = 1

max of ['we', 'are', 'in', 'it', 'together'] = we

min of ['we', 'are', 'in', 'it', 'together'] = are


values = [ ]

values.append( "u" )

values.append( "v" )

values.append( "a" )

values.append( "!" )

values.append( " " )

values.append( "u" )

values.append( "v" )

values.append( "a" )

values.append( "!" )

print( "values =", values )

ints = []

for i in range( 0, 10 ) :

  ints.append( i )

print( "ints =", ints

Program run

values = ['u', 'v', 'a', '!', ' ', 'u', 'v', 'a', '!']

ints = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]


Processing a numeric list

reply = input( 'Enter a list of numbers: ' )

slist = reply.split()

print( 'reply =', reply )

print( 'slist =', slist )

# first set up a holder of elements

nlist = [] # initialize holder to prepare for adding elements

# now get conversions of the elements of slist one-by-one

for s in slist :

  # get numeric equivalent of s

  nbr = int( s )

  # add the equivalent to the numeric list

  nlist.append( nbr )

  # print the updated list

  print( nlist )

Some possible program runs

Enter a list of numbers: 3 1 4 1 5 9

reply = 3 1 4 1 5 9

slist = ['3', '1', '4', '1', '5', '9']

nlist = [3]

nlist = [3, 1]

nlist = [3, 1, 4]

nlist = [3, 1, 4, 1]

nlist = [3, 1, 4, 1, 5]

nlist = [3, 1, 4, 1, 5, 9]

Enter a list of numbers: 58 2 37 16 99 1 9 23

reply = 58 2 37 16 99 1 9 23

slist = ['58', '2', '37', '16', '99', '1', '9', '23']

nlist = [58]

nlist = [58, 2]

nlist = [58, 2, 37]

nlist = [58, 2, 37, 16]

nlist = [58, 2, 37, 16, 99]

nlist = [58, 2, 37, 16, 99, 1]

nlist = [58, 2, 37, 16, 99, 1, 9]

nlist = [58, 2, 37, 16, 99, 1, 9, 23]


reply = input( 'Enter a list of numbers: ' )

slist = reply.split()

nlist = []

for s in slist :

  nbr = int( s )

  nlist.append( nbr )

# by accumulation get the sum of the numbers

total = 0

for nbr in nlist :

  total = total + nbr

# print summation

print( "sum(", nlist, "):", total )

Some possible program runs

Enter a list of numbers: 3 1 4 1 5 9

sum( [3, 1, 4, 1, 5, 9] ): 23

Enter a list of numbers: 58 2 37 16 99 1 9 23

sum( [58, 2, 37, 16, 99, 1, 9, 23] ): 245



Introducing module random

random number generator as black box process



Some possible 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: 2 10

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

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

Enter four integers: 0 8 31 39

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

Four random values from interval [ 31 , 39 ): 33 33 38 34

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



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



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]



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



Decrementing range (for the interested student)

Some possible program runs

Enter starting and ending numbers for the countdown: 10 1

10

9

8

7

6

5

4

3

2

1

Blast off!

Enter starting and ending numbers for the countdown: 15 5

15

14

13

12

11

10

9

8

7

6

5

Blast off!




Coney Island Cyclone