HackerRank Python - collections.Counter()




collections.Counter()
A counter is a container that stores elements as dictionary keys, and their counts are stored as dictionary values.



  1. # Enter your code here. Read input from STDIN. Print output to STDOUT
  2. from collections import Counter
  3. num_shoes = int(input())
  4. shoe_sizes = Counter(map(int, input().split()))
  5. num_customers = int(input())
  6. cost = 0
  7. for _ in range(num_customers):
  8. size, price = map(int, input().split())
  9. if shoe_sizes[size]:
  10. cost += price
  11. shoe_sizes[size] -= 1
  12. print(cost)




codesadda.com

Codesadda.com is your home of programming solutions, tutorials, video tutorials and much more. Sign Up for our weekly newsletter to get update about new content.

Like us on Facebook | Connect with us on LinkedIn | Subscribe our Channel on Youtube