Created
March 21, 2013 02:53
-
-
Save tjhanley/5210351 to your computer and use it in GitHub Desktop.
I thought about the problem a bit more and given we can assume it is a time range if I knew where the maximum number was in the array i could get that index and work backward. Once I thought of the problem that way and with the key statement you gave me to watch out for those ranges that could go up and down. A sample result would look like this…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby -wKU | |
require "test/unit" | |
class Stock | |
# To change this template use File | Settings | File Templates. | |
def self.find_longest_run(prices=[]) | |
return {:low=>{0.0=>0},:high=>{0.0=>0}} if prices.nil? || prices.size == 0 | |
result = {} | |
# start at the end | |
#find the highest value | |
index_and_max_value = prices.each_with_index.max | |
result[:high] = Hash[*index_and_max_value.flatten] | |
#p result[:high] | |
#remove any values after the highest value | |
left_prices = prices[0..result[:high].values.first] | |
index_and_min_value = left_prices.each_with_index.min | |
result[:low] = Hash[*index_and_min_value.flatten] | |
result | |
end | |
end | |
class TestStock < Test::Unit::TestCase | |
def test__standard_range | |
puts "testing standard range" | |
prices = %w{2 1 2 3 2}.collect!{|i| i.to_f} | |
result = Stock.find_longest_run(prices) | |
puts "Result: #{result}" | |
assert result[:low].values.first.eql?(1) | |
assert result[:high].values.first.eql?(3) | |
end | |
def test__inner_range | |
puts "testing inner range" | |
prices = %w{10 5 10 15 20 12 30 2}.collect!{|i| i.to_f} | |
result = Stock.find_longest_run(prices) | |
puts "Result: #{result}" | |
assert result[:low].values.first.eql?(1) | |
assert result[:high].values.first.eql?(6) | |
end | |
def test__no_values | |
puts "testing no values" | |
result = Stock.find_longest_run(nil) | |
puts "Result: #{result}" | |
assert result[:low].values.first.eql?(0) | |
assert result[:high].values.first.eql?(0) | |
end | |
def test__msft | |
puts "testing msft" | |
prices = [17.47, 24.6, 23.77, 22.03, 27.29, 27.87, 29.41, 26.66, 22.98, 20.61, 23.42, 25.86, 26.69, 25.66, 23.5, 24.29, 21.05, 20.51, 22.03, 19.33, 19.77, 17.62, 21.54, 23.23, 20.83, 19.12, 19.15, 19.57, 20.67, 19.89, 20.72, 21.34, 21.43, 22.47, 21.25, 20.9, 22.24, 22.47, 21.56, 20.26, 21.24, 21.32, 23.21, 23.16, 22.25, 22.54, 22.8, 24.36, 24.28, 23.88, 22.93, 22.03, 23.06, 23.59, 22.71, 23.41, 25.11, 23.59, 23.56, 25.45, 24.05, 25.89, 24.79, 25.11, 22.28, 20.98, 21.58, 22.29, 23.89, 25.43, 26.69, 27.39, 27.86, 28.79, 26.37, 26.09, 28.03, 28.82, 27.68, 27.23, 27.08, 27.77, 34.69, 31.77, 33.66, 30.83, 25.82, 26.94, 27.07, 26.98, 26.21, 24.5, 26.1, 25.53, 21.36, 19.47, 18.72, 16.47, 15.66, 17.81, 19.64, 20.38, 23.19, 22.95, 24.19, 25.24, 27.21, 28.99, 30.04, 27.77, 28.39, 29.0, 30.24, 25.66, 22.89, 25.67, 23.47, 24.49, 26.67, 26.95].collect!{|i| i.to_f} | |
result = Stock.find_longest_run(prices) | |
puts "Result: #{result}" | |
assert result[:low].values.first.eql?(0) | |
assert result[:high].values.first.eql?(82) | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/Users/tom/.rbenv/versions/1.9.3-p194/bin/ruby -e $stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift) /Users/tom/Workspace/linked_list/stock.rb | |
Run options: | |
# Running tests: | |
testing inner range | |
Result: {:high=>{30.0=>6}, :low=>{5.0=>1}} | |
.testing msft | |
Result: {:high=>{34.69=>82}, :low=>{17.47=>0}} | |
.testing no values | |
Result: {:low=>{0.0=>0}, :high=>{0.0=>0}} | |
.testing standard range | |
Result: {:high=>{3.0=>3}, :low=>{1.0=>1}} | |
. | |
Finished tests in 0.001186s, 3372.6813 tests/s, 6745.3626 assertions/s. | |
4 tests, 8 assertions, 0 failures, 0 errors, 0 skips | |
Process finished with exit code 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment