Solution - In Class Activity - Python

In this notebook we will try to put some of the Python knowledge you acquired into use. For each exercise, you will see some code that has some bugs or asks you to write a little program. Simply fix the code cell to make code that runs correctly, or write the code to specification. Then add a markdown cell below your answer that says what you changed and why, or explains what your code is doing in English.

Importing and using existing functions

Problem 0: Random number generation

The following code imports the random number functionality in Python from the numpy library and uses to do generate random numbers. Why won’t this code run?

import numpy.random as npr

print("My random number is ", numpy.random.random())
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-1-fb1545c1a95a> in <module>
      1 import numpy.random as npr
      2 
----> 3 print("My random number is ", numpy.random.random())

NameError: name 'numpy' is not defined
# Solution 1
import numpy.random as npr
print("My random number is ", npr.random())
My random number is  0.5185895455925532
# Solution 2
import numpy
print("My random number is ", numpy.random.random())
My random number is  0.3396647640432414

Problem 1: Random sample

The next line chooses a random sample of 5 elements from a list. What is wrong here?

import numpy.random as npr

my_list = list(range(100)
               
npr.choice(my_list,size=5,replace=False)
  File "<ipython-input-4-01dc1d56f8a4>", line 5
    npr.choice(my_list,size=5,replace=False)
      ^
SyntaxError: invalid syntax
# Solution
import numpy.random as npr
my_list = list(range(100))            
npr.choice(my_list,size=5,replace=False)
array([55, 97, 22, 18, 25])

Problem 2: Looking things up

In the above example when you get it to work what does the choice() function do? The documentation for this function is available by googling for “numpy.random choice”. Add your answer in a cell below. Then look up the random() function used in Problem 0. What does it do?

Answer

choice(a) picks random elements of a 1D array a. You can choose how many elements to pick with size, and whether or not to sample with replacement based on replace

random() generates a random number uniformly from 0 to 1

Writing a function

In the problem that remain you will need to write little function that accomplish various goals. Let’s verify your understanding of functions.

Problem 3: Defining a function

What does this function do? Write a new cell below the function definition that calls the function using five different inputs and verify the outputs match your expectation.

def my_function(x, y):
    return x+y
# Solution : the function adds two numbers together!
print(my_function(0,10))
print(my_function(1,10))
print(my_function(1,4))
print(my_function(2,4))
print(my_function(1,5))
10
11
5
6
6

Problem 4: What is not great about this function?

This function works but is not ideal, why?

def my_function_2(x, y, z):
    return x+y

# Solution : the argument z is doing nothing

Problem 5: Write you own function

Write your own custom function named catchy_name() to write a program that takes as input two arguments which are assumed to be strings. Return a new string which is the concatenations of the first two letters of each of the inputs. e.g.

catchy_name("South","Houston") -> "SoHo"
catchy_name("Jennifer", "Lopez") -> "JeLo"
# Solution
def catchy_name(name1,name2):
    return name1[:2] + name2[:2]

print(catchy_name("South","Houston"))
print(catchy_name("Jennifer", "Lopez"))
SoHo
JeLo

Strings

Problem 6: Cat-Dog

Write a function called catdog that returns True if a input string given has the word cat and dog an equal number of times. For example,

cat_dog('catdog') # shoudl return True
cat_dog('catcat') # should return false
cat_dog('1cat1cadodog') # should return true

Google ‘python count occurrences in string’ to find a existing function of method that counts the number of times a substring appears in a string to get started.

# Solution
def cat_dog(s):
    ncat = s.count('cat')
    ndog = s.count('dog')
    return ncat==ndog

print(cat_dog('catdog'))
print(cat_dog('catcat'))
print(cat_dog('1cat1cadodog'))
True
False
True

Lists

Problem 7: Summing

Write a function call summing() that takes a list of numbers as input and return the sum of all the numbers. Use a for loop to do this.

summing([1, 2, 3]) → 6
summing([5, 11, 2]) → 18
summming([7, 0, 0]) → 7
# Solution
def summing(mylist):
    mysum = 0
    for x in mylist:
        mysum += x
    return mysum

print(summing([1, 2, 3]))
print(summing([5, 11, 2]))
print(summing([7, 0, 0]))
6
18
7

Problem 8: Biggest difference

Given an array length 1 or more of ints, return the difference between the largest and smallest values in the array. Note: the built-in min and max functions.

big_diff([10, 3, 5, 6]) → 7
big_diff([7, 2, 10, 9]) → 8
big_diff([2, 10, 7, 2]) → 8
# Solution
def big_diff(mylist):
    return max(mylist)-min(mylist)

print(big_diff([10, 3, 5, 6]))
print(big_diff([7, 2, 10, 9]))
print(big_diff([2, 10, 7, 2]))
7
8
8