I am preparing for a technical round and while preparing I encountered this problem through leetcode’s interview questions section.
My solution can take 3 items in its input dict anything less than that it throws error.
I would also like to know what do you think the ranking of this question will be in terms of LC easy, medium and hard if it was actually in the problems section of LC.
PROBLEM:
Juan Hernandez is a Shopify merchant that owns a Pepper sauce shop
with five locations: Toronto, Vancouver, Montreal, Calgary and Halifax.
He also sells online and ships his sauces across the country from one
of his brick-and-mortar locations.
The pepper sauces he sells are:
Jalapeño (J)
Habanero (H)
Serrano (S)
The inventory count for each location looks like this:
City J H S
Toronto 5 0 0
Vancouver 10 2 6
Montreal 3 5 5
Calgary 1 18 2
Halifax 28 2 12
Every time he gets an online order, he needs to figure out
which locations can fulfill that order. Write a function that
takes an order as input and outputs a list of locations which
have all the items in stock.
Example
Input : J:3. H:2 s:4
Output: Van, Mon, Hali
Input: H:7 S:1
Output: Cal
My Solution:
inven = {
'tor': {'j':5,'h':0,'s':0},
'van': {'j':10,'h':2,'s':6},
'mon': {'j':3,'h':5,'s':5},
'cal': {'j':1,'h':18,'s':2},
'hal': {'j':28,'h':2,'s':12},
}
order = {
'j':3,
'h':2,
's':4
}
def find_order(order):
output = []
for city in inven:
if order['j'] <= inven[city]['j'] and order['h'] <= inven[city]['h'] and order['s'] <= inven[city]['s']:
output.append(city)
return output
print(find_order(order))
Sorry, if the answer is something super easy. I am still kinda new to coding and its my first technical round.
I only know python as of now. If its not your language, a hint toward the right direction will be very helpful.
2
Answers
Your solution looks very close to ok. I’m guessing by less then 3 items you mean that not all types of sauces are present in the order. To fix the error that you get in that case you can just check if the dict contains all expected keys (‘j’, ‘h’ and ‘s’), and if some of them are missing, insert them with the value of 0.
Here’s a way to do it:
Output:
Explanation:
city
that satisfies a conditionSome help from the docs:
all()
dict.items()