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()
Auf dieser Seite können Sie das Online-Video #HackerRank mit der Dauer stunde minuten sekunde in guter Qualität ansehen, das der Benutzer CodeVisium 23 März 2025 hochgeladen hat, den Link mit Freunden und Bekannten teilen, dieses Video wurde auf Youtube bereits 69 Mal angesehen und es wurde von 4 den Zuschauern gefallen. Viel Spaß beim Betrachtenden Zuschauern gefallen!