5 kyu

Pick peaks

10,124 of 29,849frenetic_be

Description:

In this kata, you will write a function that returns the positions and the values of the "peaks" (or local maxima) of a numeric array.

For example, the array arr = [0, 1, 2, 5, 1, 0] has a peak at position 3 with a value of 5 (since arr[3] equals 5).

The output will be returned as an object with two properties: pos and peaks. Both of these properties should be arrays. If there is no peak in the given array, then the output should be {pos: [], peaks: []}.

Example: pickPeaks([3, 2, 3, 6, 4, 1, 2, 3, 2, 1, 2, 3]) should return {pos: [3, 7], peaks: [6, 3]} (or equivalent in other languages)

All input arrays will be valid integer arrays (although it could still be empty), so you won't need to validate the input.

The first and last elements of the array will not be considered as peaks (in the context of a mathematical function, we don't know what is after and before and therefore, we don't know if it is a peak or not).

Also, beware of plateaus !!! [1, 2, 2, 2, 1] has a peak while [1, 2, 2, 2, 3] and [1, 2, 2, 2, 2] do not. In case of a plateau-peak, please only return the position and value of the beginning of the plateau. For example: pickPeaks([1, 2, 2, 2, 1]) returns {pos: [1], peaks: [2]} (or equivalent in other languages)

Have fun!

Arrays
Algorithms

Stats:

CreatedNov 6, 2013
PublishedNov 6, 2013
Warriors Trained94653
Total Skips21745
Total Code Submissions246544
Total Times Completed29849
JavaScript Completions10124
Python Completions11338
Ruby Completions667
Go Completions1099
PHP Completions666
C++ Completions2183
Java Completions2859
Haskell Completions218
C# Completions1062
F# Completions42
COBOL Completions8
Total Stars1952
% of votes with a positive feedback rating89% of 2966
Total "Very Satisfied" Votes2399
Total "Somewhat Satisfied" Votes486
Total "Not Satisfied" Votes81
Ad
Contributors
  • frenetic_be Avatar
  • jhoffner Avatar
  • Unnamed Avatar
  • GiacomoSorbi Avatar
  • donaldsebleung Avatar
  • JohanWiltink Avatar
  • JorgeVS Avatar
  • Blind4Basics Avatar
  • pmatseykanets Avatar
  • Voile Avatar
  • neilm Avatar
  • scorphus Avatar
  • KataSideKick Avatar
  • FArekkusu Avatar
  • user9644768 Avatar
  • hobovsky Avatar
  • rge123 Avatar
  • akar-0 Avatar
  • dfhwze Avatar
  • Just4FunCoder Avatar
  • saudiGuy Avatar
Ad