Question

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...

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.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

python code :

Problem 1 : define a function sort_farms_num_of_liv

def sort_farms_assets(raw_farm_data):

#using sorted function and lambda function as a combination

# sorted function takes 3 inputs list,reverse and key where last 2 are optional

return sorted(raw_farm_data,reverse=True,key=lambda raw_farm_data:int(raw_farm_data[6]))
sort_farms_assets(raw_farm_data)

Problem 2 :define A function sort_farms_num_of_liv

def sort_farms_num_of_liv(raw_farm_data):

#using sorted function and lambda function as a combination

# sorted function takes 3 inputs list,reverse and key where last 2 are optional

return sorted(raw_farm_data,reverse=True,key=lambda raw_farm_data:int(raw_farm_data[4]))
sort_farms_num_of_liv(raw_farm_data)

Problem 3 :define A function  farm_to_density

def farm_to_density(raw_farm_data):
#creating a empty list to collect farm name and its density

farm_density_dictionary={}
for i in raw_farm_data:
density=int(i[4])/int(i[3])
farm_density_dictionary[i[0]]=density

return farm_density_dictionary
farm_to_density(raw_farm_data)

Problem 4 :define a function shortage_or_surplus

def shortage_or_surplus(demand_list,supply_list):

#return a list comprehension assuming both input list has equal length and comprises of int value
return [supply_list[i]-demand_list[i] for i in range(len(demand_list))]
demand_list = [300, 200, 900]
supply_list = [600, 850, 100]
shortage_or_surplus(demand_list,supply_list)

sample screenshot

Add a comment
Know the answer?
Add Answer to:
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...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • class BinaryTree: def __init__(self, data, left=None, right=None): self.__data = data self.__left = left self.__right = right...

    class BinaryTree: def __init__(self, data, left=None, right=None): self.__data = data self.__left = left self.__right = right def insert_left(self, new_data): if self.__left == None: self.__left = BinaryTree(new_data) else: t = BinaryTree(new_data, left=self.__left) self.__left = t def insert_right(self, new_data): if self.__right == None: self.__right = BinaryTree(new_data) else: t = BinaryTree(new_data, right=self.__right) self.__right = t def get_left(self): return self.__left def get_right(self): return self.__right def set_data(self, data): self.__data = data def get_data(self): return self.__data def set_left(self, left): self.__left = left def set_right(self, right): self.__right...

  • Previous code: class BinarySearchTree: def __init__(self, data): self.data = data self.left = None self.right = None de...

    Previous code: class BinarySearchTree: def __init__(self, data): self.data = data self.left = None self.right = None def search(self, find_data): if self.data == find_data: return self elif find_data < self.data and self.left != None: return self.left.search(find_data) elif find_data > self.data and self.right != None: return self.right.search(find_data) else: return None    def get_left(self): return self.left def get_right(self): return self.right def set_left(self, tree): self.left = tree def set_right(self, tree): self.right = tree def set_data(self, data): self.data = data def get_data(self): return self.data def traverse(root,order):...

  • class WorkoutClass: """A workout class that can be offered at a gym. === Private Attributes ===...

    class WorkoutClass: """A workout class that can be offered at a gym. === Private Attributes === _name: The name of this WorkoutClass. _required_certificates: The certificates that an instructor must hold to teach this WorkoutClass. """ _name: str _required_certificates: List[str] def __init__(self, name: str, required_certificates: List[str]) -> None: """Initialize a new WorkoutClass called <name> and with the <required_certificates>. >>> workout_class = WorkoutClass('Kickboxing', ['Strength Training']) >>> workout_class.get_name() 'Kickboxing' """ def get_name(self) -> str: """Return the name of this WorkoutClass. >>> workout_class =...

  • PYHTON Preliminaries For this assignment you will be working with the following classes, which are available in the file homework5-classes.py: class Pharmacy: def _init_(self, inventory, unit_prices)...

    PYHTON Preliminaries For this assignment you will be working with the following classes, which are available in the file homework5-classes.py: class Pharmacy: def _init_(self, inventory, unit_prices) self.inventory - inventory self.unit_prices-unit_prices class Prescription: definit_(self, patient, drug_name, quantity): self.patient- patient self.drug_namedrug_name self.quantity - quantity You will be asked to write several functions that work with the Prescription and Pharmacy classes. To complete this assignment you may write any helper functions you like inside your homework5.py file. Pharmacy class attributes: * inventory: A...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT