DEV Community

Cover image for collections.Counter() | HackerRank | Python
Retiago Drago
Retiago Drago

Posted on

collections.Counter() | HackerRank | Python

The Problem

Raghu is a shoe shop owner with X number of shoes available. He has a list containing the size of each shoe in his inventory. There are N customers, each willing to pay x1x_1 amount of money, but only if they get the shoe of their desired size.

Your task is to calculate the total amount of money Raghu will earn based on these conditions.

The Input

The first line contains X, the total number of shoes.
The second line contains a space-separated list of all the shoe sizes in the shop.
The third line contains N, the total number of customers.
The next N lines contain space-separated values of the shoe size desired by the customer and x1x_1 , the price they are willing to pay for the shoe.

Constraints

  • 0<X<1030 \lt X \lt 10^3
  • 0<N1030 \lt N \le 10^3
  • 20<x1<10020 \lt x_1 \lt 100
  • 2<shoesize<202 \lt shoe size \lt 20

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
Enter fullscreen mode Exit fullscreen mode

The Output

The output should be the total amount of money earned by Raghu.

Sample Output:

200
Enter fullscreen mode Exit fullscreen mode

Explanation

Customer 1: Purchased size 6 shoe for $55.
Customer 2: Purchased size 6 shoe for $45.
Customer 3: Size 6 is 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 is not available, so no purchase.

Total money earned =55+45+40+60=200= 55 + 45 + 40 + 60 = 200

The Solution

To solve this problem, we need to keep track of the shoe sizes available and sell them to customers if we have their desired size. This can be accomplished efficiently using the Python collections.Counter class, which counts the number of occurrences of elements in a list.

The Code

Here is the Python code that solves this problem:

from collections import Counter as ctr

_ = input()
shoe_sizes_counter = ctr(map(int, input().split(' ')))
earning = 0

for _ in range(int(input())):
    size, price = tuple(map(int, input().split(' ')))
    if shoe_sizes_counter.get(size, 0):
        earning += price
        shoe_sizes_counter[size] -= 1

print(earning)
Enter fullscreen mode Exit fullscreen mode

This code maintains a counter of available shoe sizes. For each customer, it checks whether the desired shoe size is available. If so, the customer's price is added to the total earnings and the shoe size is decremented from the counter. The final earnings are then printed.

Original Source

For more insightful solutions and tech-related content, feel free to connect with me on my Beacons page.

ranggakd - Link in Bio & Creator Tools | Beacons

@ranggakd | center details summary summary Oh hello there I m a an Programmer AI Tech Writer Data Practitioner Statistics Math Addict Open Source Contributor Quantum Computing Enthusiast details center.

favicon beacons.ai

Top comments (0)