How to resolve the algorithm Zig-zag matrix step by step in the Clojure programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Zig-zag matrix step by step in the Clojure programming language
Table of Contents
Problem Statement
Produce a zig-zag array.
A zig-zag array is a square arrangement of the first N2 natural numbers, where the
numbers increase sequentially as you zig-zag along the array's anti-diagonals.
For a graphical representation, see JPG zigzag (JPG uses such arrays to encode images).
For example, given 5, produce this array:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Zig-zag matrix step by step in the Clojure programming language
Source code in the clojure programming language
(defn partitions [sizes coll]
(lazy-seq
(when-let [n (first sizes)]
(when-let [s (seq coll)]
(cons (take n coll)
(partitions (next sizes) (drop n coll)))))))
(defn take-from [n colls]
(lazy-seq
(when-let [s (seq colls)]
(let [[first-n rest-n] (split-at n s)]
(cons (map first first-n)
(take-from n (concat (filter seq (map rest first-n)) rest-n)))))))
(defn zig-zag [n]
(->> (partitions (concat (range 1 (inc n)) (range (dec n) 0 -1)) (range (* n n)))
(map #(%1 %2) (cycle [reverse identity]) ,)
(take-from n ,)))
user> (zig-zag 5)
(( 0 1 5 6 14)
( 2 4 7 13 15)
( 3 8 12 16 21)
( 9 11 17 20 22)
(10 18 19 23 24))
user> (zig-zag 6)
(( 0 1 5 6 14 15)
( 2 4 7 13 16 25)
( 3 8 12 17 24 26)
( 9 11 18 23 27 32)
(10 19 22 28 31 33)
(20 21 29 30 34 35))
You may also check:How to resolve the algorithm Pangram checker step by step in the PARI/GP programming language
You may also check:How to resolve the algorithm Tokenize a string step by step in the Amazing Hopper programming language
You may also check:How to resolve the algorithm Accumulator factory step by step in the NewLisp programming language
You may also check:How to resolve the algorithm Abbreviations, easy step by step in the Tcl programming language
You may also check:How to resolve the algorithm ABC problem step by step in the Ruby programming language