MATH 385 Week 03 Worksheet
Please submit one Python file (worksheet03_solutions.ipynb) by 11:59pm
Pacific time on Friday in Week 03 to your Week 03
GitHub repository.
- Calculate by typing this mathematical expression in the code cell below and then execute the cell and the entire notebook by clicking Rumtime > Run all.
- Create new variable that holds a list that stores one of
each of the following types:
int
,float
,string
,bool
, andlist
. Make up the variable names. - Create a new variable that holds a dictionary that stores at
least three made up pets. The keys of the
dict
should be a pet name and the values should be the type of pet. In this case, both keys and values arestring
s. - Create a list that holds a 5 pet
dict
s as elements. Each petdict
should have keysname
,age
,species
. - Write a function that accepts a
string
and returns the same string with all letters capitalized. - Write a function that accepts a list of
string
s and returns a list of the input strings with all characters capitalized. Call the function appropriately. - Write a function
count_unique(lst)
that counts the unique elements of list argumentlst
. The output should be a dictionary with unique elements as keys and counts as the values. For example,count_unique([1, 1, 2, 2, 2, 3])
should return{1: 2, 2: 3, 3: 1}
. Call the function appropriately. - Write a function
collatz_inner(n)
that accepts a positive integer greater than 1 and returns- half of
n
ifn
is even, or - three times
n
plus one ifn
is odd.
- half of
- Write a function
collatz(n)
that accepts a positive integer greater than 1 and returns of a list of evaluations ofcollatz_inner(n)
untiln
is less than 1. The last element of the returned list should be 1. For example,collatz(2)
should return[1]
, andcollatz(17)
should return[52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1]
. Call the function appropriately. - Write a class
Pet
that stores as internal/private members/properties the animal type, name, and age. The class should have getters that return the animal's species, name, and age. There should be a method speak that returns what your animal says as a string. The variables species, name, age, and the string of what your animal says should all be arguments to the constructor. There should also be a methodcollatz_name
that returns thecollatz(...)
number of the length of the name of your Pet. - Write a function
collatz_pets(lst_pets)
that takes a list ofPet
s and prints calls of the methodcollatz_name
for each pet. - Call the function from 11. on a list of Pets.