Table of Contents
ToggleQuestion
A counter is a container that stores elements as dictionary keys, and their counts are stored as dictionary values.
Sample Code
>>> from collections import Counter
>>>
>>> myList = [1,1,2,3,4,5,3,2,3,4,2,1,2,3]
>>> print Counter(myList)
Counter({2: 4, 3: 4, 1: 3, 4: 2, 5: 1})
>>>
>>> print Counter(myList).items()
[(1, 3), (2, 4), (3, 4), (4, 2), (5, 1)]
>>>
>>> print Counter(myList).keys()
[1, 2, 3, 4, 5]
>>>
>>> print Counter(myList).values()
[3, 4, 4, 2, 1]
Task
Raghu is a shoe shop owner. His shop has X number of shoes.
He has a list containing the size of each shoe he has in his shop.
There are N number of customers who are willing to pay xi amount of money only if they get the shoe of their desired size.
Your task is to compute how much money Raghu earned.
Input Format
The first line contains X, the number of shoes.
The second line contains the space separated list of all the shoe sizes in the shop.
The third line contains N, the number of customers.
The next N lines contain the space separated values of the shoe size desired by the customer and xi, the price of the shoe.
Constraints
0 < X < 10^3
0 < N <= 10^3
20 < xi < 100
2 < shoe size < 20
Output Form
Output Format
Print the amount of money earned by Raghu.
Sample Input
10
2 3 4 5 6 8 7 6 5 18
6
6 55
6 45
6 55
4 40
18 60
10 50
Sample Output
200
Explanation
Customer 1: Purchased size 6 shoe for $55.
Customer 2: Purchased size 6 shoe for $45.
Customer 3: Size 6 no longer available, so no purchase.
Customer 4: Purchased size 4 shoe for $40.
Customer 5: Purchased size 18 shoe for $60.
Customer 6: Size 10 not available, so no purchase.
Total money earned = 55 + 45 + 40 + 60 = $200
Solution
from collections import Counter
# Input
X = int(input()) # Number of shoes
shoe_sizes = list(map(int, input().split())) # List of shoe sizes
N = int(input()) # Number of customers
# Count the occurrences of each shoe size
shoe_inventory = Counter(shoe_sizes)
# Calculate the total money earned
total_earnings = 0
for _ in range(N):
size, price = map(int, input().split())
if shoe_inventory[size] > 0: # If the requested shoe size is available
total_earnings += price
shoe_inventory[size] -= 1 # Decrease the count of that shoe size
# Output
print(total_earnings)
We start by importing the
Counter
class from thecollections
module. This class is used to count the occurrences of elements in a list efficiently.The first part of the code handles the input. We read the following values from the input:
X
: The number of shoes in the shop.shoe_sizes
: A list of shoe sizes in the shop.N
: The number of customers.- For each customer, we read their requested
size
and theprice
they are willing to pay.
Next, we create a
Counter
object calledshoe_inventory
from theshoe_sizes
list. This will count the occurrences of each shoe size in the inventory.We initialize a variable
total_earnings
to keep track of the total money earned by Raghu. Initially, it is set to 0.We then iterate
N
times, once for each customer. Inside the loop, we do the following:- Read the
size
andprice
requested by the customer. - Check if the requested
size
is available in theshoe_inventory
by checking if its count is greater than 0. - If the requested size is available, we add the
price
tototal_earnings
and decrement the count of that shoe size inshoe_inventory
by 1.
- Read the
Finally, we print the value of
total_earnings
, which represents the amount of money earned by Raghu.
If you find anything wrong in this Solution, feel free to reach us in the comment section.