How to resolve the algorithm Zig-zag matrix step by step in the Joy programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Zig-zag matrix step by step in the Joy programming language

Table of Contents

Problem Statement

Produce a zig-zag array.

A   zig-zag   array is a square arrangement of the first   N2   natural numbers,   where the
numbers increase sequentially as you zig-zag along the array's   anti-diagonals. For a graphical representation, see   JPG zigzag   (JPG uses such arrays to encode images).

For example, given   5,   produce this array:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Zig-zag matrix step by step in the Joy programming language

Source code in the joy programming language

(*
    From the library.
*)
DEFINE reverse == [] swap shunt;
       shunt   == [swons] step.

(*
    Split according to the parameter given.
*)
DEFINE take-drop  == [dup] swap dup [[] cons [take swap] concat concat] dip []
                     cons concat [drop] concat.

(*
    Take the first of a list of lists.
*)
DEFINE take-first == [] cons 3 [dup] times [dup] swap concat [take [first] map
                     swap dup] concat swap concat [drop swap] concat swap
                     concat [take [rest] step []] concat swap concat [[cons]
                     times swap concat 1 drop] concat.

DEFINE zigzag ==

(*
    Use take-drop to generate a list of lists.
*)
4 [dup] times 1 swap from-to-list swap pred 1 swap from-to-list reverse concat
swap dup * pred 0 swap from-to-list swap [take-drop i] step [pop list] [cons]
while

(*
    The odd numbers must be modified with reverse.
*)
[dup size 2 div popd [1 =] [pop reverse] [pop] ifte] map

(*
    Take the first of the first of n lists.
*)
swap dup take-first [i] cons times pop

(*
    Merge the n separate lists.
*)
[] [pop list] [cons] while

(*
    And print them.
*)
swap dup * pred 'd 1 1 format size succ [] cons 'd swons [1 format putchars]
concat [step '\n putch] cons step.

11 zigzag.

  

You may also check:How to resolve the algorithm RPG attributes generator step by step in the Forth programming language
You may also check:How to resolve the algorithm Leap year step by step in the Q programming language
You may also check:How to resolve the algorithm Find the intersection of a line with a plane step by step in the Raku programming language
You may also check:How to resolve the algorithm Narcissistic decimal number step by step in the 11l programming language
You may also check:How to resolve the algorithm Fibonacci n-step number sequences step by step in the Action! programming language