Answered step by step
Verified Expert Solution
Question
1 Approved Answer
class Livestock: def __init__(self,name,price_in,utilizations): self.name,self.price_in,self.utilizations = name,float(price_in),utilizations def __lt__(self,other): if self.utilizations is None: return True elif other.utilizations is None: return False else: return self.utilizations.count(';') <
class Livestock:
def __init__(self,name,price_in,utilizations):
self.name,self.price_in,self.utilizations = name,float(price_in),utilizations
def __lt__(self,other):
if self.utilizations is None:
return True
elif other.utilizations is None:
return False
else:
return self.utilizations.count(';') < other.utilizations.count(';')
def __eq__(self,other):
return self.name == other.name
def __repr__(self):
return ("{}, {}".format(self.name,self.price_in))
raw_livestock_data = [
# name, price_in, utilizations
['Dog', '200.0', 'Draught,Hunting,Herding,Searching,Guarding.'],
['Goat', '1000.0', 'Dairy,Meat,Wool,Leather.'],
['Python', '10000.3', ''],
['Cattle', '2000.75', 'Meat,Dairy,Leather,Draught.'],
['Donkey', '3400.01', 'Draught,Meat,Dairy.'],
['Pig', '900.5', 'Meat,Leather.'],
['Llama', '5000.66', 'Draught,Meat,Wool.'],
['Deer', '920.32', 'Meat,Leather.'],
['Sheep', '1300.12', 'Wool,Dairy,Leather,Meat.'],
['Rabbit', '100.0', 'Meat,Fur.'],
['Camel', '1800.9', 'Meat,Dairy,Mount.'],
['Reindeer', '4000.55', 'Meat,Leather,Dairy,Draught.'],
['Mule','4400.2', 'Draught.'],
]
given the class and raw data, write the following funtions using python:
Function name 1 : livestock_to_occurences
Parameters :
livestock_list_dup (list) A list of Livestock instances with duplicates (i.e.
the name attribute of some Livestock instances
might be the same)
Return Type : dict
Description :
This function should return a dictionary mapping the names of the Livestock in
livestock_list_dup to their corresponding occurrences in the list. For example,
one possibility could be {dog:2, cattle:8, llama:3} , meaning there are
2 dogs, 8 cattles, and 3 llamas in livestock_list_dup .
Function name 2 : remove_dup
Parameters :
livestock_list_dup (list) A list of Livestock instances with duplicates (i.e.
the name attribute of some Livestock instances
might be the same)
Return Type : list
Description :
This function should take in a list of Livestock instances with duplicates and returns a
new list with no duplicate Livestock . Only include the first unique instance of
Livestock in the list returned.
Function name 3: livestock_objs_to_dict
Parameters :
livestock_list (list) A list of Livestock instances
Return Type : dict
Description :
This function should return a dictionary representation of the Livestock instances in
livestock_list . The keys of the dictionary should be the name attributes of the
Livestock instances and the values should be tuples of the price_in attributes and
the utilizations attributes. For example, one possibility could be
{
goat:(1000.0, dairy;leather;meat;wool),
mule:(4400.2, draught)
}
In order to receive full credit for this function you must use a dictionary
comprehension and follow the formatting mentioned above. Failure to do so will
result in an 80% deduction for the points allocated to this function.
Function name 4: livestock_to_util
Parameters :
livestock_list (list) A list of Livestock instances
Return Type : dict
Description :
This function should take in a list of Livestock instances and return a dictionary
mapping the names of the Livestock to their number of potential utilizations. If the
utilizations attribute is the bool None , the corresponding Livestock name should
be mapped to 0.
In order to receive full credit for this function you must use a dictionary
comprehension and follow the formatting mentioned above. Failure to do so will
result in an 80% deduction for the points allocated to this function.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started