USE! PYTHON!!! Problems: Use the usual naming scheme. Make sure you include the
ID: 3662005 • Letter: U
Question
USE! PYTHON!!!
Problems:
Use the usual naming scheme. Make sure you include the name(s) of any collaborators, and the results of running the doctest on your solutions.
1. Implement a container class Stat that stores a sequence of numbers and provides statistical information about the numbers. It supports an overloaded constructor that initializes the container either using a list or with no parameter which creates an empty sequence. The class also includes the methods necessary to provide the following behaviors:
>>> s = Stat()
>>> s.add(2.5)
>>> s.add(4.7)
>>> s.add(78.2)
>>> s
Stat([2.5, 4.7, 78.2])
>>> len(s)
3
>>> s.min()
2.5
>>> s.max()
78.2
>>> s.sum()
85.4
>>> s.mean()
28.46666666666667
>>> s.clear()
>>> s
Stat([])
If a Stat is empty, several (but not all) methods raise errors. Note that you won’t literally see “…”. You will instead see more information on the error.
>>> s = Stat()
>>>
>>> len(s)
0
>>> s.min()
Traceback (most recent call last):
...
EmptyStatError: empty Stat does not have a min
>>> s.max()
Traceback (most recent call last):
...
hw3.EmptyStatError: empty Stat does not have a max
>>> s.mean()
Traceback (most recent call last):
...
hw3.EmptyStatError: empty Stat does not have a mean
>>> s.sum()
0
2. Implement a class intlist which a list that stores only integers. You MUST subclass list. Please note the following:
constructor –can be passed a list of ints, or, by default constructs an empty intlist.
< >, insert,extend – can also be used to add ints to an intlist. If you don’t know how extend works for a list, look it up. All should raise errors if a non-int is added.
< > - can be used for item assignment using an index. Raises error if non-int is used.
< > – write a method odds() which returns an intlist consisting of the odd int’s. They should not be removed from the original.
< > – same as odds(), but for even ints
< > – also write an Exception class NotIntError that subclasses Exception.
< > – a NonIntError should be raised when client code attempts to place something other than an int in an intlist. This can happen in three ways (all shown in code below):
< >
< >
The constructor – when passed a list that contains something other an int
Note: you can check whether item is an int by evaluating the expression type(item)==int
Your goal is to get the following behavior:
>>> il = intlist()
>>> il
intlist([])
>>> il = intlist([1,2,3])
>>> il
intlist([1, 2, 3])
>>> il.append( 5 )
>>> il
intlist([1, 2, 3, 5])
>>> il.insert(1,99)
>>> il
intlist([1, 99, 2, 3, 5])
>>> il.extend( [22,44,66] )
>>> il
intlist([1, 99, 2, 3, 5, 22, 44, 66])
>>> odds = il.odds()
>>> odds
intlist([1, 99, 3, 5])
>>> evens = il.evens()
>>> evens
intlist([2, 22, 44, 66])
>>> il
intlist([1, 99, 2, 3, 5, 22, 44, 66])
>>> il[2] = -12 # calls __setitem__
>>> il
intlist([1, 99, -12, 3, 5, 22, 44, 66])
>>> il[4] # calls __getitem__
5
Trying to put anything except for an int into an intlist will always raise an NotIntError. Note that there 5 different ways this could be attempted:
>>> il.append(33.4)
Traceback (most recent call last):
...
NotIntError: 33.4 not an int
>>> il.insert(2,True)
Traceback (most recent call last):
...
NotIntError: True not an int
>>> il = intlist([2,3,4,"apple"])
Traceback (most recent call last):
...
NotIntError: apple not an int
>>> il.extend( [2,3,'hello'])
Traceback (most recent call last):
...
NotIntError: hello not an int
>>> il[2] = 22.3
Traceback (most recent call last):
...
NotIntError: 22.3 not an int
Explanation / Answer
Good Wishes,
Program 1:
class Stat(object):
def __init__(self,seq1= None):
if seq1 is None:
self.seq=set()
else :
self.seq=seq1
def add(self,a):
try
self.seq.add(a)
except:
print("stat empty")
def min(self):
try:
print min(self.seq)
except:
print ("empty stat does not have a min")
def max(self):
try:
print max(self.seq)
except:
print("empty stat dose not have max")
def sum(self):
try:
print sum(self.seq)
except:
print("no sum stat empty")
def mean(self):
try:
l=len(self.seq)
s=sum(self.seq)
print (s/l)
except:
print("empty stat has no mean")
def clear(self):
try:
print self.seq.clear()
except:
print("no clear stat empty")
S1=Stat()
S2=Stat({1,2,3})
S1.add(23)
S2.add(23)
print S1.seq
print S2.seq
print len(S2.seq)
S2.min()
S2.max()
S2.sum()
S2.mean()
S2.clear()
print S2.seq
print len(S2.seq)
S2.min()
S2.max()
S2.sum()
S2.mean()
S2.clear()
Program 2:
class intlist(object):
def __init__(self,l=None):
if l is None:
self.il=[]
else:
self.il=l
def append(self,a):
self.il.append(a)
print self.il
def extend(self,a):
self.il.extend(a)
print self.il
def insert(self,index,a):
self.il.insert(index,a)
print self.il
def odds(self):
c=[]
for l in self.il:
if(l%2!=0):
c.append(l)
print c
def evens(self):
c=[]
for l in self.il:
if(l%2==0):
c.append(l)
print c
ilist=intlist()
ilist.append(4)
ilist.extend([5,7,3])
ilist.insert(2,9)
ilist.odds()
ilist.evens()
As per the time alloted to me I did the first question completely but in the second question I did with the code to do all the operations mentioned but the exception handling was not included
just include the below part in your code:
class NotIntError(Exception):
def __init__(self, arg):
self.args = arg
and then include try except block in all the methods of the class
try:
# test each element in list anf if not an integer then raise an exception as follows
raise NotIntError(i+" is not an integer")
# if element is integer then exceute the code here
except NotIntError,e:
print e.args
Hope this is clear.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.