1
0
Fork 0
mirror of https://gitlab.com/mfocko/CodeWars.git synced 2024-11-21 16:13:47 +01:00

5kyu: add pick peaks

Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
Matej Focko 2021-12-31 17:12:42 +01:00
parent 29b9da0f5e
commit 1f4adef5e6
No known key found for this signature in database
GPG key ID: 332171FADF1DB90B
2 changed files with 31 additions and 0 deletions

View 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..]

View file

@ -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