How to resolve the algorithm Multi-dimensional array step by step in the jq programming language

Published on 12 May 2024 09:40 PM
#Jq

How to resolve the algorithm Multi-dimensional array step by step in the jq programming language

Table of Contents

Problem Statement

For the purposes of this task, the actual memory layout or access method of this data structure is not mandated. It is enough to:

Show all output here, (but you may judiciously use ellipses to shorten repetitive output text).

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Multi-dimensional array step by step in the jq programming language

Source code in the jq programming language

# The input is used to initialize the elements of the
# multi-dimensional array:
def multiarray(d):
  . as $in
  | if (d|length) == 1 then [range(0;d[0]) | $in]
    else multiarray(d[1:]) | multiarray( d[0:1] )
    end;

def ary: 0 | multiarray( [5, 4, 3, 2] );

ary | .[4][3][2][1]

ary | getpath( [4,3,2,1])

def ix: [4,3,2,1];

ary | setpath(ix; 100) | getpath(ix)
#=> 100

ary | setpath(ix; 100) | .[4][3][2][1]
#=> 100

def dimensions:
  def same(f):
    if length == 0 then true
    else (.[0]|f) as $first | reduce .[] as $i (true; if . then ($i|f) == $first else . end)
    end;

  if type == "array"
  then if length == 0 then [0]
       elif same( dimensions ) then [length] + (.[0]|dimensions)
       else null
       end
  else []
  end;

ary | dimensions
#=> [5,4,3,2]

  

You may also check:How to resolve the algorithm Entropy step by step in the C# programming language
You may also check:How to resolve the algorithm Function composition step by step in the ZX Spectrum Basic programming language
You may also check:How to resolve the algorithm CSV data manipulation step by step in the D programming language
You may also check:How to resolve the algorithm Last Friday of each month step by step in the 360 Assembly programming language
You may also check:How to resolve the algorithm Pseudo-random numbers/Middle-square method step by step in the Delphi programming language