Best travel
Description:
John and Mary want to travel between a few towns A, B, C ... Mary has on a sheet of paper a list of distances between these towns.
ls = [50, 55, 57, 58, 60]
.
John is tired of driving and he says to Mary that he doesn't want to drive more than t = 174 miles
and he
will visit only 3
towns.
Which distances, hence which towns, they will choose so that the sum of the distances is the biggest possible to please Mary and John?
Example:
With list ls
and 3 towns to visit they can make a choice between:
[50,55,57],[50,55,58],[50,55,60],[50,57,58],[50,57,60],[50,58,60],[55,57,58],[55,57,60],[55,58,60],[57,58,60]
.
The sums of distances are then:
162, 163, 165, 165, 167, 168, 170, 172, 173, 175
.
The biggest possible sum taking a limit of 174
into account is then 173
and the distances of the 3
corresponding towns is [55, 58, 60]
.
The function chooseBestSum
(or choose_best_sum
or ... depending on the language) will take as parameters t
(maximum sum of distances, integer >= 0), k
(number of towns to visit, k >= 1)
and ls
(list of distances, all distances are positive or zero integers and this list has at least one element).
The function returns the "best" sum ie the biggest possible sum of k
distances less than or equal to the given limit t
, if that sum exists,
or otherwise nil, null, None, Nothing, depending on the language. In that case with C, C++, D, Dart, Fortran, F#, Go, Julia, Kotlin, Nim, OCaml, Pascal, Perl, PowerShell, Reason, Rust, Scala, Shell, Swift return -1
.
Examples:
ts = [50, 55, 56, 57, 58]
choose_best_sum(163, 3, ts) -> 163
xs = [50]
choose_best_sum(163, 3, xs) -> nil (or null or ... or -1 (C++, C, D, Rust, Swift, Go, ...)
ys = [91, 74, 73, 85, 73, 81, 87]
choose_best_sum(230, 3, ys) -> 228
Notes:
- try not to modify the input list of distances
ls
- in some languages this "list" is in fact a string (see the Sample Tests).
Similar Kata:
Stats:
Created | Sep 2, 2015 |
Published | Sep 2, 2015 |
Warriors Trained | 97022 |
Total Skips | 28326 |
Total Code Submissions | 185043 |
Total Times Completed | 21065 |
Ruby Completions | 1185 |
Python Completions | 9989 |
Haskell Completions | 255 |
Java Completions | 1273 |
C# Completions | 797 |
Clojure Completions | 58 |
JavaScript Completions | 4224 |
CoffeeScript Completions | 15 |
C++ Completions | 888 |
PHP Completions | 317 |
C Completions | 416 |
Rust Completions | 348 |
Swift Completions | 189 |
Crystal Completions | 17 |
TypeScript Completions | 353 |
Go Completions | 430 |
F# Completions | 44 |
R Completions | 97 |
Shell Completions | 6 |
OCaml Completions | 15 |
Kotlin Completions | 182 |
Elixir Completions | 72 |
Scala Completions | 187 |
Julia Completions | 28 |
PowerShell Completions | 12 |
Nim Completions | 9 |
Reason Completions | 2 |
Racket Completions | 11 |
Fortran Completions | 6 |
Dart Completions | 113 |
Pascal Completions | 4 |
Perl Completions | 7 |
Elm Completions | 4 |
D Completions | 5 |
Erlang Completions | 6 |
Prolog Completions | 9 |
Total Stars | 2917 |
% of votes with a positive feedback rating | 90% of 2311 |
Total "Very Satisfied" Votes | 1903 |
Total "Somewhat Satisfied" Votes | 338 |
Total "Not Satisfied" Votes | 70 |