Last active
April 25, 2024 18:28
-
-
Save PEZ/03788b118a3d7923f7aae143e8ef1aee to your computer and use it in GitHub Desktop.
Drop Every Nth Item – Rich 4Clojure Problem 41 – See: https://github.com/PEZ/rich4clojure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns rich4clojure.easy.problem-041 | |
(:require [hyperfiddle.rcf :refer [tests]])) | |
;; = Drop Every Nth Item = | |
;; By 4Clojure user: dbyrne | |
;; Difficulty: Easy | |
;; Tags: [seqs] | |
;; | |
;; Write a function which drops every Nth item from a | |
;; sequence. | |
(def __ :tests-will-fail) | |
(comment | |
) | |
(tests | |
(__ [1 2 3 4 5 6 7 8] 3) := [1 2 4 5 7 8] | |
(__ [:a :b :c :d :e :f] 2) := [:a :c :e] | |
(__ [1 2 3 4 5 6] 4) := [1 2 3 5 6]) | |
;; To participate, fork: | |
;; https://github.com/PEZ/rich4clojure | |
;; Post your solution below, please! |
chenj7
commented
Sep 8, 2021
(def __ #(->> %1 ( partition-all (- %2 1) %2) flatten))
(fn [seq1 nth-val]
(apply concat
(partition
(dec nth-val) ;parts size nth-val -1
nth-val ;size n step so each n is lost
(list) ; pad empty list to get leftovers of seq
seq1))))
(defn drop-at [coll idcs]
(->> coll
(map-indexed (fn [i n] [i n]))
(remove (fn [[i x]] (some #{i} idcs)))
(map second)))
(def __ (fn [xs n]
(let [idcs (map dec (range 0 (inc (count xs)) n))]
(drop-at xs (rest idcs)))))
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment