How do approach this problem?
Write a function points which takes a list L of non negative integers and returns a list of pairs $[x, y]$ where $1 ≤ x ≤$ the length of $L$ and $1 ≤ y ≤ L[x]$.
How do approach this problem?
Write a function points which takes a list L of non negative integers and returns a list of pairs $[x, y]$ where $1 ≤ x ≤$ the length of $L$ and $1 ≤ y ≤ L[x]$.
Copyright © 2021 JogjaFile Inc.
This is easily done as a nested loop, ie. one loop within another, where the one or both of the end-points of the inner loop will depend upon the current value of the outer loop.
You can use a pair of for-do loops, but then you have to be creative about where to store the list [x,y] that gets generated each time through the inner loop (or be inefficient in memory and repeatedly concatenate with a list of previous pairs). It's easier and efficient to accumulate instead an expression sequence of all the [x,y] pairs using a pair of nested calls to the
seqcommand rather than a pair of for-do loops.The inner loop can determine
y, since that loop has to run from 1 toL[x]where a particularxwould be known. The outer loop can determinex, since the restrictions onxdon't involvey.The length of a list
Lis obtained with the commandnops(L).Experiment with a pair of nested
seqcalls like the following, to get an idea of how they can form a longer sequence of results. Your goal is to nest such a pair in the right order and to find the particular rangesi=a..bandj=c..dwhere some of thea,b,c,dof the innerseqmay depend uponiorjof the outerseq.Notice that if the right end-point of a range is less that the left end-point then that range is empty: it produces nothing. This comes in handy. For example, the following produces
NULL.