def learn_to_program(): return """ CS149 - Introduction to Programming Department of Computer Science James Madison University Spring 2022 """
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.
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 time calculate_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. For example:
if __name__ == '__main__':
print(calculate_earnings(100.0)) # Prints "12.0"
update_return(.06)
print(calculate_earnings(100.0)) # Prints "6.0"
Note: Global variables are ALMOST ALWAYS A BAD IDEA. We are asking you to use them in this exercise to help you understand the difference between local and global scope. In general, if you find yourself using the global
keyword in your code, you should usually rethink your approach.
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
.
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
height
, mass
, and radius
, in that order.mass
and radius
should have default values of MASS_OF_EARTH
and RADIUS_OF_EARTH
, respectively, as defined in orbit.py
.height
should have no default.Note that
Here are some examples illustrating the expected output when orbital_velocity
is executed in the shell:
You should submit orbit.py
.
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.
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:
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: