User talk:Gaaijz: Difference between revisions

→‎Zigzag: improved version
(→‎Zigzag: improved version)
Line 12:
== Zigzag ==
Two different approaches with Haskell for the zigzag task.
zigzag m revcsn = take m .transp_wof $ zipWith id (cycle [id,reverse,id]) antiDiags where
flist = map (:[])
dlmmn = min m n
elist = cycle [[]]
antiDiags = unfoldr (\((c:cs),xs) -> if null xs then Nothing else Just (take c xs,(cs,drop c xs)))
([1..dlmmn-1]++(replicate nd(succ.abs $ m-n) dlmmn)++[dlmmn-1,dlmmn-2..0], [0..m*n-1])
transp_wof = unfoldr (\xs -> if null xs then Nothing else Just $ next xs)
next xs = (,) (concatMap (take 1) ks) ((map (drop 1) $ drop 1 ks) ++ ts)
where (ks,ts) = splitAt n xs
zigzag m n = transpose. map concat. transpose
. uncurry ((.(map (liftM2 (++) ((`take`elist).(`subtract`n).length) flist))).(++)
.(map (liftM2 (++) flist ((`take`elist).(`subtract`n).length))))
$ splitAt k revcs
where
k = truncate . sqrt . fromIntegral $ (m*n)
dl = min m n
nd = abs (m-n)
antiDiags = unfoldr (\((c:cs),xs) -> if null xs then Nothing else Just (take c xs,(cs,drop c xs)))
([1..dl]++(replicate nd dl)++[dl-1,dl-2..0], [0..m*n-1])
revcs = zipWith id (cycle [id,reverse]) antiDiags
:Slower version, almost complete emulation of the J-solution
groupon f x y= f x == f y
Anonymous user