Created
September 11, 2016 18:48
-
-
Save danielpowell4/66d66bfa6b88ca9f54f983f4f978f87b to your computer and use it in GitHub Desktop.
Outputs max distance you can travel where t is destination count, k is distance, and ls is a list of length.
This file contains 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
def choose_best_trip(t, k, ls) | |
ls.combination(k).to_a.map{|a| a.reduce(:+)}.select{ |v| v <= t }.max | |
end | |
# example: ts = [50, 55, 56, 57, 58] choose_best_trip(163, 3, ts) -> 163 | |
# Tests | |
Test.describe("choose_best_sum") do | |
Test.it("Basic Tests") do | |
ts = [50, 55, 56, 57, 58] | |
Test.assert_equals(choose_best_trip(163, 3, ts), 163) | |
ts = [50] | |
Test.assert_equals(choose_best_trip(163, 3, ts), nil) | |
ts = [91, 74, 73, 85, 73, 81, 87] | |
Test.assert_equals(choose_best_trip(230, 3, ts), 228) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment