collections.Counter()

Sort by

recency

|

1409 Discussions

|

  • + 0 comments

    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)
    
  • + 0 comments

    Way 1

    from collections import Counter
    
    X = int(input())
    shoe_sizes = Counter(map(int,input().split()))
    N = int(input())
    total_price = 0
    for i in range(N):
        size, price = list(map(int,input().split()))
        if shoe_sizes[size] !=0:
            total_price+= price
            shoe_sizes[size] -= 1
            
    print(total_price)
    

    Way 2

       X = int(input())
    shoe_sizes = list(map(int,input().split()))
    
    unique = set(shoe_sizes)
    count_dict = {size: 0 for size in unique}
    
    for shoe_size in shoe_sizes:
        count_dict[shoe_size] +=1
    
    N = int(input())
    total_price = 0
    for i in range(N):
        size, price = list(map(int,input().split()))
        if size in count_dict.keys():
            if count_dict[size] !=0:
                total_price+= price
                count_dict[size] -= 1
            
    print(total_price)    
    
  • + 0 comments

    from collections import Counter

    X = int(input()) sizes = list(map(int, input().split())) customers = int(input()) orders = list()

    for i in range(customers): x, y = map(int, input().split()) orders.append({x: y})

    sizes_count = Counter(sizes)

    list_prices = list()

    for order in orders: k, v = list(order.items())[0] if (k in sizes_count.keys()) & (sizes_count[k] > 0): sizes_count[k] -= 1 list_prices.append(v)

    print(sum(list_prices))