Loops/Wrong ranges

From Rosetta Code
Revision as of 10:48, 16 September 2018 by rosettacode>Gerard Schildberger (→‎{{header|REXX}}: added the REXX computer programming language.)
Loops/Wrong ranges is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

Some languages have syntax or function(s) to generate a range of numeric values from a start value, a stop value, and an increment.

The purpose of this task is to select the range syntax/function that would generate at least two increasing numbers when given a stop value more than the start value and a positive increment of less than half the difference. You are than to use that same syntax/function but with different parameters; and show, here, what would happen.

Use these values if possible:

start stop increment Comment
-2 2 1 Normal
-2 2 0 Zero increment
-2 2 -1 Increments away from stop value
-2 2 10 First increment is beyond stop value
2 -2 1 Start more than stop: positive increment
2 2 1 Start equal stop: positive increment
2 2 -1 Start equal stop: negative increment
2 2 0 Start equal stop: zero increment
0 0 0 Start equal stop equal zero: zero increment

Python

Python has the range function. <lang python>import re from itertools import islice # To limit execution if it would generate huge values

  1. list(islice('ABCDEFG', 2)) --> ['A', 'B']
  2. list(islice('ABCDEFG', 4)) --> ['A', 'B', 'C', 'D']


data = start stop increment Comment -2 2 1 Normal -2 2 0 Zero increment -2 2 -1 Increments away from stop value -2 2 10 First increment is beyond stop value 2 -2 1 Start more than stop: positive increment 2 2 1 Start equal stop: positive increment 2 2 -1 Start equal stop: negative increment 2 2 0 Start equal stop: zero increment 0 0 0 Start equal stop equal zero: zero increment

table = [re.split(r'\s\s+', line.strip()) for line in data.strip().split('\n')]

  1. %%

for _start, _stop, _increment, comment in table[1:]:

   start, stop, increment = [int(x) for x in (_start, _stop, _increment)]
   print(f'{comment.upper()}:\n  range({start}, {stop}, {increment})')
   error, values = None, None
   try: 
       values = list(islice(range(start, stop, increment), 999))
   except ValueError as e:
       error = e
       print('  !!ERROR!!', e)
   if values is not None:
       if len(values) < 22:
           print('    =', values)
       else:
           print('    =', str(values[:22])[:-1], '...')

</lang>

Output:
NORMAL:
  range(-2, 2, 1)
    = [-2, -1, 0, 1]
ZERO INCREMENT:
  range(-2, 2, 0)
  !!ERROR!! range() arg 3 must not be zero
INCREMENTS AWAY FROM STOP VALUE:
  range(-2, 2, -1)
    = []
FIRST INCREMENT IS BEYOND STOP VALUE:
  range(-2, 2, 10)
    = [-2]
START MORE THAN STOP: POSITIVE INCREMENT:
  range(2, -2, 1)
    = []
START EQUAL STOP: POSITIVE INCREMENT:
  range(2, 2, 1)
    = []
START EQUAL STOP: NEGATIVE INCREMENT:
  range(2, 2, -1)
    = []
START EQUAL STOP: ZERO INCREMENT:
  range(2, 2, 0)
  !!ERROR!! range() arg 3 must not be zero
START EQUAL STOP EQUAL ZERO: ZERO INCREMENT:
  range(0, 0, 0)
  !!ERROR!! range() arg 3 must not be zero

REXX

