Question

In: Computer Science

How would you map invoices by description and quantity, then sort the invoices by quantity? Here...

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

Solutions

Expert Solution

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:


Related Solutions

How do I calculate the total of all invoices? Here is my code: # Part F...
How do I calculate the total of all invoices? Here is my code: # Part F Calculate the total of invoices 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)) print('\nThe total of all invoices is', invoice) for invoice in invoices: print(invoice)
How would you a description of how you would define fuzzy decision making for healthcare administration...
How would you a description of how you would define fuzzy decision making for healthcare administration practice. Then, explain how you might implement fuzzy decision making to evaluate decisions when uncertainty exists. Provide an example where fuzzy decision making might be important for your work or life, and explain why. Be specific and provide examples.
How would I make a bubble sort and an optimized bubble sort with the code given?...
How would I make a bubble sort and an optimized bubble sort with the code given? I also need to implement a timer into each sort and display runtime with the sorts. NODE.H _______________________________________________________________________________________________________ /* node.h */ /* two classes 1: node.h 2. singlylinkedlist.h nod1 (value + pointer) ---> node2 ---> node3 ---> |||| <--- node.h ^ | singlylinkedlist ----------------*node head; */ #ifndef NODE_H #define NODE_H #include <iostream> using namespace std; class Node {    friend class singlyLinkedList; public:   ...
How would you react to the typical “this is how we do things here” response to...
How would you react to the typical “this is how we do things here” response to change? How can you help the employees make their transition from the unawareness stage to the action stage (stages in the acceptance of individual transition)? Specially thanks to the change curve we can predict people’s emotions and reactions to change. in reference with ringtone: exploring the rise and fall of nokia in mobile phones
Java code: Use Radix sort to sort them in alphabetic order. How many passes would be...
Java code: Use Radix sort to sort them in alphabetic order. How many passes would be made through the outer while loop? Trace the contents of the list after each of these passes. Define the efficiency of the following code fragment:             K := 1             repeat                     J := 1                         repeat                              ……..                              J := J * 2                         until J >= N                         K := K + 1                until K >= N
How would the body normally compensate for this change in MAP such as a decrease? targets,...
How would the body normally compensate for this change in MAP such as a decrease? targets, steps pathways end response?
Please let me know how to make code sort. If you put sort (sort 1000 6...
Please let me know how to make code sort. If you put sort (sort 1000 6 5 4 3 2 1, not separate), you will get 1 2 3 4 5 6 1000. sort 50 300 27 5 5 27 50 300 public static void main (String[] args) {        Scanner scnr = new Scanner(System.in);        String a = "";            a = scnr.nextLine();            String[] b = imput.split(" ") if (b[0].equalsI("sort")) { }...
Assume you have a $50,000/year income and live here in the GTA. What sort of financial...
Assume you have a $50,000/year income and live here in the GTA. What sort of financial products would you most likely to invest in, and why? Explain both the pros and cons of your choice. Is your decision based more on rational analysis or emotion?
Note: In case you are not familiar with Boston Beer Company, here is their business description:...
Note: In case you are not familiar with Boston Beer Company, here is their business description: The Boston Beer Company, Inc. and certain subsidiaries (the “Company”) are engaged in the business of selling alcohol beverages throughout the United States and in selected international markets, under the trade names “The Boston Beer Company,” “Twisted Tea Brewing Company,” “Angry Orchard Cider Company,” “Hard Seltzer Beverage Company,” Traveler Beer Co.®, the Angel City Brewing Company®, the Concrete Beach Brewery® and the Coney Island®...
Note: In case you are not familiar with Masco Corporation, here is their business description: Masco...
Note: In case you are not familiar with Masco Corporation, here is their business description: Masco Corporation is a global leader in the design, manufacture and distribution of branded home improvement and building products. Our portfolio of industry-leading brands includes BEHR® paint; DELTA® and HANSGROHE® faucets, bath and shower fixtures; KRAFTMAID® and MERILLAT® cabinets; MILGARD® windows and doors; and HOT SPRING® spas. This is Masco’s income statement. It is similar to Exhibit 2-14 on page 35 of our text. MASCO...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT