collections.Counter()

Sort by

recency

|

1375 Discussions

|

  • + 0 comments
    # Enter your code here. Read input from STDIN. Print output to STDOUT
    
    X = int(input())
    strshoes = input()
    N = int(input())
    
    
    shoe_list = [int(x) for x in strshoes.split(" ")]
    total_price = 0
    
    
    for cus in range(0,N):
      A = input()
      size_price = [int(a) for a in A.split(" ")]
    
      if size_price[0] in shoe_list:
        shoe_list.remove(size_price[0])
        total_price += size_price[1]
    
    print(total_price)
    
  • + 0 comments

    import collections

    X = int(input()) c = collections.Counter(map(int, input().split())) N = int(input()) total = 0

    for i in range(N): s, p = map(int, input().split()) if c[s]: c[s] -= 1 # Reduce stock count total += p

    print(total)

  • + 0 comments

    I try using this way

    import sys
    
    linhas = sys.stdin.read().splitlines()
    amount = 0
    X = int(linhas[0])
    shoes_sizes = linhas[1].split()
    N = linhas[2]
    for linha in linhas[3:]:
        Customer = linha.split()
        shoe_size = Customer[0]
        price = Customer[1]
        if shoe_size in shoes_sizes:
            shoes_sizes.remove(shoe_size)
            amount += int(price)
            X -= 1   
    print(amount)  
    
  • + 0 comments

    Completed using the methods in the documentation:

    import collections
    
    X = int(input())
    c = collections.Counter(map(int, input().split()))
    N = int(input())
    total = 0
    for i in range(N):
        s,p = map(int, input().split())
        if c[s]:
            c.subtract([s])
            c += collections.Counter() # This removes any 0 counts, as mentioned in documentation
            total += p
    print(total)
    
  • + 0 comments
    from collections import Counter
    '''
    # method 1 without Counter:
    def total_money_earned(N,shoes_sizes):
        total = 0
        shoe_size_price = []
        for _ in range(N):
            shoe_size_price = list(map(int,input().split()))
            if shoe_size_price[0] in shoe_sizes:
                total += shoe_size_price[1]
                shoe_sizes.remove(shoe_size_price[0])
                shoe_size_price.clear()
        return total
    '''
    # method 2 with Counter:
    def total_money_earned(N,shoes_sizes):
        total = 0
        shoes= Counter(shoes_sizes)
        shoe_size_price = []
        for _ in range(N):
            size, price = map(int, input().split())
            if shoes[size]: 
                total += price
                shoes[size] -= 1
        return total
    
    if __name__ == "__main__":
        X = int(input())
        shoe_sizes = list(map(int, input().split()))
        N = int(input())
        print(total_money_earned(N,shoe_sizes))