Skip to content
Programmingoneonone
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

Learn everything about programming

HackerRank collections.Counter() solution in Python

YASH PAL, 31 July 2024

In this HackerRank collection.counter() problem solution in python, A counter is a container that stores elements as dictionary keys, and their counts are stored as dictionary values.

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.

HackerRank collections.Counter() solution in Python

Problem solution in Python 2 programming.

from collections import Counter

X = int(input())
sizes = Counter(map(int,raw_input().split()))
N = int(input())
earnings = 0

for i in xrange(N):
    size,x = map(int,raw_input().split())
    if sizes[size]>0:
        sizes[size]-=1
        earnings += x
        
print earnings
    


Problem solution in Python 3 programming.

# Enter your code here. Read input from STDIN. Print output to STDOUT
import collections

numShoes = int(input())
shoes = collections.Counter(map(int, input().split()))
numCust = int(input())

income = 0

for i in range(numCust):
    size, price = map(int, input().split())
    if shoes[size]: 
        income += price
        shoes[size] -= 1

print(income)

Problem solution in pypy programming.

import collections

numShoes = int(raw_input())
shoes = collections.Counter(map(int, raw_input().split()))
numCust = int(raw_input())

money = 0 

for i in range(numCust):
    size, price = map(int, raw_input().split())
    if shoes[size]:
        money += price
        shoes[size] -= 1
        

print money
       
    

Problem solution in pypy3 programming.

# Enter your code here. Read input from STDIN. Print output to STDOUT
from collections import Counter

num_shoes = int(input())
sizes = [int(i) for i in input().split()] #2 3 4 5 6 8 7 6 5 18
num_cust = int(input())

d = Counter(sizes)

earn = 0
for i in range(num_cust):
#while(input() != null)    
    req_i = [int(j) for j in input().split()]
    req_i_size = req_i[0]
    if req_i_size in d.keys():
        if d[req_i_size] > 0:
            earn += req_i[1]
            d[req_i_size] -= 1
        
print(earn)        

coding problems solutions Hackerrank Problems Solutions Python Solutions

Post navigation

Previous post
Next post

Are you a student and stuck with your career or worried about real-time things, and don't know how to manage your learning phase? Which profession to choose? and how to learn new things according to your goal, and land a dream job. Then this might help to you.

Hi My name is YASH PAL, founder of this Blog and a Senior Software engineer with 5+ years of Industry experience. I personally helped 40+ students to make a clear goal in their professional lives. Just book a one-on-one personal call with me for 30 minutes for 300 Rupees. Ask all your doubts and questions related to your career to set a clear roadmap for your professional life.

Book session - https://wa.me/qr/JQ2LAS7AASE2M1

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes