In this video, we break down the Shoe Shop problem step-by-step using Python’s collections.Counter.
Steps Explained:
Input Reading:
Read the total number of shoes and the list of shoe sizes.
Build an inventory using collections.Counter.
#InputHandling #Counter
Processing Customers:
Loop through each customer's request.
Check if the desired shoe size is available; if so, add the price to the total earnings and update the inventory.
#Looping #ConditionalLogic
Output:
Print the total earnings after all transactions.
#Output #EarningsCalculation
Copy and paste the code below into your Jupyter Notebook and practice along with the detailed solution!
Code for Detailed Version:
===============================
HackerRank Python: Shoe Shop Problem (Detailed Version)
===============================
Step 1: Read the number of shoes and build the inventory using collections.Counter.
Step 2: Process each customer's purchase:
Check if the desired shoe size is available, then update earnings and inventory.
Step 3: Print the total money earned.
from collections import Counter
def shoe_shop_long():
Read the total number of shoes
n = int(input().strip())
Read the list of shoe sizes and create an inventory counter
shoes = list(map(int, input().split()))
inventory = Counter(shoes)
Initialize total earnings
total = 0
Read the number of customers
customers = int(input().strip())
Process each customer's request
for _ in range(customers):
size, price = map(int, input().split())
If the desired shoe size is available, process the sale
if inventory[size] v 0:
total += price
inventory[size] -= 1 # Reduce inventory for that shoe size
Print the total earnings
print(total)
To run the detailed solution, call:
if _name_ == '__main__':
shoe_shop_long()
On this page of the site you can watch the video online #HackerRank with a duration of hours minute second in good quality, which was uploaded by the user CodeVisium 23 March 2025, share the link with friends and acquaintances, this video has already been watched 69 times on youtube and it was liked by 4 viewers. Enjoy your viewing!