Function and Module Questions
1. Write a function sum that takes two integer parameters a and b and returns the sum of squares of all the integers n such that a <= n <= b.
2. Write a function triangle that takes three real parameters which represent the lengths of three sides of a triangle. The function should return True if the triangle is right-angled and False otherwise.
3.
a. Write a function factorial that takes a nonnegative integer parameter and returns its facorial; we define 0! = 1.
b. The number e can be defined as the limit of the infinite sum 1/0! + 1/1! + 1/2! + 1/3! + 1/4! + .... Write a function compute_e which takes a positive integer parameter. This parameter indicates the number of terms that are to be summed (i.e. compute_e(3) should produce the sum 1/0! + 1/1! + 1/2!). Sum the terms and return the approximate value of e. Call/use the function you wrote in part a in your solution.
4. You have already written a program that calculates all prime numbers less than 100. Write a function primes that takes one integer parameter and returns the number of primes less than that integer.
5. Write a function solve_quadratic that takes three real parameters a, b, c (in that order). The function should return the roots of the quadratic equation ax**2 + bx + c = 0 as a tuple. If the roots are imaginary, the function should print "No real solutions" and return nothing.
6. Predict the outcome of the following code:
def mystary(a, b):
a += b
b = 5
return a
def foo(a, b):
b -= a
b = 7
a += b
a = 1
b = 2
print a, b
b = mystary(a, b)
print b
a = mystary(a, b)
print a
print a, b
foo(a, b)
print a, b
7. Write a function plot_polynomial that takes two integers and a list as parameters. The list contains the coefficients of the polynomial (with the first corresponding to the coefficient of the term of the highest degree). The two integers are the bounds of the domain of the polynomial. The function should produce a graph of the polynomial.
8. Write a function lissajous that takes four real parameters a, b, c, d. Let a be the amplitude of oscillation in the x direction, b be the angular frequency of oscillation in the x direction, c be the amplitude of oscillation in the y direction, d be the angular frequency of oscillation in the y direction. Assume the oscillations are in phase and described by cosine waves. The function should display the Lissajous figure for this motion on the time interval [0, 1000].
9. Save the code from question 6 of this section into a module called question.py. Now start a new program and import this module. Modify the code from question 6 so that any code that is not a function definition is not executed on the import statement.
10. Turn question 12 in the while loop section into a function called test and save it into a module called tester.py. Import this function into another program and use it to test if a word input by the user is a palindrome.