In this collection.counter() problem we need to develop a python program that can read a list and then we need to print a dictionary that contains the number and number of occurrence on the output screen.


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)