collections.Counter()

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