HW 1
HW 1
HW 1
• variable assignment (optional: you may avoid it by directly returning the results, rather than first
storing them)
• function calls and branching (call with arguments, parameters, return) (although we have built all of
this function structure for you, and you just need to use it)
In this first homework, you will use Python as a calculator, similar to the way you would use your HP
calculator in a physics class. This is already incredibly useful. Even though Python in this homework acts
like a glorified calculator, it is an elegant calculator, with less tedium and more control.
The following syntax is banned for HW1: strings, sequences like lists, recursion.
If you don’t know what some (all?) of these words mean, that is to be expected; we will cover the
meaning of these terms in later lectures. Associated keywords are also banned.
• do not hardcode the test cases into your solutions (this is counterproductive anyway, since we will be
testing your code using different test cases)
Instructions
Each step indicates the relevant section of the primer_3.0, where you can find help on this issue.
To work on HW1:
• in the notebook, edit the first markdown cell, add your full name and blazerid and Run it
• in the notebook, edit the myName function, replacing James Bond with your name (please leave
everything else the same, including the quotes)
• starting with the first problem, solve the rest of the HW1 problems by writing code in the file
hw1.ipynb using the Jupyter Notebook, do not forget to run the cell each time you make a change.
• once you are confident that your code works, submit on Canvas. Do not forget to include your
Independent Completion Form (ICF).
code provided:
def f(x):
return #add your code here
correct answer:
def f(x):
return 5*x - 3
HW1 problems
areaCircle(r)
Write the function areaCircle(r) that takes a positive float value, representing the radius of a
circle, and returns the area of this circle.
Sample Expected
Input: Output (~):
n1 = 4, n2 = 5 20
n1 = 12, n2 = 7 84
n1 = 15, n2 = 10 25
phoneBill (m,tx) Write the function phoneBill(m,tx) that takes two int m and
tx as input and returns a float. The function takes total number of minutes and text messages
that user spends in a particular month and returns the bill amount. These is the policy of the
carrier company:
• The plan includes 50 minutes of airtime and 50 text messages for $15.00 a month.
• Each additional minute of airtime costs $0.25, while additional text messages cost $0.15
each.
• All cell phone bills include an additional charge of $0.44 to support 911 call centers.
• The entire bill (including 911 charge) is subject to 5 percent sales tax.
• The function will take the total number of minutes and text messages and
compute/return the total amount to be paid.
radToDegree(rad)
Write the function “radToDegree” that takes a radian value and converts it to degree. You can check
this link to learn about the radian/degree conversion.
Submission:
Make sure that all your code is correct and producing the correct output. Then, upload your hw1.ipynb
file into Canvas. Do not forget to sign and upload your independent completion form as a pdf.
Pep talk Despite the long discussion of each problem; the actual code is quite short. This is a common characteristic
of good code: lots of thought leading to terse but clear code. In particular, each function in HW1 may be implemented using
one line of code in the body of the function for some questions.