You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Understand what the interviewer is asking for by using test cases and questions about the problem.
What if I reduce an items stock to exactly 0?
In that case, remove the item from the dictionary -- stock should always be 1 or higher.
P-lan
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Check if the operation is valid, and if so, deduct the stock and return the updated dict.
1) If the item does not exist, return an error message
2) If the quantity requested > stock, return an error message
3) Reduce the item stock by the requested quantity
4) If the stock is reduced to zero, remove from items dict
5) Return updated items dict
I-mplement
defpop_stock(items, item_name, quantity):
ifitem_namenotinitems:
return"Item does not exist."ifquantity>items[item_name]:
return"Not enough stock."items[item_name] -=quantityifitems[item_name] <=0:
items.pop(item_name)
returnitems