Output Interpretation

midterm.py

 # Please write your answers to the written problems in this file,

# as comments.  Also, complete the coding questions in this file,

# with the exception of the 4th problem (timer), which should be

# be written in timer.py.

#

# There is another file in the Midterm dropbox, called

# verify_code.py.  It is designed to help you test the

# code that you write in problems 4-6.  You’ll find 5 functions

# in verify_code.py, called test_timer, test_concat, test_remove,

# test_bill, and test_tip.  You will need to call these

# functions from the Idle shell in order to test each portion

# of your code.

####################################################################

#

# Part I:  Written problems

#

####################################################################

# 1.

#

# a.  How many list objects are created by the code below?

#

# >>> x = [ 5 ]

# >>> y = x

# >>> z = y + x

# >>>x.append(10)

#

#

#

#

#

# b.  How many str objects are created by the code below?

#

# >>> x = ‘ab’

# >>> y = x

# >>> z = ‘abc’

# >>> y += ‘c’

#

#

#

#

#

# c.  How many list objects are created by the code below?

#

# >>>lst_one = [1]

# >>>lst_two = [2]

# >>>lst_three = [lst_one, lst_two]

#

#

#

#

#

#

####################################################################

#

# 2. Specify whether an iteration loop or a counter loop

#    should be used to write each function below.  Also,

#    specify if an accumulator variable should be used.

#    You do not need to write complete code for these problems.

#

#    There are 4 possible answers for each question: (a) iteration

#    loop with no accumulator; (b) iteration loop with accumulator;

#    (c) counter loop with no accumulator; (d) counter loop with

#    accumulator.

 

# a. A function is passed a list of floats.  It returns True

#    if all of the floats are positive, or False otherwise.

#

#

#

#

#

# b. A function is passed a list of 1 or more strings.

#    It returns the longest string in the list.

#

#

#

#

#

# c. A function is passed an integer n.  It returns

#    a list of 2-tuples.  For each tuple (x,y) in

#    the returned list, x + y = n.

#    For example:

#

#    >>>tuples(4)

#    [(0, 4), (1, 3), (2, 2), (3, 1), (4, 0)]

#

#

#

#

#

# d. A function is passed a list of integers called lst.

#    It returns a list containg the differences between

#    lst[i] and lst[i-1], for i ranging from 1 to

#    len(lst)-1.  For example:

#    >>> x = [1, 2, 5, 11, 8, 8]

#    >>> differences(x)

#    [1, 3, 6, -3, 0]

#

#    In this case, differences(x) returns [1, 3, 6, -3, 0]

#    because 2-1 = 1, 5-2 = 3, 11-5 = 6, 8-11 = -3,

#    and 8 – 8 is 0.

#

#

#

#

#

#

#

####################################################################

#

# 3. We would like to write an import statement

#    so that we can call the sqrt function in our code.

#    There are 3 ways that we can do this:

import math               # first way

from math import sqrt     # second way

from math import *        # third way

# Explain the differences between each way.  Also, specify how

# you would call sqrt in each of the 3 ways.

#

#

#

####################################################################

#

# Part II:  Coding Problems

#

####################################################################

# 4.  Timer — see the file timer.py.  Write code for

#     this problem in timer.py, so that it is a module.

#

# 5.  We would like to create our own version

#     of Python’s str class.  Our class, which we call String,

#     will have destructive methods called concat and remove

#     which should work as follows:

#

#     >>> s = String(‘abc’)

#     >>>s.concat(‘def’)

#     >>> s

#     ‘abcdef’

#     >>>s.remove(‘cd’)

#     >>> s

#     ‘abef’

#

#     Complete the String class below.

class String:

# __init__, __str__, and __repr__ are already completed.

# You must complete the concat and remove methods

def __init__(self, s):

self.string = s

def __str__(self):

returnself.string

def __repr__(self):

