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.
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 STDOUTdefcalculate_total_revenu(shoes_available=None,customers=None):'''input'sshoes_available-Adictionaryitemwherekey=shoe_sizevalue=number_pairs_availablecustomers-Alistofcustomersandtheirdesiredshoesizeandsaleprice.returnstotal_revenueasanintegervalue.'''#initialize the total_revenu tagtotal_revenue=0ifshoes_availableisNone:shoes_available=[]ifcustomersisNone:customers={}forcustomerincustomers:# Assign the customer's desired shoe sizeshoe_size=customer[0]# Assign the customer's sale price for said shoe. shoe_price=customer[1]ifshoes_available[shoe_size]>0:# Add the shoe price to the total revenue if said size is still availabletotal_revenue+=shoe_price# Remove one shoe from the available sizes if we sold the pair of shoes.shoes_available[shoe_size]-=1returntotal_revenuedefread_info_from_STDIN():'''returnstuple(number_of_shoes_avalable,customers)number_of_shoes_available-->dictwithkey=shoe_sizevalue=number_pairs_availablecustomers-->listofcustomersdesiredshoesizeandsaleprice.'''fromcollectionsimportCounter# Read in the number of shoes from STDIN and convert to integernumber_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)foriininput().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)foriininput().split()]for_inrange(int(input()))]returnshoes_available,customersif__name__=='__main__':shoes_available,customers=read_info_from_STDIN()print(calculate_total_revenu(shoes_available,customers))
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
collections.Counter()
You are viewing a single comment's thread. Return to all 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.