def learn_to_program():
    return """  
        CS149 - Introduction to Programming
        Department of Computer Science
        James Madison University
        Spring 2022
    """

Homework #5

Objectives

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:

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:

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.

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:

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

v=GMR+h

where v is the velocity in m/s2, G=6.673×1011Nm2/kg2 is the universal gravitational constant, M is the mass of the planet in kg, R is the average radius of the planet in meters, and h is the satellite’s average height above the planet’s surface in meters.

Using orbit.py as your starting point, write a function orbital_velocity which calculates v for any planet. The function should use the equation above, and should be defined as follows:

Note that G doesn’t change, as it is a universal constant.

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 1,1,2,3,5,8...

and so on.

You will create a module called fibonacci.py, which contains two functions:

Here is an example of executing fibonacci.py in the Thonny shell and then calling the two methods:

>>> %Run fibonacci.py >>> import fibonacci >>> seq = fibonacci.start_fibonacci_sequence() >>> print(seq) [1, 1] >>> fibonacci.add_next_fibonacci_number(seq) >>> print(seq) [1, 1, 2]