Lab 4

Class and recursion
  1. Write a Python class called Client.

    Each instance of the class should have the attributes name, address and balance. The instantiation method should have input determining the customer name and address. It should also set the balance to zero. In addition to constructor and __str__ method for output, each instance should have access to methods debit and credit to change the balance.

    Test the class by rewriting SBapp program using a list to store and process Client objects with the following features required:

    Make sure your class and program includes a meaningful docstring.

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

    Include doc string comment in Rational class. After testing, use pydoc to generate class documentation in HTML format.

  3. A Caesar Cipher is a way of sending secret messages. It uses an integer between 1 and 26 as a key. Each letter in a message is moved forward by that many letters (with letters at the end of the alphabet going to the start). For example, if the word is "yellow" and the key 3 then the message becomes "bhoorz". Write a script which when run first asks the user for an integer between 1 and 26 (the key). It should allow the user to either encode a message or decode a message. It should allow the user to try unlimited messages until they type exit.

    Modify "cgilogon.py" in Lab 2 to make use of the Cipher function in checking user password.

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

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

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

    LevelOutput
    1
    3
    5

    Using Point class from Lab 3, write Python code to draw the Sierpinski Triangle for a specified order.