Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Spiralled Arrays

Name: Anonymous 2007-07-28 18:53 ID:RZVt/+fJ

turn [1,2,3,4,5,6,7,8,9] into [9,2,3,8,1,4,7,6,5]. Make it work for an arbitrary array of size n^2. equally acceptable is [[1,2,3],[4,5,6],[7,8,9]] into [[9,2,3],[8,1,4],[7,6,5]].

how's it done?

pseudocode'll do

Name: Anonymous 2007-07-29 7:13 ID:mg5+tEVB

import List (transpose)

rotate = reverse . transpose

spiral (x:xs) = concat $ spiral' xs [[x]]
    where spiral' [] s = s
          spiral' ys s = let n = length $ head s
                         in spiral' (drop n ys) (rotate $ take n ys : s)

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List