mirror of
https://gitlab.com/mfocko/CodeWars.git
synced 2024-11-22 00:23:47 +01:00
5kyu: add pick peaks
Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
parent
29b9da0f5e
commit
1f4adef5e6
2 changed files with 31 additions and 0 deletions
30
5kyu/pick_peaks/solution.hs
Normal file
30
5kyu/pick_peaks/solution.hs
Normal file
|
@ -0,0 +1,30 @@
|
|||
module PickPeak.JorgeVS.Kata where
|
||||
|
||||
data PickedPeaks = PickedPeaks { pos :: [Int], peaks :: [Int] } deriving (Eq, Show)
|
||||
|
||||
windowed :: Int -> [a] -> [[a]]
|
||||
windowed n xs
|
||||
| null xs || length window < n = []
|
||||
| otherwise = window : (windowed n $ tail xs)
|
||||
where window = take n xs
|
||||
|
||||
addIfPeak :: PickedPeaks -> [(Int, Int)] -> PickedPeaks
|
||||
addIfPeak ps [l, mid, r]
|
||||
| (snd mid > snd l) && (snd mid > snd r) = PickedPeaks { pos = fst mid : pos ps, peaks = snd mid : peaks ps }
|
||||
| otherwise = ps
|
||||
addIfPeak _ _ = error "Invalid state"
|
||||
|
||||
removeConsecutiveDuplicates :: [(Int, Int)] -> [(Int, Int)]
|
||||
removeConsecutiveDuplicates = foldr addIfNotDuplicate []
|
||||
where addIfNotDuplicate :: (Int, Int) -> [(Int, Int)] -> [(Int, Int)]
|
||||
addIfNotDuplicate pair [] = [pair]
|
||||
addIfNotDuplicate pair@(i, x) pairs
|
||||
| x == (snd . head) pairs = pair : tail pairs
|
||||
| otherwise = pair : pairs
|
||||
|
||||
pickPeaks :: [Int] -> PickedPeaks
|
||||
pickPeaks = foldl addIfPeak PickedPeaks { pos = [], peaks = [] }
|
||||
. windowed 3
|
||||
. reverse
|
||||
. removeConsecutiveDuplicates
|
||||
. zip [0..]
|
|
@ -97,6 +97,7 @@
|
|||
### Haskell
|
||||
|
||||
- [Directions Reduction](https://www.codewars.com/kata/550f22f4d758534c1100025a) - [solution](5kyu/directions_reduction)
|
||||
- [Pick peaks](https://www.codewars.com/kata/5279f6fe5ab7f447890006a7) - [solution](5kyu/pick_peaks)
|
||||
|
||||
### JS
|
||||
|
||||
|
|
Loading…
Reference in a new issue