Question

in a dictionary whose keys are the warehouses: associated with each warehouse is an inner dictionary...

in a dictionary whose keys are the warehouses: associated with each warehouse is an inner dictionary whose keys are the stocked products (and whose associated values are the inventory of that product in the warehouse). The inventory must always be a non-negative value; an inventory of 0 is legal. For example, a simple/small database might be.

db = {'Irvine' : {'brush': 3, 'comb': 2, 'wallet': 2}, 'Newport': {'comb': 7, 'stapler': 0},
'Tustin' : {'keychain': 3, 'pencil': 4, 'wallet': 3}}

This data structure means that

  1. The Irvine warehouse stocks 3 brushes, 2 combs, and 2 wallets.

  2. The Newport warehouse stocks 7 combs, and 0 staplers.

  3. The Tustin warehouse stocks 3 keychains, 4 pencils, and 3 wallets.

(b) The by_store_inventory function returns a list of str (warehouse names), sorted descending by which warehouses have the largest inventory (summed over all the products). If two warehouses store the same inventory, they should appear in ascending order of warehouse name: for the db dictionary above the result is ['Tustin', 'Irvine', 'Newport'].

0 0
Add a comment Improve this question Transcribed image text
Answer #1

OUTPUT :

CODE :

def by_store_inventory(db):
ldb = [(x,sum(y.values())) for x,y in db.items()]
#ldb stores warehouse,total products tuples
#print(ldb)
#We need to sort it based on total products and warehouse name
ldb.sort(key = lambda x:(-x[1],x[0]))
#print(ldb)
#Return the names
ans = [x[0] for x in ldb]
return ans

#Given Warehouse Inventory list
db = {'Irvine' : {'brush': 3, 'comb': 2, 'wallet': 2}, 'Newport': {'comb': 7, 'stapler': 0},
'Tustin' : {'keychain': 3, 'pencil': 4, 'wallet': 3}}

print(by_store_inventory(db))
  

Add a comment
Know the answer?
Add Answer to:
in a dictionary whose keys are the warehouses: associated with each warehouse is an inner dictionary...
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
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