return ‘String(\'{}\’)’.format(self.string)

# COMPLETE THIS METHOD.

# concatenate the parameter string onto the end of

# the current String object (self).

# Do not return anything, but change the internal

# str object within the String.

defconcat(self, second_part):

pass

# COMPLETE THIS METHOD.

# Remove the specified substring (the 2nd parameter)

# from the current String object (self).

# If the current String object does not contain the

# substring, do nothing.  In any case, do not return

# anything, but change the internal str object if

# the substring is there to be removed.

def remove(self, substr):

pass

##########################################################

# 6.  Below is a simple version of the Money

#     class discussed in lecture.  Define the following

#     methods:

#

#     (a) bill.  It should be a non-destructive method,

#     which returns a Money object representing the smallest

#     single bill ($1.00, $5.00, $10.00, $20.00, etc.)

#     that is at least as large as the specified amount

#     of money. Please refer to the verify_code.py file

#     to see examples of how the bill method should behave

#     once it has been completed.

#     (b) tip.  It should be a non-destructive method.  It is

#     passed one parameter, which represents a percentage.

#     It returns a Money object which is the specified percentage

#     of the original amount.  Please refer to the verify.py file

#     to see examples of how the tip method should behave once

#     it has been completed.

class Money:

# __init__, __str__, and __repr__ are already complete.

# You must write bill and tip.

def __init__(self, d, c):

self.dollars = d

self.cents = c

def __str__(self):

return ‘${}.{:02d}’.format(self.dollars, self.cents)

def __repr__(self):

return ‘Money({},{})’.format(self.dollars, self.cents)

# COMPLETE THESE METHODS

def bill(self):

pass

def tip(self, pct):

pass 

README.txt

 Here are the materials for the midterm exam:

midterm.py     — contains short-answer questions and

coding questions.  Please write your

answers to the short-answer questions

in the form of Python comments, and

complete the code in this file.

timer.py       — one question asks you to write code

for the timer module.  Please write it

in this file

verify_code.py — this code can be used to test your work.

It contains functions test_timer, test_concat,

test_remove, test_bill, and test_tip.

You should not modify this file, and you do not

need to turn it in as part of your submission.

timer.py 

# 4. Complete the Timer module below. The module

#    has 3 functions, called timer, tick, and zero. You must write tick

#    and zero.

#    tick subtracts one minute from the time.

#    zero returns True once the timer has reached 0:00, and False

#    otherwise.

#

#    Please refer to the verify_code.py file to see examples of how

#    the Timer class should behave once it has been completed.

#

def Timer(h, m):

return [h, m]

def tick(timer):

‘decrement the timer by one minute’

pass

def zero(time):

‘return True if the timer has reached 0:00 or False otherwise’

return False    # replace this 

verify_code.py 

#######################################

# Test cases for coding problems from

# the midterm.

#

# Below are some examples of using

# the methods that are on the midterm

# exam.  You might find this file

# useful to help check to see if your

# code is correct.

from midterm import *

from timer import *

# Example use of the Timer class

deftest_timer():

t = Timer(1,0)

print(‘The timer is {}’.format(t))              # should print 1:00

print(‘Is the timer zero? {}’.format(zero(t)))  # should print False

tick(t)

print(‘After one tick, the timer is {}’.format(t)) # should print 0:59

for i in range(55):

tick(t)

print(‘After 55 more ticks, the timer is {}’.format(t)) # 0:04

print(‘Countdown to 0:00’)

# This loop should print

# 0:03

# 0:02

# 0:01

# 0:00

for i in range(10):

if zero(t):

break

tick(t)

print(t)

# Example use of the concat method of the String class

deftest_concat():

s = String(‘computer science’)

s.concat(‘ 242 Section 701’)

print(s)

# Example use of the remove method of the String class

deftest_remove():

s = String(‘computer science’)

print(‘s = \'{}\”.format(s))

s.remove(‘ersc’)

print(‘After removing \’ersc\’, s is \'{}\”.format(s))

s.remove(‘python’)

print(‘After removing \’pyton\’, s is \'{}\”.format(s))

deftest_bill():

m = Money(2,25)

# This should print $5.00

print(‘The smallest bill >= {} is {}’.format(m, m.bill()))

m = Money(19,95)

# This should print $20.00

print(‘The smallest bill >= {} is {}’.format(m, m.bill()))

m = Money(20,1)

# This should print $50.00

print(‘The smallest bill >= {} is {}’.format(m, m.bill()))

# Example use of the tip method of the Money

deftest_tip():

m = Money(10,0)  # $10.00

t = m.tip(15)    # 15% tip

print(‘A 15% tip on {} is {}’.format(m,t)) # tip is $1.50

print(t)

m = Money(25,95) # $25.95

t = m.tip(20)    # 20% tip

# tip $5.19, rounded to the nearest penny

print(‘A 15% tip on {} is {}’.format(m,t))

# if you want to test all of your code at once,

# uncomment these lines

##test_timer()

##test_remove()

##test_bill()

##test_tip() 

Solution 

timer.py 

# 4. Complete the Timer module below. The module

#    has 3 functions, called timer, tick, and zero. I have

#    written the timer function for you.  You must write tick and zero.

#    tick subtracts one minute from the time.

#    zero returns True once the timer has reached 0:00, and False

#    otherwise.

#

#    Please refer to the verify_code.py file to see examples of how

#    the Timer class should behave once it has been completed.

#

def Timer(h, m):

return [h, m]

def tick(timer):

‘decrement the timer by one minute’

h, m = timer

if zero(timer):

return

if m:

timer[1] = m – 1

else:

timer[0] = h – 1

timer[1] = 59

def zero(time):

‘return True if the timer has reached 0:00 or False otherwise’

h, m = time

if h or m:

return False

return True