collections.Counter()

Sort by

recency

|

1411 Discussions

|

  • + 0 comments

    Here is the most optimised and cleaner code (without constraint) for this problem:-

    from collections import Counter
    no_of_shoes = int(input())
    size_list = map(int, input().split(" "))
    total_customers = int(input())
    
    size_dict = Counter(size_list)
    
    earning = 0
    for j in range(total_customers):
        i = list(map(int, input().split(" ")))
        if size_dict[i[0]]:
            earning += i[1]
            size_dict[i[0]] -= 1
    
    print(earning)
    

    `

  • + 0 comments
    from collections import Counter
    total_shoes = int(input())
    shoe_sizes = [int(i) for i in input().split()]
    shoe_sizes = Counter(shoe_sizes)
    customer = int(input())
    profit = 0
    for i in range(0,customer):
        each_cus = [int(i) for i in input().split()]
        if shoe_sizes.get(each_cus[0]) is not None and shoe_sizes.get(each_cus[0]):
            shoe_sizes.update({each_cus[0] : -1})
            profit += each_cus[1]
            
    print(profit)
    
  • + 1 comment

    Why all test cases are not getting passed, can anyone shed some light on my mistake.

    from collections import Counter
    
    x = int(input())
    s = list(map(int,input().split()))
    n = int(input())
    c = set()
    
    for i in range(0, n):
        j = list(map(int,input().split()))
        c.add(tuple(j))
    
    c = list(c)
    for i in range(0,len(c)):
        c[i] = list(c[i])
        
    d = dict(Counter(s))
    
    su = 0
    
    for i in range(0,len(c)):
        if c[i][0] in d.keys() and d[c[i][0]]>0:
            d[c[i][0]] -= 1
            su = su + c[i][1]     
    
    print(su)       
    
  • + 0 comments
    from collections import Counter
    if __name__=='__main__':
        X,shoe_sizes,N=int(input()),Counter(list(map(int, input().split()))),int(input())
        earnings=0
        for _ in range(N):
            size,price= map(int, input().split())
            if size in shoe_sizes.keys() and shoe_sizes[size]>0:
                earnings+=price
                shoe_sizes[size]-=1
            
        print(earnings)
    
  • + 0 comments
    x = int(input())
    shoe_sizes = list(map(int, input().split()))
    n = int(input())
    l1 = []
    for i in range(n):
        l1.append(list(map(int,input().split())))
    
    from collections import Counter
    avail_shoe = Counter(shoe_sizes)
    cost = 0
    for i in l1:
        if avail_shoe[i[0]] > 0:
            cost += i[1]
            avail_shoe[i[0]] = avail_shoe[i[0]] - 1
            
    print(cost)