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()
En esta página del sitio puede ver el video en línea #HackerRank de Duración hora minuto segunda en buena calidad , que subió el usuario CodeVisium 23 marzo 2025, comparta el enlace con amigos y conocidos, en youtube este video ya ha sido visto 69 veces y le gustó 4 a los espectadores. Disfruta viendo!