Write each of the functions described below. Place them all in a single file. Use the function names given. Do pay close attention to the order in which I describe the parameters; that will be the order in which they should be found in the function header.
I've helped you out - I've provided function skeletons for you. These are a start for each function you are to write. You'll simply need to flesh them out. So to begin, copy and paste the function skeletons into a file for Chapter 2. Each function skeleton has the name of the function, but it includes none of its parameters, and it does not include its return statement. You'll find that the body of each skeleton function is simply pass. This is a genuine line of code, but it's a line that does nothing. I include it only because Python's syntax requires at least one line of code in the body of any function.
Below all your functions, paste in the unit test at GitHub. (The unit test is also available at CodeHS.) Note that the unit test will crash if not all functions are defined. (They'll all be defined if you pasted in the skeleton functions.) The unit test will send inputs to your functions, compare their outputs to the outputs I know are correct, and then generate a score. The unit test will write its report to a file named "report.txt". Read it! You may run the unit test as many times as you like; each time you do, a new report will be generated.
If all you've done is paste in the function skeletons, the unit test should give you 0 points. As you write the functions, that should increase.
Last, note that after each function description I've provided a few test cases. Recall that you can import a function from a file into the Python console by means of the command >>> from <fileName> import <functionName>. Once you've done this for a particular function, you can then attempt to replicate my outputs.
Remember what those mean? Perhaps not. An example: when we divide 29 by 8, the quotient is 3 and the remainder is 5. Another: when we divide 32 by 5, the quotient is 6 and the remainder is 2. In general, the quotient of m and n is the number of whole n's in m, and the remainder is how much of m remains after that number of n's is removed. Note that the remainder will always be less than n.
Write me a pair of functions, one named quotient and the other remainder. Each takes a pair of positive integers. The first should return their quotient and the second their remainder.
The first lines of your functions might be def quotient(m, n): and def remainder(m, n): - indeed the only way in which they might differ from this is in the choice of parameter names.
Test Cases:
>>> quotient(12, 2)
6
>>> quotient(12, 3)
4
>>> quotient(12, 4)
3
>>> quotient(12, 5)
2
>>> quotient(12, 6)
2
>>> quotient(12, 7)
1
>>> remainder(12, 2)
0
>>> remainder(12, 5)
2
>>> remainder(12, 6)
0
>>> remainder(12, 7)
5
Write me a function named circle_area that takes the length of a circle's radius and returns the area of that circle.
Add (and then use) this line of code to the body of your function: PI = 3.14159.
The first line of your function might be def circle_area(radius):.
Test Cases:
>>> circle_area(1)
3.14159
>>> circle_area(5)
78.53975
Write me a function named f_to_c that takes a temperature given in Fahrenheit and returns the conversion to Celsius.
Test Cases:
>>> f_to_c(212)
100.0
>>> f_to_c(32)
0.0
>>> f_to_c(102.5)
39.16666666666667
Write me a function named c_to_f that takes a temperature given in Celsius and returns the conversion to Fahrenheit.
Test Cases:
>>> c_to_f(100)
212.0
>>> c_to_f(0)
32.0
>>> c_to_f(39.16666666666667)
102.50000000000001
In my Honors Geometry, the semester average is computed in this way: 40% for each of the two quarters and 20% for the final exam. Write a function named hg_grade that takes two quarter grades and a final exam grade and returns the semester average. Assume that all are given as percents.
Do pay close attention to the order of the parameters. I said "a function that takes two quarter grades and a final exam grade". I meant in that order. So in the first example below, 98 is the first quarter grace, 87 is the second quarter grade and 81 is the final exam grade.
Test Cases:
>>> hg_grade(98, 87, 81)
90.2
>>> hg_grade(77.2, 90.1, 88.5)
84.62
Assume that same formula as above: semester average = 40% of quarter 1 + 40% of quarter 2 + 20% of final. Write a function named grade_needed that takes the desired semester average and the two quarter scores and returns the grade needed on the final to get that desired semester average. As before, work with percents.
(I'm often asked about the math of this. Here's what you need to do before you write any code: take the equation sem_avg = 0.4 * q1 + 0.4 * q2 + 0.2 * f and solve for f. I mean algebra. Likely done on a piece of paper.)
Test Cases:
>>> grade_needed(90, 88, 76)
121.99999999999996
>>> grade_needed(80, 88, 76)
71.99999999999996
Imagine that we place a arrow with its endpoint at the origin pointed in the direction of the positive x-axis and allow it to rotate with its endpoint fixed. Let a counterclockwise rotation produce an angle of positive measure, and let a clockwise rotation produce an angle of negative measure. So for instance, if we rotate the ray 90°, it will point directly upwards, and it we rotate it -180°, its direction will be reversed.
We can, if we wish, rotate the ray more than one complete rotation, where a complete rotation of course corresponds to 360°. If we do, the ray will come to occupy a position that it did once before. For instance, if we rotate 450°, the ray ends at the position that it occupied when it had swept out 90°.
Note as well that every rotation that produces a negative angle measure leaves the ray in the same position as would have a positive rotation. For instance, a rotation of -90° leaves the ray where it would have been had we rotated 270°.
For a given rotation of x°, let the reference angle of that rotation be a rotation between 0° and 360° that would have left the ray in the same position as the rotation of x°. Write a function named reference_angle that takes a given rotation in degrees and returns the reference angle of that rotation.
Test Cases:
Write a function named linear_eq that returns the y-coordinate of a point on a line given the slope of the line, the y-intercept of the line and the x coordinate of the point. (Don't know about slope and y-intercept? I beg to differ. You have seen it. Perhaps a quick Google search will jog your memory ... )
Test Cases:
>>> linear_eq(-2.5, 12, 0)
12.0
>>> linear_eq(-2.5, 12, 12)
-18.0
>>> linear_eq(5, -3.3, 9.7)
45.2
I said "given slope, y-intercept and x coordinate", and I meant in that order. So in the first example above, slope was -2.5, y-intercept was 12 and x-coordinate was 0.
Write me a function named cube_rt that takes a number and returns its cube root.
Don't import any modules. (Don't know what that means? Don't worry about it; you will.) You'll need a fractional power - the square root of a number is that number raised to the one-half power.
Test Cases:
>>> cube_rt(8)
2.0
>>> cube_rt(12)
2.2894284851066637
>>> cube_rt(144)
5.241482788417793
Write me a function name nth_rt that takes two numbers m and n and returns the nth root of m. So for instance if you send it 12 and 3 (in that order), it will return the cube root of 12.
Test Cases:
>>> nth_rt(1, 1)
1.0
>>> nth_rt(3, 3)
1.4422495703074083
>>> nth_rt(5, 7)
1.2584989506418267
Write a function named distance that computes the distance between a pair of points. The function will have four inputs: the x- and y-coordinates of point one and the x- and y-coordinates of point two. Use the sq_rt function you defined above. (You knew, didn't you, that later functions can make use of earlier ones. Think of functions as extensions of Python.) What's the distance formula? A quick Google search will answer that question.
Test Cases:
>>> distance(1, 1, 2, 2)
1.4142135623730951
>>> distance(-12.3, 14.0, 87.8, -99.9)
151.63515423542128
I'll often use the format below to describe the function I want you to write.
Function name: abs_val
Parameter(s): a number
Return value: the absolute value of the argument
Do not use the built-in absolute value function. Do not import a module. Do not use the if-elif-else structure. (If you don't know what this is, you will soon. It's the topic of Chapter 4.) If you're clever (and you are), you'll make use of your previously defined sq_rt function.
Test Cases:
>>> abs_val(0)
0.0
>>> abs_val(3)
3.0
>>> abs_val(-3)
3.0
Write me a function named digit_chop that takes two integers m and n and returns the nth digit from the right of m. For all you Pythonistas out there, I forbid you to use any bit of Python that I haven't yet introduced. So no iteration and no selection. Hint: // and %.
Test Cases:
>>> digit_chop(123, 1)
3
>>> digit_chop(123, 2)
2
>>> digit_chop(123, 3)
1
On a 24-hour clock, 11 is 11am, 23 is 11pm, and 0 is midnight. If the time is currently 22 and you set your alarm to go off in 8 hours, the alarm time will be 6. If the time is currently 13 and you set your alarm to go off in 50 hours, the alarm time will be 15 (3 pm).
Write a function named alarm_clock that takes the current hour (on a 24-hours clock) and the number of hours until the alarm should go off and returns the alarm time. The current hour will lie between 0 and 23 inclusive. So too will the alarm time.
Test Cases:
>>> alarm_clock(8, 9)
17
>>> alarm_clock(8, 19)
3
>>> alarm_clock(18, 29)
23
Efficient cashiers use the fewest number of coins possible when they give change. Write a function named make_change that takes as argument the number of cents in change. (Assume that it will be greater than or equal to 0 and less than 100.) Return the number of quarters, dimes, nickels and pennies that will be given in change by an efficient cashier.
What? Multiple return values? How?
Like this:
return qs, ds, ns, ps
Ain't Python grand? You'd hope that this would work, and it does! (More on multiple return later.)
Test Cases:
>>> make_change(99)
(3, 2, 0, 4)
>>> make_change(50)
(2, 0, 0, 0)
>>> make_change(41)
(1, 1, 1, 1)
If you're stuck on how to do write make_change, ask yourself these questions: How would I find the number of whole quarters that go into a given number of cents? and How would I find out who much remains after I takes out that number of quarters?
Each chapter will have at least one problem that's meant to really stretch you. You might not get it even if you put in the work. Such is life. I do however guarantee that if you get all the others done, you'll still get an A. Not a perfect, but an A. (Perfection is often not a realistic goal. I aim to teach you that.)
Function name: pt_to_line
Parameter(s): the x- and y-coordinates of a point, the slope and y-intercept of a line (so that's a total of four values)
Return value: the distance from the given point to the given line
Test Cases:
>>> pt_to_line(2, -2, 0, -12)
10.0
>>> pt_to_line(-6, 1, 5, 12)
3.7262065676