Skip to content

Instantly share code, notes, and snippets.

@l1x
Created August 29, 2012 04:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save l1x/3506753 to your computer and use it in GitHub Desktop.
Save l1x/3506753 to your computer and use it in GitHub Desktop.
(ns spiral)
(def matrix1 [[1 2 3][8 9 4][7 6 5]])
(def matrix2 [[1 2 3 4][12 13 14 5][11 16 15 6][10 9 8 7]])
(defn spiral-print [matrix]
(let [ [row & rows] (seq matrix)]
(doseq [el row] (print (str " " el)))
(when (seq rows)
(->> rows (apply map vector) reverse recur))))
(spiral-print matrix1)
(spiral-print matrix2)
@l1x
Copy link
Author

l1x commented Aug 29, 2012

[[1 2 3] [[4 5] [[6 7] [[8]
[8 9 4] -> [9 6] -> [9 8]] -> [9]] -> [[9]]
[7 6 5]] [8 7]]

@gfredericks
Copy link

(defn spiral
  [matrix]
  (let [[row & rows] (seq matrix)]
    (doseq [el row] (print el))
    (when (seq rows)
      (->> rows (apply map vector) reverse recur))))

@gfredericks
Copy link

;; Evil version
(->> rows
     (apply map vector)
     reverse
     recur
     (when (seq rows))
     (let [[row & rows] (seq matrix)] (doseq [el row] (print el)))
     (defn spiral [matrix]))

@l1x
Copy link
Author

l1x commented Aug 29, 2012

This is really tricky :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment