In: Computer Science
How would you map invoices by description and quantity, then sort the invoices by quantity?
Here is my code:
#Part C: Map each invoice tuple containing part description and then quantity, sort by quantity
invoices=[]
invoices.append((83,'Electric Sander',7,57.98))
invoices.append((24,'Power Saw',18,99.99))
invoices.append((7,'Sledge Hammer',11,21.50))
invoices.append((77,'Hammer',76,11.99))
invoices.append((39,'Jig Saw',3,79.50))
list(map(lambda invoice:invoice[1,2]))
invoices.sort(key=lambda invoice:invoice[2])
print('\nSorted by Quantity')
for invoice in invoices:
print(invoice)
this is python
Hey the question is sort of ambiguous. In your code you're trying to apply map with only one argument. In fact map function applies any function you give in its arguments over the iterable in its arguments. For example map([2,3,-4,5],list.sort) would sort the list in the ascending order. According to me that's not at all what the question is asking. I guess it is asking you to create a HashMap and sort it on the basis of quantity. Code for that is as follows:
*****************************************************************************************************************
invoices=[]
d = {}
inv_map = {}
def into_sorted_dict(invoices):
for invoice in invoices:
d[invoice[1]] = invoice[2]
return list(sorted(d.items(),key = lambda
x:x[1]))
def to_dict(li):
for item in li:
inv_map[item[0]] = item[1]
invoices.append((83,'Electric Sander',7,57.98))
invoices.append((24,'Power Saw',18,99.99))
invoices.append((7,'Sledge Hammer',11,21.50))
invoices.append((77,'Hammer',76,11.99))
invoices.append((39,'Jig Saw',3,79.50))
to_dict(into_sorted_dict(invoices))
print(inv_map)
********************************************************************************************
You would get items in a hashmap sorted on basis of quantity;
Output: