Skip to content

Instantly share code, notes, and snippets.

@danielpowell4
Created September 11, 2016 18:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielpowell4/66d66bfa6b88ca9f54f983f4f978f87b to your computer and use it in GitHub Desktop.
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.
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