Python Homework #2
Instructions:
You will be creating several functions, all in the same.py file.
For each function, please include at least 2 test cases in the following format:
print("fred(18) should be 34, and is: ", fred(18)).
You may comment out the tests after you are done with them.
NOTE: For ALL functions in this homework, use for i in range, not while
- Create the function sumAtoB(a,b), (using for i in range). Assume that a < b. It should return the sum of the numbers from a to b (inclusive)
- Create the function sumAuptoB(a,b). Assume that a < b. It should return the sum of the numbers between a and b, including a and not including b (inclusive/exclusive)
- Create the function sumFiveFromAuptoB(a, b). Assume that a < b, and assume that a is already a multiple of 5. It should return the sum of all multiples of 5 from a to b, (inclusive/exclusive)
- Create the function evaluateCubic(x).
It should evaluate and return the value of
this cubic function
for the specified value of x.
-
Create the function findMaxCubicAuptoB(a,b). Assume that a < b.
Evaluate the cubic function from the previous problem at each integer from a to b (incl./excl.). This function should return
the largest value of the cubic (at integer values of x) over this interval.
- Create the function fizzbuzz(n), which will be given a number n > 1. It will print out all the numbers from 1 to n, except: if a number is divisible by 3, it will print out "fizz" instead, if it's divisble by 5 then "buzz" instead, and if divisible by both, then "fizzbuzz" instead. All other numbers are printed out in numerical form. The function will not return anything (no return statement).
Challenge
Using evaluateCubic, see if you can write a function findRootsCubicAuptoB(a,b) that will return the x-value between a and b at which evaluateCubic(x) is closest to 0.
For a bonus challenge, try looping over finer values of x like a + 0.01, a + 0.02, etc, not just integer values. See if you can use it to find roots of a polynomial like this.