Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a function add _ owners that will match cars with their owners based on a dictionary passed as a parameter. The dictionary uses owners

Write a function add_owners that will match cars with their owners based on a dictionary passed as a parameter. The dictionary uses owners (Person objects) as keys and tuples of Car objects they own, as values. You may/should reuse the previously developed function get_address to retrieve Persons addresses. Note that this function does not print or return anything, its role is to modify existing objects.
Example usage:
>>
houses =[]
h1= House('123 Happy St. Blackwood')
h2= House('6 Agnes Av. Aldinga')
h3= House('8 Daw St. Aldinga')
h4= House('23 Oak St. Blackwood')
h5= House('61 Gorge Av. Aldinga')
h6= House('81 Waterman St. Aldinga')
c1= Car('Ford','Mustang',2)
c2= Car('Ford','Fiesta',4)
c3= Car('Toyota','Camry',2)
c4= Car('Mazda','Cx-9',6)
c5= Car('Mazda','Cx-5',5)
c6= Car('Mitsubishi','Colt',12)
c7= Car('Toyota','Corolla',2)
p1= Person('Jim',28)
p2= Person('Ashley',31)
p3= Person('Cleo',21)
p4= Person('Jessica',18)
p5= Person('Adam',19)
p6= Person('Andrew',34)
p7= Person('Jake',20)
p8= Person('Joshua',17)
p9= Person('Amica',27)
p10= Person('Helen',34)
p11= Person('Barney',29)
p12= Person('Sandra',30)
p13= Person('John',25)
p14= Person('Patrick',26)
p15= Person('Sammy',19)
h1.add_car(c1,c2)
h2.add_car(c3)
h3.add_car(c4)
h4.add_car(c5,c6,c7)
h5.add_car(c8)
h6.add_car(c9,c10)
h1.add_inhabitant(p1,p2,p3,p4)
h2.add_inhabitant(p5,p6)
h3.add_inhabitant(p7,p8,p9)
h4.add_inhabitant(p10)
h5.add_inhabitant(p11,p12)
h6.add_inhabitant(p13,p14,p15)
houses.extend([h1,h2,h3,h4,h5,h6])
d ={p1:(c1,c2),p2:(c3,c4),p3:(c6,c7,c1), p5:c5}
add_owners(houses,d)
print(c1.get_owners_list())
print(c4.get_owners_list())
print(c5.get_owners_list())
output:
[['Jim','123 Happy St. Blackwood'],['Cleo','123 Happy St. Blackwood']]
[['Ashley','123 Happy St. Blackwood']]
[['Adam','6 Agnes Av. Aldinga']]
Here are the Classes:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def __str__(self):
return 'This is '+ self.name +','+ str(self.age)+' years old.'
def is_teenager(self):
return self.age <20
class Car:
def __init__(self, make,model,age):
self.make = make
self.model = model
self.age = age
self.owners ={}
def __str__(self):
return 'This is: '+ self.make +','+ str(self.age)+' years old.'
def add_owner(self,*o):
for i in range(len(o)):
if isinstance(o[i][0],Person) and isinstance(o[i][1],str):
self.owners[o[i][0]]= o[i][1]
else:
print('Error: cannot update the list of car owners')
break
def get_owners_list(self):
o_list =[n.name for n in self.owners.keys()]
return [[x,y] for x,y in zip(o_list,self.owners.values())]
class House:
def __init__(self, address):
self.address = address
self.inhabitants =[]
self.cars =[]
def __str__(self):
return 'This house is inhabited by '+ str(len(self.inhabitants))+' people.'
def add_inhabitant(self,*p):
for i in range(len(p)):
if isinstance(p[i],Person):
self.inhabitants.append(p[i])
else:
print('Error: cannot update the list of inhabitants')
break
def add_car(self,*c):
for i in range(len(c)):
if isinstance(c[i],Car):
self.cars.append(c[i])
else:
print('Error: cannot update the list of cars')
break
def get_inhabitants(self):
inhab_names =[n.name for n in self.inhabitants]
inhab_ages =[n.age for n in self.inhabitants]
return {n:a for (n,a) in zip(inhab_names,inhab_ages)}
def get_cars(self):
car_makes =[c.make for c in self.cars]
car_models =[c.model for c in self.cars]
return [[x,y] for x, y in zip(car_makes,car_models)]

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Hands On Database

Authors: Steve Conger

1st Edition

013610827X, 978-0136108276

More Books

Students also viewed these Databases questions

Question

Compare and contrast a forward contract with a futures contract.

Answered: 1 week ago