Skip to content

Instantly share code, notes, and snippets.

(fn [s e]
(let [squares (apply str (take-while #(<= % e) (iterate #(* % %) s)))
filler (drop (count squares) (repeat (#(* % %) (Math/ceil (Math/sqrt (count squares)))) \*))
elements (concat squares filler)
dimension (int (Math/sqrt (count elements)))
cardinal (- (* 2 dimension) 1)
canvas (vec (repeat cardinal ""))
start-pos (* 2 (quot (dec dimension) 2))
path (take (count elements) (mapcat #(repeat (+ 2 (* 2 %)) (if (odd? %) dec inc)) (range)))
balance (fn [c] (#(apply str `(~@% ~@(interpose \ c) ~@%)) (repeat (/ (- cardinal (- (* 2 (count c)) 1)) 2) \ )))]
;; ummels's solution to Squares Squared
;; https://4clojure.com/problem/138
(fn [start end]
(letfn
[(squares [start end]
(take-while #(<= % end) (iterate #(* % %) start)))
(arrange [x]
(let [n (count x)
dim (int (Math/ceil (Math/sqrt n)))
/** Implicit class for adding `|>` and `tap` operations to `AnyVal`.
*
* See `http://stackoverflow.com/a/11120847/839131`.
*/
implicit class PipeAndTap[A](val x: A) extends AnyVal {
/** Applies `f` to the underlying value and returns the result. */
def |>[B](f: A => B) = f(x)
/** Applies `f` to the underlying value `x` (presumably with side effects)
* and returns `x`.
object Util {
/** Merges a collection of streams into a single stream.
*
* Returns a sorted stream if all input streams are sorted.
*/
def mergeStreams[A](streams: Traversable[Stream[A]])(implicit ord: Ordering[A]): Stream[A] = {
val streams1 = streams.toList filterNot (_.isEmpty) sortBy (_.head)
if (streams1.isEmpty)
Stream.empty
else {