Note that a do loop with zero by value, or a do loop that goes in the "wrong" direction is not considered an error in REXX as there are other methods of limiting the range (or stopping condition) within the loop body.   A special check was made in this REXX version to check for a runaway (race) condition. <lang rexx>/*REXX program demonstrates several versions of DO loops with "unusual" interations. */ @.=; @.1= ' -2 2 1 ' /*"normal". */

         @.2=  '  -2      2       0  '      /*"normal",                zero  increment.*/
         @.3=  '  -2      2      -1  '      /*increases away from stop, neg  increment.*/
         @.4=  '  -2      2      10  '      /*1st increment > stop, positive increment.*/
         @.5=  '   2     -2       1  '      /*start > stop,         positive increment.*/
         @.6=  '   2      2       1  '      /*start equals stop,    positive increment.*/
         @.7=  '   2      2      -1  '      /*start equals stop,    negative increment.*/
         @.8=  '   2      2       0  '      /*start equals stop,       zero  increment.*/
         @.9=  '   0      0       0  '      /*start equals stop,       zero  increment.*/

zLim= 10 /*a limit to check for runaway (race) loop.*/

                                            /*a zero increment is not an error in REXX.*/
 do k=1  while  @.k\==                    /*perform a  DO  loop with several ranges. */
 parse var   @.k    x  y  z  .              /*obtain the three values for a DO loop.   */
 say
 say center('start of performing DO loop number '   k   " with range: "  x y z,  79, '═')
 zz= 0
       do  j=x   to y   by z   until zz>=zLim           /* ◄───  perform the  DO  loop.*/
       say '   j ───►'  right(j, max(3, length(j) ) )   /*right justify J for alignment*/
       if z==0  then zz= zz + 1                         /*if zero inc, count happenings*/
       end   /*j*/
 if zz>=zLim  then say 'the DO loop for the '    k    " entry was terminated (runaway)."
 say center(' end  of performing DO loop number '   k   " with range: "  x y z,  79, '─')
 say
 end         /*k*/                              /*stick a fork in it,  we're all done. */</lang>
output:
══════════start of performing DO loop number  1  with range:  -2 2 1═══════════
   j ───►  -2
   j ───►  -1
   j ───►   0
   j ───►   1
   j ───►   2
────────── end  of performing DO loop number  1  with range:  -2 2 1───────────


══════════start of performing DO loop number  2  with range:  -2 2 0═══════════
   j ───►  -2
   j ───►  -2
   j ───►  -2
   j ───►  -2
   j ───►  -2
   j ───►  -2
   j ───►  -2
   j ───►  -2
   j ───►  -2
   j ───►  -2
the DO loop for the  2  entry was terminated (runaway).
────────── end  of performing DO loop number  2  with range:  -2 2 0───────────


══════════start of performing DO loop number  3  with range:  -2 2 -1══════════
────────── end  of performing DO loop number  3  with range:  -2 2 -1──────────


══════════start of performing DO loop number  4  with range:  -2 2 10══════════
   j ───►  -2
────────── end  of performing DO loop number  4  with range:  -2 2 10──────────


══════════start of performing DO loop number  5  with range:  2 -2 1═══════════
────────── end  of performing DO loop number  5  with range:  2 -2 1───────────


═══════════start of performing DO loop number  6  with range:  2 2 1═══════════
   j ───►   2
─────────── end  of performing DO loop number  6  with range:  2 2 1───────────


══════════start of performing DO loop number  7  with range:  2 2 -1═══════════
   j ───►   2
────────── end  of performing DO loop number  7  with range:  2 2 -1───────────


═══════════start of performing DO loop number  8  with range:  2 2 0═══════════
   j ───►   2
   j ───►   2
   j ───►   2
   j ───►   2
   j ───►   2
   j ───►   2
   j ───►   2
   j ───►   2
   j ───►   2
   j ───►   2
the DO loop for the  8  entry was terminated (runaway).
─────────── end  of performing DO loop number  8  with range:  2 2 0───────────


═══════════start of performing DO loop number  9  with range:  0 0 0═══════════
   j ───►   0
   j ───►   0
   j ───►   0
   j ───►   0
   j ───►   0
   j ───►   0
   j ───►   0
   j ───►   0
   j ───►   0
   j ───►   0
the DO loop for the  9  entry was terminated (runaway).
─────────── end  of performing DO loop number  9  with range:  0 0 0───────────