forked from devops-by-examples/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortandConditionalSum
More file actions
45 lines (35 loc) · 1.21 KB
/
Copy pathSortandConditionalSum
File metadata and controls
45 lines (35 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#####################################################
#Author: Abhishek Veeramalla
"""
John wants to buy a few things from a groceries store, To his surprise he finds a grocery store which is running a strange give away. The store has a section
of items to which owner is ready to pay to the customers. Unfortunately, John can accomodate only `M` number of items in his truck. So, John decides to Pick few items that he
needs and a few from the give away section.
Create a function that would tell you the profit John made.
Sample Inputs:
Set 1:
------
1
2
[10, 20, 30, -40, -50]
Set 2:
------
2
3
[-10, -20 -30, 10, 15]
First line of the Data Set defines `M` - Number of items that John can accomadate in his truck
Second line of the Data Set defines the number of items which are on give away.
Third lines of the Data Set defines the prices of all the items that are available in the store.
"""
#####################################################
def getProfit(arr, M):
arr.sort()
profit = 0
for i in range(M):
if arr[i] < 0:
profit = profit + arr[i]
print("Profit made by John is: " + str(abs(profit)))
def main():
items = [-1, -2, 3, 4, 5]
M = 3
getProfit(items, M)
main()