-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.rb
61 lines (49 loc) · 1.5 KB
/
script.rb
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
prices = [17,3,6,9,15,8,6,1,10]
def stock_picker(prices)
new_low_found = false
new_low = nil
new_high_found = false
new_high = nil
previous_price = prices[0] + 1
i = 0
k = 0
comparing_array = []
comparing_array << Array.new(2)
prices.each do |current_price|
if current_price < previous_price && new_low_found == false
comparing_array[i][k] = current_price
elsif current_price > previous_price && new_low_found == false
new_low_found = true
k += 1
comparing_array[i][k] = current_price
elsif current_price > previous_price && new_low_found == true
comparing_array[i][k] = current_price
elsif current_price < previous_price && new_high_found == false
new_high_found = true
new_low_found = false
i += 1
k -= 1
comparing_array << Array.new(2)
comparing_array[i][k] = current_price
new_high_found = false
end
previous_price = current_price
end
previous_difference = nil
best_prices = nil
comparing_array.each do |nested_array|
current_difference = nested_array[1] - nested_array[0]
if previous_difference == nil
previous_difference = current_difference
best_prices = nested_array
elsif current_difference > previous_difference
previous_difference = current_difference
best_prices = nested_array
end
end
best_days = Array.new(2)
best_days[0] = prices.index(best_prices[0])
best_days[1] = prices.index(best_prices[1])
p best_days
end
stock_picker(prices)