collections.Counter() in Python - Hacker Rank Solution


collections.Counter() in Python - Hacker Rank Solution


Problem


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

Sample Code
>>> from collections import Counter
>>> 
>>> myList = [1,1,2,3,4,5,3,2,3,4,2,1,2,3]
>>> print Counter(myList)
Counter({2: 4, 3: 4, 1: 3, 4: 2, 5: 1})
>>>
>>> print Counter(myList).items()
[(1, 3), (2, 4), (3, 4), (4, 2), (5, 1)]
>>> 
>>> print Counter(myList).keys()
[1, 2, 3, 4, 5]
>>> 
>>> print Counter(myList).values()
[3, 4, 4, 2, 1]

Task :

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.



Input Format :

The first line contains X, the number of shoes.
The second line contains the space separated list of all the shoe sizes in the shop.
The third line contains N, the number of customers.
The next N lines contain the space separated values of the shoe size desired by the customer and xi, the price of the shoe.

Constraints :

  • 0 <= X <= 10^3
  • 0 <= N <= 10^3
  • 20 <= xi <= 100
  • 2 <= shoe size <= 20

Output Format :

Print the amount of money earned by Raghu.



Sample 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

Sample Output :

200

Explanation :

Customer 1: Purchased size 6 shoe for $55.
Customer 2: Purchased size 6 shoe for $45.
Customer 3: Size 6 no longer available, so no purchase.
Customer 4: Purchased size 4 shoe for $40.
Customer 5: Purchased size 18 shoe for $60.
Customer 6: Size 10 not available, so no purchase.

Total money earned = 55+45+40+60 = $200



Solution :


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
# Enter your code here. Read input from STDIN. Print output to STDOUT
# collections.Counter() in Python - Hacker Rank Solution START
from collections import Counter
X = input()
S = Counter(map(int,input().split()))
n = input()
N = int(n) 
earnings = 0
for customer in range(N):
    size, x_i = map(int,input().split())
    if size in S and S[size] > 0:
        S[size] -= 1
        earnings += x_i
            
print(earnings)
# collections.Counter() in Python - Hacker Rank Solution END





Disclaimer :-
the above hole problem statement is given by hackerrank.com but the solution is generated by the codeworld19 authority if any of the query regarding this post or website fill the following contact form thank you.

Next Post Previous Post
2 Comments
  • Anonymous
    Anonymous Tuesday, May 17, 2022

    X = int(input())
    s = list(map(int,input().split()))
    N = int(input())

    sum = 0
    for i in range(N):
    cmd = input().split()

    for item in s:
    if item==int(cmd[0]):
    sum+=int(cmd[1])
    s.remove(item)
    print(sum)

    I am getting 210 instead of 200. Any ideas why?

    • Anonymous
      Anonymous Tuesday, May 17, 2022

      X = int(input())
      s = list(map(int,input().split()))
      N = int(input())

      sum = 0
      for i in range(N):
      cmd = input().split()

      for item in s:
      if item==int(cmd[0]):
      s.remove(items)
      sum+=int(cmd[1])

      break

      print(sum)
      mind the spaces

Add Comment
comment url