【发布时间】:2014-12-21 20:14:34
【问题描述】:
在我程序中的一个数组上调用 delete_at 后,它同时更改了另一个数组。这两个数组具有相同的值,但应该作为两个独立的东西来处理(改变一个不应该改变另一个)。
请注意,我通过使用 arr = arr[0..-2] 而不是 arr.delete_at(-1) 修复了解决方案。我只想知道为什么 delete_at 改变了两个数组。
另外请注意,我知道这可能不是解决手头难题的最佳方法,但这不是我们在这里讨论的内容;)
def stock_picker(prices)
#takes an array of stock prices by day and returns the best day to buy and sell
#this solution starts at the last day and finds the greatest difference, then drops the last day and does it again
#until there is just one day left.
def pick(all_prices, current_prices, current_pick)
if current_prices == 1
return current_pick
else
new_pick = tryer(all_prices, current_prices, [0, current_prices.size-1])
puts "all_prices"
puts all_prices
if (all_prices[new_pick.last] - all_prices[new_pick.first]) > (all_prices[current_pick.last] - all_prices[current_pick.first])
current_pick = new_pick
end
current_prices.delete_at(-1)
#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!#
puts "After current_prices.delete_at(-1)"
puts "all_prices"
puts all_prices
puts "current_prices"
puts current_prices
puts "wtf"
return pick(all_prices, current_prices, current_pick)
end
end
def tryer(all_prices, tryer_prices, tryer_pick)
#subtracts the last day from each day.
if tryer_prices.size == 1
return tryer_pick
else
#the new pick reflects the first and last of the tryer_prices.
new_pick = [tryer_pick.first + 1, tryer_pick.last]
if (tryer_prices.last - tryer_prices.first) > (all_prices[tryer_pick.last] - all_prices[tryer_pick.first])
return tryer(all_prices, tryer_prices.drop(1), new_pick)
else
return tryer(all_prices, tryer_prices.drop(1), tryer_pick)
end
end
end
all_prices = prices
current_prices = prices
pick(all_prices, current_prices, [0, prices.size-1])
end
result = stock_picker([17,3,6,9,15,8,6,1,10])
puts result
【问题讨论】: