- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python program to find how much money will be earned by selling shoes
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
- Related Articles
- C++ Program to find out the maximum amount of money that can be made from selling cars
- C++ code to find money after buying and selling shares
- Program to find maximum profit by selling diminishing-valued colored balls in Python
- Program to find maximum profit we can make by buying and selling stocks in Python?
- Program to find maximum profit we can make by holding and selling profit in Python
- How much compound interest is earned by investing $Rs. 20000$ for $6$ years at $5$ % per annum compounded annually?
- Program to find maximum profit we can get by buying and selling stocks with a fee in Python?
- Mr Bhaskar saves Rs 8756 every month. How much money will he save in 13 years?
- The marked price of a pair of shoes is Rs 5000 and the shopkeeper allows 15% discount . Find the selling price of shoes after discount.
- Program to find number of items left after selling n items in python
- Mr. Sharma is paid an hourly rate. One week he earned Rs.15750 by working 30 hours. If he works 35 hours next week, how much will he earn?
- How will be the world if currency(money) is removed from our lives?
- A and B each has some money. If A gives Rs. 30 to B, then B will have twice the money left with A. But, if B gives Rs. 10 to A, then A will have thrice as much as is left with B. How much money does each have?
- Adi had Rs. 50. He spends two fifths of it.(a) How much money does he spend?(b) How much money is left?
- Adi had Rs. 50. He spends two fifths of it.(a) How much money does he spend?(b) How much money was left?
