Use of counter() function of collections module

Use of counter() function of collections module

Instructor-svgAl-Mamun Sarkar
Mar 26 , 2020

Use of counter() function of collections module. The following code shows how to store elements as dictionary keys, and their counts as values. The following shows how to solve the HackerRank collections.Counter() Python collections problem.

Code:

from collections import Counter

X = int(input())
shoes = Counter(input().split())

N = int(input())
total = 0
for _ in range(0, N):
	size, price = input().split()
	if shoes[size] > 0:
		total = total + int(price)
		shoes[size] = shoes[size] - 1

print(total)

 

Test 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

Test Output:

200
  • Share On:
  • fb
  • twitter
  • pinterest
  • instagram