#HackerRank

Pubblicato il: 23 marzo 2025
sul canale di: CodeVisium
69
4

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()


In questa pagina del sito puoi guardare il video online #HackerRank della durata di ore minuti seconda in buona qualità , che l'utente ha caricato CodeVisium 23 marzo 2025, condividi il link con amici e conoscenti, su youtube questo video è già stato visto 69 volte e gli è piaciuto 4 spettatori. Buona visione!