Anonymous user
User talk:Gaaijz: Difference between revisions
→Zigzag streamlining
(→Jers) |
|||
Line 13:
Two different approaches with Haskell for the zigzag task.
flist = map (:[])
elist =
zigzag m n =
. uncurry ((.(map (liftM2 (++) ((`take`elist).(
.(map (liftM2 (++) flist ((`take`elist).(`subtract`n).length))))
$ splitAt k revcs
where
Line 26 ⟶ 23:
dl = min m n
nd = abs (m-n)
([1..dl]++(replicate nd dl)++[dl-1,dl-2..0], [0..m*n-1])▼
▲ pseudoAntiDiags = 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 (revNrev rev) pseudoAntiDiags
▲ -- slower, almost complete emulation of the J-solution
groupon f x y= f x == f y
tab n = fst . until (null.snd) (\(xs,ys)-> (xs++[take n ys], drop n ys)) . (,) []
Line 39 ⟶ 34:
where fdiag = map (map snd). groupBy (groupon fst).sortBy (comparing fst)
$ zip (map sum $ sequence [[0..m-1],[0..n-1]] ) [0..]
*Main> sum.map sum $ zigzag 500 500
1185103928
|