Non-continuous subsequences: Difference between revisions

Content added Content deleted
(Added Elixir)
m (→‎{{header|REXX}}: added/changed whitespace and comments, changed indentations, made other cosmetic improvements.)
Line 1,712: Line 1,712:


=={{header|REXX}}==
=={{header|REXX}}==
This REXX version also works with non-numeric (alphabetic) items   (as well as numbers).
<lang rexx>/*REXX program lists non-continuous subsequences (NCS), given a sequence. */
<lang rexx>/*REXX program lists all the non─continuous subsequences (NCS), given a sequence. */
parse arg list /*obtain the list from the C.L. */
if list='' then list=1 2 3 4 5 /*Not specified? Use the default*/
parse arg list /*obtain the arguments from the C. L. */
say 'list=' space(list); say /*display the list to terminal. */
if list='' | list==',' then list=1 2 3 4 5 /*Not specified? Then use the default.*/
w=words(list) ; #=0 /*W: words in list; # of NCS. */
say 'list=' space(list); say /*display the list to the terminal. */
$=left(123456789,w) /*build a string of decimal digs.*/
w=words(list) /*W: is the number of items in list. */
tail=right($,max(0,w-2)) /*construct a "fast" tail. */
$=left(123456789, w) /*build a string of decimal digits. */
tail=right($, max(0, w-2)) /*construct a fast tail for comparisons*/
#=0 /* [↓] L: length of Jth item. */
do j=13 to left($,1) || tail; L=length(j) /*step through list (using smart start)*/
if verify(j, $)\==0 then iterate /*Not one of the chosen (sequences) ? */
f=left(j,1) /*use the fist decimal digit of J. */
NCS=0 /*there isn't a non─continuous subseq. */
do k=2 to L; _=substr(j, k, 1) /*extract a single decimal digit of J.*/
if _ <= f then iterate j /*if next digit ≤, then skip this digit*/
if _ \== f+1 then NCS=1 /*it's OK as of now (that is, so far).*/
f=_ /*now have a new next decimal digit. */
end /*k*/


do j=13 to left($,1) || tail /*step through the list. */
if \NCS then iterate /*not OK? Then skip this number (item)*/
if verify(j,$)\==0 then iterate /*Not one of the chosen? */
#=#+1 /*Eureka! We found a number (or item).*/
f=left(j,1) /*use the 1st decimal digit of J.*/
@=; do m=1 for L /*build a sequence string to display. */
NCS=0 /*not non-continuous subsequence.*/
@=@ word(list, substr(j, m, 1)) /*pick off a number (item) to display. */
do k=2 to length(j); _=substr(j,k,1) /*pick off a single decimal digit*/
end /*m*/
if _ <= f then iterate j /*if next digit ≤, then skip it.*/
if _ \== f+1 then NCS=1 /*it's OK as of now. */
f=_ /*now have a new next decimal dig*/
end /*k*/


say 'a non─continuous subsequence: ' @ /*show the non─continuous subsequence. */
if \NCS then iterate /*not OK? Then skip this number.*/
#=#+1 /*Eureka! We found onea digit.*/
end /*j*/
say
x= /*the beginning of the NCS. */
do m=1 for length(j) /*build a sequence string to show*/
if #==0 then #='no' /*make it look more gooder Angleshy. */
say # "non─continuous subsequence"s(#) 'were found.'
x=x word(list,substr(j,m,1)) /*pick off a number to display. */
exit /*stick a fork in it, we're all done. */
end /*m*/
/*──────────────────────────────────────────────────────────────────────────────────────*/

s: if arg(1)==1 then return ''; return word(arg(2) 's', 1) /*pluralizer.*/</lang>
say 'a non-continuous subsequence: ' x /*show non─continous subsequence.*/
end /*j*/

if #==0 then #='no' /*make it more gooder Anglesh. */
say; say # "non-continuous subsequence"s(#) 'were found.'
exit /*stick a fork in it, we're done.*/
/*────────────────────────────────────────────────────────────────────────────*/
s: if arg(1)==1 then return ''; return word(arg(2) 's',1) /*plurals.*/</lang>
'''output''' &nbsp; when using the input: &nbsp; <tt> 1 2 3 4 </tt>
'''output''' &nbsp; when using the input: &nbsp; <tt> 1 2 3 4 </tt>
<pre>
<pre>