How to resolve the algorithm Range extraction step by step in the NetRexx programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Range extraction step by step in the NetRexx programming language
Table of Contents
Problem Statement
A format for expressing an ordered list of integers is to use a comma separated list of either Example The list of integers: Is accurately expressed by the range expression: (And vice-versa).
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Range extraction step by step in the NetRexx programming language
Source code in the netrexx programming language
/*NetRexx program to test range extraction. ***************************
* 07.08.2012 Walter Pachl derived from my Rexx Version
* Changes: line continuation in aaa assignment changed
* 1e99 -> 999999999
* Do -> Loop
* words(aaa) -> aaa.words()
* word(aaa,i) -> aaa.word(i)
**********************************************************************/
Say 'NetRexx program derived from Rexx'
aaa='0 1 2 4 6 7 8 11 12 14 15 16 17 18 19 20 21 22 23 24 25 27 28 29'
aaa=aaa' 30 31 32 33 35 36 37 38 39'
say 'old='aaa;
aaa=aaa 999999999 /* artificial number at the end */
i=0 /* initialize index */
ol='' /* initialize output string */
comma='' /* will become a ',' lateron */
inrange=0
Loop While i<=aaa.words /* loop for all numbers */
i=i+1 /* index of next number */
n=aaa.word(i) /* the now current number */
If n=999999999 Then Leave /* we are at the end */
If inrange Then Do /* range was opened */
If aaa.word(i+1)<>n+1 Then Do /* following word not in range */
ol=ol||n /* so this number is the end */
inrange=0 /* and the range is over */
End /* else ignore current number */
End
Else Do /* not in a range */
ol=ol||comma||n /* add number (with comma) */
comma=',' /* to the output string */
If aaa.word(i+2)=n+2 Then Do /* if the nr after the next fits */
inrange=1 /* open a range */
ol=ol'-' /* append the range connector */
End
End
End
Say 'new='ol
/* NetRexx */
options replace format comments java crossref symbols nobinary
runSample(arg)
return
-- ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
-- Compact a list of numbers by reducing ranges
method compact(expanded) public static
nums = expanded.changestr(',', ' ').space -- remove possible commas & clean up the string
rezult = ''
RANGE = 0
FIRST = nums.word(1) -- set starting value
loop i_ = 2 to nums.words -- each word in the string is a number to examine
LOCAL = nums.word(i_)
if LOCAL - FIRST - RANGE == 1 then do
-- inside a range
RANGE = RANGE + 1
end
else do
-- not inside a range
if RANGE \= 0 then do
-- we have a range of numbers so collect this and reset
rezult = rezult || FIRST || delim(RANGE) || FIRST + RANGE || ','
RANGE = 0
end
else do
-- just collect this number
rezult = rezult || FIRST || ','
end
FIRST = LOCAL -- bump new starting value
end
end i_
if RANGE \= 0 then do
-- terminating value is a range
rezult = rezult || FIRST || delim(RANGE) || FIRST + RANGE
end
else do
-- terminating value is a single number
rezult = rezult || FIRST
end
return rezult.space(1, ',') -- format and return result string
-- ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
-- determine if the range delimiter should be a comma or dash
method delim(range) private static
if range == 1 then dlm = ','
else dlm = '-'
return dlm
-- ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
-- sample driver
method runSample(arg) public static
parse arg userInput
td = 0
if userInput.words > 0 then do
-- use input from command line
td[0] = td[0] + 1; r_ = td[0]; td[r_] = userInput
end
else do
-- use canned test data
td[0] = td[0] + 1; r_ = td[0]; td[r_] = ' -6, -3, -2, -1, 0, 1, 3, 4, 5, 7, 8, 9, 10, 11, 14, 15, 17, 18, 19, 20'
td[0] = td[0] + 1; r_ = td[0]; td[r_] = ' 0, 1, 2, 4, 6, 7, 8, 11, 12, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 27, 28, 29, 30, 31, 32, 33, 35, 36, 37, 38, 39'
td[0] = td[0] + 1; r_ = td[0]; td[r_] = ' -4, -3, -2, 0, 1, 2, 4, 6, 7, 8, 11, 12, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 27, 28, 29, 30, 31, 32, 33, 35, 36, 37, 38, 39'
end
loop r_ = 1 to td[0]
say 'Original: ' td[r_].changestr(',', ' ').space(1, ',')
say 'Compacted:' compact(td[r_])
say
end r_
return
You may also check:How to resolve the algorithm Mad Libs step by step in the REBOL programming language
You may also check:How to resolve the algorithm Josephus problem step by step in the Sidef programming language
You may also check:How to resolve the algorithm Sequence of non-squares step by step in the AWK programming language
You may also check:How to resolve the algorithm Create a file step by step in the LabVIEW programming language
You may also check:How to resolve the algorithm Optional parameters step by step in the OCaml programming language