-
Notifications
You must be signed in to change notification settings - Fork 243
Build Inventory
Sar Champagne Bielert edited this page Apr 8, 2024
·
5 revisions
Unit 2 Session 1 (Click for link to problem statements)
Understand what the interviewer is asking for by using test cases and questions about the problem.
- What if
product_names
andproduct_prices
are both empty?- In this case, return an empty dict.
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Create a new dictionary using two provided lists for the keys and values
1) Create an empty inventory dict
2) For each index in the input lists
a) Get the name and price at current index
b) Map name -> price in the inventory dict
3) Return the inventory dict
- If you loop through elements in one list, it becomes tricky to find the matching element in the other list. How can you loop through indices instead?
def build_inventory(product_names, product_prices):
inventory = {}
for i in range(len(product_names)):
name = product_names[i]
price = product_prices[i]
inventory[name] = price
return inventory