Lab 6
Rational Class, recursion and testing
-
A rational number is any number that can be expressed as the quotient or fraction n/d of two integers.
Write a Python class called Rational. Every Rational object consists of two attributes n and d representing the numerator and denominator.
-
Note that n and d should both be integers. Ensure this by having the instantiation method throw an appropriate exception otherwise.
- Further, d could not be zero and should always be positive. Extend the instantiation method so that if d is zero an exception is thrown; while if d is negative the equivalent object is created with attributes -n, -d.
- Write a __str__ method that converts a Rational instance to the string "n/d".
- Use names in Python arithmetic methods to implement the appropriate arithmetic special methods for the class Rational.
Recall the following rules for arithmetic with fractions.
- Note that different Rational objects can be mathematically equal.
For example, 2/4 = 1/2. In general n1/d1 = n2/d2 if and only if n1*d2 = n2*d1.
Write a method that takes two Fraction objects; and returns True is they are mathematically equal and False otherwise.
Give it the appropriate special method name so that Python will use it when checking equality of two Rational objects with ==.
- You could write similar methods for checking whether one fraction is bigger or smaller than another.
- All equivalent mathematical fractions can be simplifed to a unique form by cancelling common factors in the numerator and detonator.
Write a function to reduce the rational to its lowest form. Make this a method of the class and run it as part of the instantiation of any Rational object.
Include doc string comment in Rational class. After testing, use pydoc to generate class documentation in HTML format.
- In Python we calculate powers using the symbol **. In this question we consider how the Python interpretor may have
implemented this: do not use ** in your solution. The mathematical definition of taking a power is np = n*np - 1 for positive integer p.
Use this to create a recursive function power(n,p) which calculates np.
- A palindrome is a word (or any other sequence of symbols) which reads the same forward and backward, for example ada,abba,level, etc.
Write a recursive function isPalindrome(S) which returns a Boolean determining whether the string S is a palindrome.
You should make use of argument in main() to test your function.
- A Sierpinski Triangle of order 0 is just an equilateral triangle (all sides the same length).
An order 1 Sierpinski Triangle is created by 3 smaller triangles coming together so their edges form the single larger one.
An order 2 Sierpinski Triangle repeats this process within the 3 smaller triangles. Examples of level 1, 3, and 5 Sierpinski Triangle are shown below.
Using Point class from Lab 3, write Python code to draw the Sierpinski Triangle for a specified order.
- By comparing output results of operations using the fractions.Fraction class in Python, write a new tester program to test Rational class in Q1 above and report your testing.
Write another tester program to sort a list of Rational objects using Python sorted() and sortAndSearchAPI function in Lab6 folder.
- Rewrite the palindrome checker program in Q2 above to use doctest module and docstring to assure the function is correct.
Write a tester program using unittest module for testing.
- Given currencyAPI in Lab6 tutorial folder, add exception handling to catch errors you find or anticipate e.g.
user enters nothing or an invalid currency code. For those errors that should rise to the user, create meaningful error messages.
Write a test plan and report your testing.