Last active
April 30, 2024 23:19
-
-
Save PEZ/2a419f3d9e0c22be01179175f6a7d2f0 to your computer and use it in GitHub Desktop.
Rotate Sequence – Rich 4Clojure Problem 44 – 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.medium.problem-044 | |
(:require [hyperfiddle.rcf :refer [tests]])) | |
;; = Rotate Sequence = | |
;; By 4Clojure user: dbyrne | |
;; Difficulty: Medium | |
;; Tags: [seqs] | |
;; | |
;; Write a function which can rotate a sequence in either | |
;; direction. | |
(def __ :tests-will-fail) | |
(comment | |
) | |
(tests | |
(__ 2 [1 2 3 4 5]) := '(3 4 5 1 2) | |
(__ -2 [1 2 3 4 5]) := '(4 5 1 2 3) | |
(__ 6 [1 2 3 4 5]) := '(2 3 4 5 1) | |
(__ 1 '(:a :b :c)) := '(:b :c :a) | |
(__ -4 '(:a :b :c)) := '(:c :a :b)) | |
;; To participate, fork: | |
;; https://github.com/PEZ/rich4clojure | |
;; Post your solution below, please! |
avillega
commented
Jul 22, 2021
(defn __ [n coll]
(let [length (count coll)]
(concat (drop (mod n length) coll)
(take (mod n length) coll))))
(fn [split-count in-seq]
"rotate a seq in either direction"
(apply concat (reverse
(split-at (mod split-count (count in-seq)) in-seq))))
(fn [n coll] (let [coll-count (count coll)]
(take coll-count (drop (mod n coll-count) (cycle coll)))))
(def __ (fn [n xs]
(let [n' (mod n (count xs))
movers (take n' xs)]
(concat (drop n' xs) movers))))
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment