Python program to find how much money will be earned by selling shoes

PythonServer Side ProgrammingProgramming

Suppose in a shoe shop there are n different shoe with different sizes present in an array called size and another list of pairs for m customers called demand is given, where demand[i] contains (shoe_size, money) so a customer with demand i has a demand of shoe whose size is shoe_size and he/she can pay given amount of money. We have to find how much money the shopkeeper can earn by selling these shoes.

So, if the input is like shoes = [2,3,4,5,6,8,7,6,5,18] demand = [(6,55), (6,45), (6,55), (4,40), (18,60), (10,50)], then the output will be 200 because

  • first one will buy shoe with size 6 at cost 55

  • second one will buy shoe with size 6 at cost 45

  • There is no shoe with size 6 in the stock

  • fourth one will buy shoe with size 4 at cost 40

  • fifth one will buy shoe with size 18 at cost 60

  • sixth one will not get shoe because there is no shoe of size 10


Total earn 55 + 45 + 40 + 60 = 200.


To solve this, we will follow these steps −

  • n := size of demand
  • sizes := a map containing frequencies of shoes based on size
  • earn := 0
  • for i in range 0 to n - 1, do
    • (sz, price) := demand[i]
    • if shoe with size sz is present in sizes, then
      • sizes[sz] := sizes[sz] - 1
      • earn := earn + price
  • return earn

Example

Let us see the following implementation to get better understanding

from collections import Counter
def solve(shoes, demand):
   n = len(demand)
   sizes = Counter(shoes)
   earn = 0
   for i in range(n):
      sz, price = demand[i]
      if sizes[sz]:
         sizes[sz] -= 1
         earn += price
   return earn

shoes = [2,3,4,5,6,8,7,6,5,18]
demand = [(6,55), (6,45), (6,55), (4,40), (18,60), (10,50)]
print(solve(shoes, demand))

Input

[2,3,4,5,6,8,7,6,5,18], [(6,55), (6,45), (6,55), (4,40), (18,60),
(10,50)]

Output

200
raja
Published on 11-Oct-2021 12:40:03
Advertisements