Homework 5
Objectives
- Demonstrate correct use of docstrings for functions.
- Understand concepts about functions:
- variable scope
- global keyword
- mutable function parameters
- keyword args + defaults
- multiple outputs
Docstrings
For this homework, and all of the remaining homeworks, all of your modules and functions must exhibit correct documentation. Submissions that don’t pass docstring checks will receive no credit.
You should read the Docstring Requirements carefully to see how it should be done.
Exercise 5.1 Investment Return
You have recently made an investment in a dilithium mine that your friend Riker assures you will earn 12% a year. You want to make a calculator in Python to calculate the annual return on your investment.
To build your calculator, create a module called investment.py
. Represent the return on investment as a global variable called percent_return
. Make the initial rate of return 12%.
Then define two functions:
calculate_earnings
, which takes the amount of investment as a float, and returns a float representing the earnings given the return on investment;update_return
, which takes a new rate of return as a float, and returns nothing. After calling this function, the next timecalculate_earnings
is called, it should use the new rate of return.
Your module should not print anything when imported. If you want to test your functions with print statements, make sure to put the print statements inside an if __name__ == '__main__':
block.
Exercise 5.2 Grade Sorting
Your CS 149 professor has a problem. They are planning to transfer student grade data from an old system to a new system, and they want some changes made to the data.
The current data is stored a list of numbers, for instance [95, 82, 96, 73, 100]
. However, the new system needs the data ordered smallest-to-largest, for example: [73, 82, 95, 96, 100]
.
Your professor has written some of the code to order the grades, but they’re stuck on the last part. They’ve asked you to write the last, critical function:
move_min_to_front
– this function should take a list and return a copy with the smallest element moved to the front (position 0). The resulting list should be the same length as the original, and all of the elements should be in the same order except for the smallest one. The function must not change the original list.
Name your module list_utils.py
.
Hint #1: There are a few ways to copy a list; One easy way is the copy
function, for instance: new_list = old_list.copy()
.
Hint #2: The append
method can be used to add an element to the end of a list, but none of the list methods described in our textbook allow us to add an element to the beginning of a list. This can be accomplished using the insert
method. The insert
method takes two arguments: the index where the new element should be placed, and the element to insert. Here is an example:
When you’re done with your function, test it in action by downloading and running your professor’s code: sort_grades.py, which will use your module. If all is working correctly, it should print a list of grades, and then the same list, sorted.
Your code should not print anything out when imported. Submit only list_utils.py
. Do not submit sort_grades.py
.
Exercise 5.3 Orbital Velocity
A satellite in a circular orbit around a planet travels at a predictable velocity, as long as its distance from the planet doesn’t change. The velocity (or more accurately, the magnitude of the velocity) is given by the equation
where
Using orbit.py as your starting point, write a function orbital_velocity
which calculates
- It should take the parameters
height
,mass
, andradius
, in that order. - The parameters
mass
andradius
should have default values ofMASS_OF_EARTH
andRADIUS_OF_EARTH
, respectively, as defined inorbit.py
. - The parameter
height
should have no default.
Note that
Here are some examples illustrating the expected output when orbital_velocity
is executed in the shell:
>>> import orbit
>>> orbit.orbital_velocity(10000) # 10,000 meters above earth
7902.666842384641
>>> mass_of_mars = 6.39 * 10**23
>>> radius_of_mars = 3389500
>>> orbit.orbital_velocity(10000, mass_of_mars, radius_of_mars)
3541.632291490833
You should submit orbit.py
.
Exercise 5.4 More Basketball Stats
In Exercise 3.3, you helped the JMU basketball program display data about the 2020-2021 Women’s Basketball Team. Now they need help again.
They would like a Python function called scoring_stats
that, given a player’s name, will return that player’s name, games played, total score, and score per game.
For instance, for the data: (from the 2019-2020 JMU Men’s Basketball Team)
Name | Games Played | Points Scored |
---|---|---|
Banks | 30 | 362 |
Wilson | 26 | 254 |
Lewis | 30 | 569 |
Parker | 30 | 304 |
Jacobs | 29 | 190 |
scoring_stats('Wilson')
would return the values ('Wilson', 26, 254, 9.7692)
.
Starter code is in more_stats.py.
Exercise 5.5 Fibonacci Numbers
The Fibonacci numbers are a sequence of numbers that starts with two 1s, after which each number is the sum of the two that came before it. For example:
fib0=1
fib1=1
fib2=1+1=2
fib3=1+2=3
fib4=2+3=5
fib5=3+5=8
or
and so on.
You will create a module called fibonacci.py
, which contains two functions:
start_sequence
, which takes no arguments and returns a sequence containing the first two Fibonacci numbers.add_next_number
, which takes a list (which should contain Fibonacci numbers) as an argument, and adds the next Fibonacci number to the end of the list. The function should return nothing.
Here is an example of executing fibonacci.py
in the Thonny shell and then calling the two methods:
>>> %Run fibonacci.py
[1, 1, 2, 3, 5, 8, 13, 21]
>>> import fibonacci
>>> seq = fibonacci.start_fibonacci_sequence()
>>> print(seq)
[1, 1]
>>> fibonacci.add_next_fibonacci_number(seq)
>>> print(seq)
[1, 1, 2]