7 kyu
How Green Is My Valley?
1,528 of 5,579g964
Description:
Input : an array of integers.
Output : this array, but sorted in such a way that there are two wings:
the left wing with numbers decreasing,
the right wing with numbers increasing.
the two wings have the same length. If the length of the array is odd the wings are around the bottom, if the length is even the bottom is considered to be part of the right wing.
each integer
l
of the left wing must be greater or equal to its counterpartr
in the right wing, the differencel - r
being as small as possible. In other words the right wing must be nearly as steep as the left wing.
The function is make_valley or makeValley or make-valley
.
a = [79, 35, 54, 19, 35, 25]
make_valley(a) --> [79, 35, 25, *19*, 35, 54]
The bottom is 19, left wing is [79, 35, 25], right wing is [*19*, 35, 54].
79..................54
35..........35
25.
..19
a = [67, 93, 100, -16, 65, 97, 92]
make_valley(a) --> [100, 93, 67, *-16*, 65, 92, 97]
The bottom is -16, left wing [100, 93, 67] and right wing [65, 92, 97] have same length.
100.........................97
93..........
.........92
67......
.....65
-16
a = [66, 55, 100, 68, 46, -82, 12, 72, 12, 38]
make_valley(a) --> [100, 68, 55, 38, 12, *-82*, 12, 46, 66, 72]
The bottom is -82, left wing is [100, 68, 55, 38, 12]], right wing is [*-82*, 12, 46, 66, 72].
a = [14,14,14,14,7,14]
make_valley(a) => [14, 14, 14, *7*, 14, 14]
a = [14,14,14,14,14]
make_valley(a) => [14, 14, *14*, 14, 14]
A counter-example:
a = [17, 17, 15, 14, 8, 7, 7, 5, 4, 4, 1]
A solution could be [17, 17, 15, 14, 8, 1, 4, 4, 5, 7, 7]
but the right wing [4, 4, 5, 7, 7] is much flatter than the left one
[17, 17, 15, 14, 8].
Summing the differences between left and right wing:
(17-7)+(17-7)+(15-5)+(14-4)+(8-4) = 44
Consider the following solution:
[17, 15, 8, 7, 4, 1, 4, 5, 7, 14, 17]
Summing the differences between left and right wing:
(17-17)+(15-14)+(8-7)+(7-5)+(4-4) = 4
The right wing is nearly as steep as the right one.
Fundamentals
Similar Kata:
Stats:
Created | Mar 12, 2016 |
Published | Mar 12, 2016 |
Warriors Trained | 17729 |
Total Skips | 1623 |
Total Code Submissions | 22043 |
Total Times Completed | 5579 |
Ruby Completions | 174 |
Python Completions | 1528 |
JavaScript Completions | 1490 |
CoffeeScript Completions | 15 |
Java Completions | 550 |
C# Completions | 271 |
Haskell Completions | 114 |
Clojure Completions | 48 |
Elixir Completions | 68 |
C++ Completions | 323 |
PHP Completions | 188 |
TypeScript Completions | 128 |
Crystal Completions | 11 |
F# Completions | 22 |
C Completions | 133 |
Shell Completions | 66 |
OCaml Completions | 19 |
Kotlin Completions | 220 |
Julia Completions | 14 |
R Completions | 38 |
Go Completions | 229 |
Nim Completions | 10 |
Rust Completions | 143 |
Racket Completions | 14 |
Reason Completions | 4 |
VB Completions | 44 |
Scala Completions | 38 |
Pascal Completions | 13 |
Perl Completions | 22 |
COBOL Completions | 8 |
D Completions | 9 |
Erlang Completions | 8 |
Total Stars | 257 |
% of votes with a positive feedback rating | 91% of 774 |
Total "Very Satisfied" Votes | 656 |
Total "Somewhat Satisfied" Votes | 99 |
Total "Not Satisfied" Votes | 19 |