Skip to content
Programming101
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • 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
Programming101
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

Topics we are covering

Toggle
  • Problem solution in Python 2 programming.
  • Problem solution in Python 3 programming.
    • Problem solution in pypy programming.
    • Problem solution in pypy3 programming.

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
  • Automating Image Format Conversion with Python: A Complete Guide
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes