Chp 2 Functions

Instructions

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.

For each, I've provided a few examples of function calls; these are the Test Cases. Run your function and then type the Test Case function calls into the console. Compare to my  outputs.

I've provided function skeletons for you. Copy and then paste them into a new project.

Below all your functions, paste in the unit test. Note that the unit test will crash if not all  functions are defined. 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.

Quotient and Remainder

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

Circle Area

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

Fahrenheit to Celsius

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

Celsius to Fahrenheit

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

Weighted Average

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

What Do I Need on the Final?

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

Equation of a Line

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.

Cube Root

Write me a function named cubed_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:

>>> cubed_rt(8)

2.0

>>> cubed_rt(12) 

2.2894284851066637

>>> cubed_rt(144) 

5.241482788417793

nth Root

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

Distance

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

Absolute Value

I'll often use the format below to describe the function I want you to write.

Do not use the built-in absolute value function. Do not import a module. Do not use the if-elif-else structure. 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

Digit Chop

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.

Test Cases:

>>> digit_chop(123, 1)

3

>>> digit_chop(123, 2)

2

>>> digit_chop(123, 3)

1

24 Hour Clock

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

Change

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)

Distance from Point to Line (Stretch)

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.)

Test Cases:

>>> pt_to_line(2, -2, 0, -12)

10.0

>>> pt_to_line(-6, 1, 5, 12)

3.7262065676