7 kyu

How Green Is My Valley?

1,526 of 5,575g964

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 counterpart r in the right wing, the difference l - 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

More By Author:

Check out these other kata created by g964

Stats:

CreatedMar 12, 2016
PublishedMar 12, 2016
Warriors Trained17730
Total Skips1623
Total Code Submissions22038
Total Times Completed5575
Ruby Completions174
Python Completions1526
JavaScript Completions1490
CoffeeScript Completions15
Java Completions550
C# Completions271
Haskell Completions114
Clojure Completions48
Elixir Completions68
C++ Completions322
PHP Completions188
TypeScript Completions128
Crystal Completions11
F# Completions22
C Completions133
Shell Completions66
OCaml Completions19
Kotlin Completions220
Julia Completions14
R Completions38
Go Completions228
Nim Completions10
Rust Completions143
Racket Completions14
Reason Completions4
VB Completions44
Scala Completions38
Pascal Completions13
Perl Completions22
COBOL Completions8
D Completions9
Erlang Completions8
Total Stars258
% of votes with a positive feedback rating91% of 773
Total "Very Satisfied" Votes655
Total "Somewhat Satisfied" Votes99
Total "Not Satisfied" Votes19
Ad
Contributors
  • g964 Avatar
  • jhoffner Avatar
  • raulbc777 Avatar
  • Chrono79 Avatar
  • imjasonmiller Avatar
  • kazk Avatar
  • Blind4Basics Avatar
  • Voile Avatar
  • monadius Avatar
  • hobovsky Avatar
  • StephanieCunnane Avatar
  • trashy_incel Avatar
  • user8436785 Avatar
  • akar-0 Avatar
  • Just4FunCoder Avatar
  • KayleighWasTaken Avatar
  • saudiGuy Avatar
Ad