We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • HackerRank Home

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Hiring developers?
  1. Prepare
  2. Python
  3. Collections
  4. collections.Counter()
  5. Discussions

collections.Counter()

Problem
Submissions
Leaderboard
Discussions
Editorial

Sort 1117 Discussions, By:

recency

Please Login in order to post a comment

  • joshuabdeckard
    1 week ago+ 0 comments
    from collections import Counter
    X = int(input())
    
    shoes = Counter(map(int,input().split()))
    # "4 5 6 7 7"
    # ["4","5","6","7","7"]
    # [4,5,6,7,7]
    # {4:1,5:1,6:1,7:2}
    
    N = int(input())
    earned = 0
    
    for i in range(N):
        size,price= map(int,input().split())
        if shoes[size] > 0:
            shoes[size]-=1
            earned += price
    
    print(earned)
    
    0|
    Permalink
  • olwethuphakade89
    2 weeks ago+ 0 comments
    from collections import Counter
    
    shoe_total = int(input().strip())
    shoe_list = list(map(int, input().split()))
    customers = int(input())
    count_shoes = Counter(shoe_list)
    total = 0
    
    for _ in range(customers):
        shoe_size, price = list(map(int, input().split()))
        if count_shoes[shoe_size] > 0:
            count_shoes[shoe_size] -=1
            total += price
    
    print(total)
    
    0|
    Permalink
  • willwardlow
    2 weeks ago+ 0 comments
    from collections import Counter
    
    x = int(input().strip())
    shoes = list(map(int, input().split()))
    shoe_counter = Counter(shoes)
    N = int(input().strip())
    
    revenue = 0
    
    for i in range(N):
        size, price = map(int, input().split())
        if shoe_counter[size] > 0:
            revenue+=price
            shoe_counter[size] -= 1
        elif shoe_counter[size] == 0:
            del shoe_counter[size]
            revenue +=0
    
    print(revenue)
    
    0|
    Permalink
  • jarithfry
    2 weeks ago+ 0 comments

    I could have done this in a really short stint of code but wanted to build it more like an actual program so I broke it down into its basics and structured it more like a real world program would be.

    # Enter your code here. Read input from STDIN. Print output to STDOUT
    
    def calculate_total_revenu(shoes_available=None, customers=None):
        '''
            input's
                shoes_available - A dictionary item where key=shoe_size value=number_pairs_available
                customers - A list of customers and their desired shoe size and sale price.
    
            returns
                total_revenue as an integer value.
        '''
        #initialize the total_revenu tag
        total_revenue = 0
        
        if shoes_available is None:
            shoes_available = []
        
        if customers is None:
            customers = {}
        
        for customer in customers:
            # Assign the customer's desired shoe size
            shoe_size = customer[0]
            # Assign the customer's sale price for said shoe.     
            shoe_price = customer[1]
            
            if shoes_available[shoe_size] > 0:
                # Add the shoe price to the total revenue if said size is still available
                total_revenue += shoe_price
                # Remove one shoe from the available sizes if we sold the pair of shoes.
                shoes_available[shoe_size] -= 1
    
        return total_revenue
    
    def read_info_from_STDIN():
        '''
            returns
                tuple (number_of_shoes_avalable, customers)
                number_of_shoes_available --> dict with key=shoe_size value=number_pairs_available
                customers --> list of customers desired shoe size and sale price.
        '''
        from collections import Counter
        
        # Read in the number of shoes from STDIN and convert to integer
        number_of_shoes = int(input())
        # Read the list of shoe sizes from STDIN and convert each size to an integer
        # Create a count of each size available and store it in a dictionary.
        shoes_available = Counter([int(i) for i in input().strip().split()])
        # Read in the list of customer shoe size and price from STDIN and convert to integer.
        # The number of customers is given in advance so the code can grab the correct number of customers.
        customers = [[int(i) for i in input().split()] for _ in range(int(input()))]
        
        return shoes_available, customers
        
    if __name__ == '__main__':
        shoes_available, customers = read_info_from_STDIN()
        print(calculate_total_revenu(shoes_available, customers))
    
    0|
    Permalink
  • h1259611215
    3 weeks ago+ 0 comments
    from collections import Counter
    
    
    n_shoes = int(input())
    sizeList = input().split(' ')
    sizeCounter = Counter(sizeList)
    n_customer = int(input())
    sum_ = 0
    for _ in range(n_customer):
        desire_list = input().split(' ')
        if desire_list[0] in sizeCounter.keys() and sizeCounter[desire_list[0]] != 0:
            sum_ += int(desire_list[1])
            sizeCounter[desire_list[0]] -=1
    print(sum_)
        
    
    0|
    Permalink
Load more conversations

Need Help?


View editorial
View top submissions
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy