"""
Project1 : Basic Python
=======================

Hand in answers to Part A and code listings for Part B. 

Part 1: Questions
-----------------

Make sure you number all your answers.

1a. What is printed by the Python code?
"""

1a

 #Write a Python program that prompts the user for
 #two numbers, reads them in, and prints out the
 #product, . Hint: raw_input, eval

#Given a string s, write a short expression for a
#string that includes s repeated five times.

#Suppose you know x is an integer and ys is a string
#representing an integer. For instance, x is 3 and
#y is '24'. Write code to print out the arithmetic sum of the two. 
#In the example case, 27 would be printed.


#Suppose you are given a list of words,
#wordList. Write Python code that will write one
#line for each word, repeating that word twice. For example if wordList is ['Jose', 'Sue', 'Ivan'], then your code would print
Jose Jose
Sue Sue
Ivan Ivan

Write code to create a Python dictionary (the dict type). Add two entries to the dictionary: Associate
the key ‘name’ with the value ‘Juan’, and associate the key ‘phone’ with ‘508-1234’

Complete the code for the following function so it matches its documentation:

def doubleList(numberList):
   """For each of the numbers in the list numberList, print a line
       containing twice the original number. For example,
      doubleList([3, 1, 5]) would print 
      6
      2
      10
    """

Assuming a function process is already defined, write two lines of code, using a for-loop, that is equivalent to the following:
process('Joe')
process('Sue')
process('Ann')
process('Yan')

2. The following
code defines the start of a class to represent bank accounts:

class BankAccount(object):
  interest_rate = 0.3
  def __init__(self, name, number, balance):
    self.name = name
    self.number = number
    self.balance = balance


a) Name the class variables and the instance variables in the given code. [5 marks]
b) Add instance methods called deposit() and withdraw() which increase and
decrease the balance of the account. Make sure the withdraw() method doesn't allow
the account to go into overdraft. Add a third method called add_interest() which adds
interest to the balance (the interest should be the interest rate multiplied by the current
balance). 

c) Create a subclass of BankAccount called StudentAccount. Every
StudentAccount should have an overdraft limit of 1000. Write a constructor for the new
class. Override the withdraw() method to make sure that students can withdraw money
up to their overdraft limits.


3. This question tests your ability to manipulate strings.
A palindromic word is one that reads the same backwards as forwards. Hence the words
hello and peel are not palindromes, but the words peep, deed and dad are palindromes.
a) Create a class called Palindrome.
b) In your Palindrome class, create a method called reverse() which takes a string
argument. Your method should return the reverse of the argument as a string. For
example, if the argument is Foobar then your method should return rabooF . 
c) Create a second method in Palindrome called isPalindrome() which takes a string
argument. This method should return True if the argument is a palindrome and False
otherwise. 
d) Write some code to test your new Palindrome class and print out results of your testing
to the user. Give some consideration to what sort of strings you might want to use for your
testing.


This question tests your understanding of iteration (e.g. loops and generators). Remember,
you will not be marked down for small errors in syntax or typos, so long as you
demonstrate that you understand how iteration works in programming languages.

a) A program to sum the number of integers from 1 to a given number n. 
b) A program which sums the contents of a integer list or array. 
c) Translate the following for loop into a while loop (which does the same thing as the
for loop):
  for i in range(1,10):
      print "i = ", i

d) Translate the following while loop into a for loop (which does the same thing as the
while loop):

   i = 20
   while (i > 0):
     print "i = ", i
     i =- 1


a) The following iterative function returns the sum of all elements in a list. For example,
given the list [1, 2, 3] the function will return 1+2+3 or 6. Re-write the function as a
recursive function.

def sum(arg):
  result = 0
  for i in arg:
    result += i
    return result


b) The following code implements a recursive function in Python called foobar. What
does the foobar function do? Write a line of code which calls the foobar function with a
suitable argument and state what the return value will be.

  def foobar(arg):
    if arg == []:
      return arg
   else:
     return foobar(arg[1:]) + [ arg[0] ]
