Day 11 - pre-class assignment

SOLUTIONS

Goals for today's pre-class assignment

  • Use random number generators to create a sequence of random floats and integers
  • Create a function and use it to do something.

Assignment instructions

Watch the videos below, read through Section 4.6, 4.7.1, and 4.7.2 of the Python Tutorial, and complete the programming problems assigned below.

This assignment is due by 11:59 p.m. the day before class, and should be uploaded into the "Pre-class assignments" dropbox folder for Day 11. Submission instructions can be found at the end of the notebook.


In [ ]:
# Imports the functionality that we need to display YouTube videos in a Jupyter Notebook.  
# You need to run this cell before you run ANY of the YouTube videos.

from IPython.display import YouTubeVideo

In [ ]:
# WATCH THE VIDEO IN FULL-SCREEN MODE

YouTubeVideo("fF841G53fGo",width=640,height=360)  # random numbers

Some possibly useful links:

And some interesting links on what you use random numbers to do in programming:

Question 1: Using the Python random module, first seed the random number generator with a number of your choosing and then use a loop to create and print out several floating-point numbers whose values are between 5 and 10. Verify that if you re-run this code several times, the random numbers stay the same, and that if you comment out the code they change!


In [ ]:
# put your code here.

import random

random.seed(8675309)

for i in range(10):
    x = 5 + 5.0*random.random()
    print(x)

In [ ]:
# WATCH THE VIDEO IN FULL-SCREEN MODE

YouTubeVideo("o_wzbAUZWQk",width=640,height=360)  # functions

Question 2: Give a function a list of floating-point numbers and have it return the min, max, and average of them as three separate variables. Store those in variables and print them out.


In [ ]:
# put your code here.

mylist = [2.7, 3.5, 5.7, 9.1, 10.3, -7.0]

def getvals(thislist):
    
    min = thislist[0]
    max = thislist[0]
    avg = 0.0
    
    for val in thislist:
        if val < min:
            min = val
        if val > max:
            max = val
        avg += val
            
    avg /= len(thislist)
    
    return min,max,avg

min,max,avg = getvals(mylist)

print(min,max,avg)

Question 3: Give a function a list of floating-point numbers and have it return either the min, max, or mean value, depending on an optional keyword (that you give it as a second argument). The default should be to provide the mean value. Store that in a variable and print it out.


In [ ]:
# put your code here

mylist = [2.7, 3.5, 5.7, 9.1, 10.3, -7.0]

def getvals(thislist, return_quant='mean'):
    
    min = thislist[0]
    max = thislist[0]
    avg = 0.0
    
    for val in thislist:
        if val < min:
            min = val
        if val > max:
            max = val
        avg += val
            
    avg /= len(thislist)
    
    if return_quant == 'min':
        return min
    elif return_quant == 'max':
        return max
    elif return_quant == 'mean':
        return avg
    else:
        print("I don't understand your keyword:", return_quant)
        return -99999
    
returnval = getvals(mylist,'min')

print(returnval)

Assignment wrapup

Please fill out the form that appears when you run the code below. You must completely fill this out in order to receive credit for the assignment!


In [ ]:
from IPython.display import HTML
HTML(
"""
<iframe 
	src="https://goo.gl/forms/rTmsyHG72q8pF0cT2?embedded=true" 
	width="80%" 
	height="1200px" 
	frameborder="0" 
	marginheight="0" 
	marginwidth="0">
	Loading...
</iframe>
"""
)

Congratulations, you're done!

Submit this assignment by uploading it to the course Desire2Learn web page. Go to the "Pre-class assignments" folder, find the dropbox link for Day 11, and upload it there.

See you in class!