forked from DSC-COEA-Ambajogai/Hacktoberfest2020
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStock buy and sell.py
95 lines (69 loc) · 1.96 KB
/
Stock buy and sell.py
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
""" The cost of stock on each day is given in an array A[] of size N. Find all the days on which you buy and
sell the stock so that in between those days your profit is maximum.
Input:
First line contains number of test cases T.
First line of each test case contains an integer value N denoting the number of days,
followed by an array of stock prices of N days.
Output:
For each testcase, output all the days with profit in a single line. And if there is no profit then print "No Profit".
Constraints:
1 <= T <= 100
2 <= N <= 103
0 <= Ai <= 104
Example
Input:
3
7
100 180 260 310 40 535 695
4
100 50 30 20
10
23 13 25 29 33 19 34 45 65 67
Output:
(0 3) (4 6)
No Profit
(1 4) (5 9)
Explanation:
Testcase 1: We can buy stock on day 0, and sell it on 3rd day, which will give us maximum profit.
Note: Output format is as follows - (buy_day sell_day) (buy_day sell_day)
For each input, output should be in a single line. """
N = int(input())
lst = [int(item) for item in input().split()]
buy = 0
sell = 0
s = 1
for i in range(0,N-1):
if lst[i]<lst[i+1] and buy==0:
buy = lst[i]
print("Buy at", buy)
if lst[i]>lst[i+1] and buy>0:
sell = lst[i]
buy = lst[i+1]
print("Sell at", sell)
print("Buy at", buy)
if buy>0 and i == N-2:
sell = lst[N-1]
print("Sell at",sell)
if i == N-2 and buy+sell==0:
print("No Profit")
""" if i == N-2 and buy<lst[N-2]:
sell = lst[N-1]
print("Sell at",sell)
if lst[i]<lst[i+1] and s == 1:
buy = lst[i]
sell = lst[i+1]
print("Buy at",buy)
s = 0
if lst[i]>lst[i+1] and i!=N-1:
continue
if i==N-1 and lst[i]>lst[i+1]:
if buy+sell==0 and i==N-1:
print("No Profit")
break
else:
sell = lst[i]
s == 1
buy = lst[i+1]
print("Sell at",sell)
print('Buy at',buy)
"""