How to resolve the algorithm Loops/With multiple ranges step by step in the jq programming language

Published on 12 May 2024 09:40 PM
#Jq

How to resolve the algorithm Loops/With multiple ranges step by step in the jq programming language

Table of Contents

Problem Statement

Some languages allow multiple loop ranges, such as the PL/I example (snippet) below.

Simulate/translate the above PL/I program snippet as best as possible in your language,   with particular emphasis on the   do   loop construct. The   do   index must be incremented/decremented in the same order shown. If feasible, add commas to the two output numbers (being displayed). Show all output here.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Loops/With multiple ranges step by step in the jq programming language

Source code in the jq programming language

# If using gojq, one may want to preserve integer precision, so:
def power($b): . as $in | reduce range(0;$b) as $i (1; . * $in);

{ prod: 1,                             #  start with a product of unity.
  sum:  0,                             #  henceforth skip redundant comments.
    x:  5,
    y: -5,
    z: -2,
  one:  1,
  three: 3,
  seven: 7 }
| .x as $x  
| reduce (range(-.three; 1 + 3|power(3); .three),
          range(-.seven; 1 + .seven;     .x),
          range(    555; 1 +  550 - .y),
          range(     22; -1 -28;          -.three),
          range(1927   ; 1 + 1939),
          range(.x     ; .y;              .z),
          range(11|power($x); 1 + ( 11 | power($x)) + .one)) as $j (.;
             .sum += ($j|length)                         # add absolute value of $j (!)
             | if (.prod|length) < (2|power(27)) and $j != 0
	       then .prod *= $j
	       else .
	       end )
| "sum=  \(.sum)",
  "prod= \(.prod)"

  

You may also check:How to resolve the algorithm Array concatenation step by step in the Trith programming language
You may also check:How to resolve the algorithm Anagrams step by step in the Nim programming language
You may also check:How to resolve the algorithm Quine step by step in the bootBASIC programming language
You may also check:How to resolve the algorithm FizzBuzz step by step in the beeswax programming language
You may also check:How to resolve the algorithm Sum digits of an integer step by step in the ArnoldC programming language