Skip to content

Commit b6d5f85

Browse files
committed
add stock picker file
1 parent 393bd6e commit b6d5f85

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

stock-picker.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
def stockpicker(arr)
2+
profit = 0
3+
best_days = [nil,nil]
4+
5+
arr.each do |buy_price|
6+
arr.each do |sell_price|
7+
if arr.index(buy_price) < arr.index(sell_price) && profit < (sell_price - buy_price)
8+
profit, best_days[0], best_days[1] = (sell_price - buy_price), arr.index(buy_price), arr.index(sell_price)
9+
end
10+
end
11+
end
12+
13+
if best_days[0] != nil && best_days[1] != nil
14+
p best_days
15+
p "Profit: #{profit}"
16+
else
17+
p "No eligible time period exists"
18+
end
19+
end
20+
21+
stockpicker([17,3,6,9,15,8,6,1,10])

0 commit comments

Comments
 (0)