Header Ad

HackerRank collections.Counter() solution in Python

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)        


Post a Comment

0 Comments