Skip to content

Instantly share code, notes, and snippets.

@PEZ
Last active April 25, 2024 18:28
Show Gist options
  • Save PEZ/03788b118a3d7923f7aae143e8ef1aee to your computer and use it in GitHub Desktop.
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
(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
Copy link

chenj7 commented Sep 8, 2021

(def __ (fn [coll nth]
          (keep-indexed #(when (pos? (mod (inc %) nth)) %2) coll)))

@szalai1
Copy link

szalai1 commented Sep 26, 2021

(def __ #(->> %1 ( partition-all (- %2 1) %2) flatten))

@StanTheBear
Copy link

(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))))

@natef-GuruTech
Copy link

(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