Take the 2-minute tour ×
Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

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]$.

share|improve this question
1  
Why ask in a math forum instead of in a programming forum? –  GEdgar Oct 15 '12 at 1:54

1 Answer 1

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 seq command rather than a pair of for-do loops.

The inner loop can determine y, since that loop has to run from 1 to L[x] where a particular x would be known. The outer loop can determine x, since the restrictions on x don't involve y.

The length of a list L is obtained with the command nops(L).

Experiment with a pair of nested seq calls 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 ranges i=a..b and j=c..d where some of the a,b,c,d of the inner seq may depend upon i or j of the outer seq.

seq( seq( [i,j], i=4..j ), j=1..6 );

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.

seq( i, i=4..2 );
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.