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

Pages: 1-

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-28 19:02 ID:WmnyXust

>>1
Say what? What kind of transformation do you want? [9,2,3,8,1,4,7,6,5] doesn't look like a spiral to me.

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

print [9,2,3,8,1,4,7,6,5] in rows of 3

9 2 3
8 1 4
7 6 5

Name: Anonymous 2007-07-28 19:09 ID:RZVt/+fJ

i should add that the elements of the array aren't necessarily ordered. it has to be able to go from ["the","spiral","was","made","from","an","array","yes","indeed"] to ["indeed","spiral","was","yes","the","made","array","an","from"]

Name: Anonymous 2007-07-28 19:13 ID:Heaven

from homework import spiral_array

Name: Anonymous 2007-07-28 19:17 ID:Heaven

>>5 win

Name: Anonymous 2007-07-28 19:29 ID:1rzJ2v77

>>1
I managed to creat a haskell solution in 4 lines of code using foldr

Post your solution, and I will post mine

Name: Anonymous 2007-07-28 19:32 ID:RZVt/+fJ

i don't have a solution. and contrary to the cynicism of >>5, this isn't homework, because a) it's summer, b) i've finished school, c) i've not taken a computer science subject at all

Name: Anonymous 2007-07-28 20:19 ID:RZVt/+fJ

great, so none of you can just give me pseudocode on how to approach this? with an explanation? good work internet!

Name: Anonymous 2007-07-28 20:21 ID:PCfc4wLn

trolled

Name: Anonymous 2007-07-28 20:22 ID:RZVt/+fJ

>>10

wtf?

Name: Anonymous 2007-07-28 20:50 ID:xqE54L4c

>>9
Bitch, bitch, bitch. Complain, complain, complain.

Name: Anonymous 2007-07-28 21:20 ID:Heaven

>>9
I'm not here to help you or anyone else, bitch. If sometimes I do so it's just because I found the problem in question interesting.

Name: Anonymous 2007-07-28 22:34 ID:H7BzSN2q

What if n is an even number?

Name: Anonymous 2007-07-28 23:57 ID:13DFZExV

this VERY UGLY code may be a hint on how to do it. you'll have to rewrite it though, it's written in haskell in a sucky imperative style, which does not really suit haskell. anyway, I'm not even sure it works. basically, the idea is, you start with an empty matrix (list of lists). you keep a sort of a pointer, which stats the position and the direction you're going in the spiral. the function travels gives you the next pointer.
so, basically, you start with the first pointer, insert the item, then get the next pointer, insert the item, etc. if you want to do it in a true functional style (which would be cleaner than this piece of shit I threw together right now), collect the pointers, then sort them by the position, and pick up (i.e. map a selecting function) the items).

travel _ ("up",0,j) = ("right", 0, j + 1)
travel _ ("up",i,j) = ("up", i - 1, j)
travel n ("right", i, j) | n == j    = ("down", i - 1, j)
                         | otherwise = ("right", i, j + 1)
-- fill up the rest yourself

group [] _ = []
group l n = take 3 l : group (drop n l)

makeList _ 0 = []
makeList i n = i:makeList i (n - 1)

start i n = makeList (makeList i n) n

inserte e 0 x:xs = e:xs
inserte e p x:xs = x:inserte e (p - 1) xs

insert 0 j e x:xs = inserte e j x : xs
insert i j e x:xs = x : insert (i - 1) j e xs

spiral x:xs = concat (loop n (start x n) x:xs ("up", n div 2 + 1, n div 2 + 1))
                where n = sqrt (length x:xs)

loop _ matrix [] _ = matrix
loop n matrix x:xs (direction, i, j) = loop n (insert i j x matrix) xs (travel n (direction, i, j))

Name: Anonymous 2007-07-29 0:49 ID:KrgQgXN/

Pseudocode:

1. Create array.
2. ?????
3. [1,2,3,4,5,6,7,8,9] turns into [9,2,3,8,1,4,7,6,5]
4. return mudkips

Name: Anonymous 2007-07-29 1:10 ID:V2uv0Bo1

>>1
1. flattern your first array (ie [[1,2,3],[4,5,6],[7,8,9]] turns into [1,2,3,4,5,6,7,8,9])
2. shuffle elements manually

Name: Anonymous 2007-07-29 2:03 ID:Heaven

have you tried asking it politely

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)

Name: Anonymous 2007-07-29 8:55 ID:/czB3sXJ

lisp hackers would use arc,

(= 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))))


 -- Paul graham

Name: Anonymous 2007-07-29 11:37 ID:LLFD1TmY

>>14

then n is divisible by 2

>>18

what part of the first few posts weren't polite, up until >>5 decided to be a cock?

Name: Anonymous 2007-07-29 14:12 ID:vx13nuDH

Name: Anonymous 2007-07-29 18:27 ID:LLFD1TmY

Name: Anonymous 2007-07-29 18:35 ID:Ve+HKYHX

Right, but >>9 appeared to be the op. That's also who the post was likely directed at. Anyway, what's your fixation on >>5 and the posts before >>5 ?

Name: Anonymous 2007-07-29 18:51 ID:Heaven

Thread over.

Name: Anonymous 2007-07-29 22:09 ID:dwx6vNep

>>19
now that's a cool solution ;_;
(i'm >>15 btw)

Name: Anonymous 2009-01-14 12:44

LISP

Name: Anonymous 2010-11-15 14:56

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