Non-continuous subsequences: Difference between revisions

Content added Content deleted
(Added Wren)
(→‎{{header|REXX}}: added/changed whitespace and comments, used templates for the output sections.)
Line 2,160: Line 2,160:
=={{header|REXX}}==
=={{header|REXX}}==
This REXX version also works with non-numeric (alphabetic) items   (as well as numbers).
This REXX version also works with non-numeric (alphabetic) items   (as well as numbers).
<lang rexx>/*REXX program lists all the 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 arguments from the C. L. */
parse arg list /*obtain optional argument from the CL.*/
if list='' | list==',' then list=1 2 3 4 5 /*Not specified? Then use the default.*/
if list='' | list=="," then list= 1 2 3 4 5 /*Not specified? Then use the default.*/
say 'list=' space(list); say /*display the list to the terminal. */
say 'list=' space(list); say /*display the list to the terminal. */
w=words(list) /*W: is the number of items in list. */
w= words(list) /*W: is the number of items in list. */
$=left(123456789, w) /*build a string of decimal digits. */
nums= strip( left(123456789, w) ) /*build a string of decimal digits. */
tail=right($, max(0, w-2)) /*construct a fast tail for comparisons*/
tail= right(nums, max(0, w-2) ) /*construct a fast tail for comparisons*/
#=0 /* [↓] L: length of Jth item. */
#= 0 /*#: number of non─continuous subseq. */
do j=13 to left($,1) || tail; L=length(j) /*step through list (using smart start)*/
do j=13 to left(nums,1) || tail /*step through list (using smart start)*/
if verify(j, $)\==0 then iterate /*Not one of the chosen (sequences) ? */
if verify(j, nums) \== 0 then iterate /*Not one of the chosen (sequences) ? */
f=left(j,1) /*use the fist decimal digit of J. */
f= left(j, 1) /*use the fist decimal digit of J. */
NCS=0 /*there isn't a non─continuous subseq. */
NCS= 0 /*so far, no non─continuous subsequence*/
do k=2 to L; _=substr(j, k, 1) /*extract a single decimal digit of J.*/
do k=2 for length(j)-1 /*search for " " " */
if _ <= f then iterate j /*if next digit ≤, then skip this digit*/
x= substr(j, k, 1) /*extract a single decimal digit of J.*/
if _ \== f+1 then NCS=1 /*it's OK as of now (that is, so far).*/
if x <= f then iterate j /*if next digit ≤, then skip this digit*/
f=_ /*now have a new next decimal digit. */
if x \== f+1 then NCS= 1 /*it's OK as of now (that is, so far).*/
end /*k*/
f= x /*now have a new next decimal digit. */
end /*k*/
$=
if \NCS then iterate /*not OK? Then skip this number (item)*/
#= # + 1 /*Eureka! We found a number (or item).*/
do m=1 for length(j) /*build a sequence string to display. */
$= $ word(list, substr(j, m, 1) ) /*obtain a number (or item) to display.*/
end /*m*/


say 'a non─continuous subsequence: ' $ /*show the non─continuous subsequence. */
if \NCS then iterate /*not OK? Then skip this number (item)*/
#=#+1 /*Eureka! We found a number (or item).*/
end /*j*/
@=; do m=1 for L /*build a sequence string to display. */
say /*help ensure visual fidelity in output*/
@=@ word(list, substr(j, m, 1)) /*pick off a number (item) to display. */
if #==0 then #= 'no' /*make it look more gooder Angleshy. */
say # "non─continuous subsequence"s(#) 'were found.' /*handle plurals.*/
end /*m*/
exit 0 /*stick a fork in it, we're all done. */

say 'a non─continuous subsequence: ' @ /*show the non─continuous subsequence. */
end /*j*/
say
if #==0 then #='no' /*make it look more gooder Angleshy. */
say # "non─continuous subsequence"s(#) 'were found.'
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
s: if arg(1)==1 then return ''; return word(arg(2) 's', 1) /*pluralizer.*/</lang>
s: if arg(1)==1 then return ''; return word( arg(2) 's', 1) /*simple pluralizer.*/</lang>
'''output''' &nbsp; when using the input: &nbsp; <tt> 1 2 3 4 </tt>
{{out|output|text=&nbsp; when using the input of: &nbsp; &nbsp; <tt> 1 &nbsp; 2 &nbsp; 3 &nbsp; 4 </tt>}}
<pre>
<pre>
list= 1 2 3 4
list= 1 2 3 4
Line 2,204: Line 2,205:
5 non-continuous subsequences were found.
5 non-continuous subsequences were found.
</pre>
</pre>
'''output''' &nbsp; when using the following input: &nbsp; <tt> a e I o u </tt>
{{out|output|text=&nbsp; when using the input of: &nbsp; &nbsp; <tt> a &nbsp; e &nbsp; I &nbsp; o &nbsp; u </tt>}}
<pre>
<pre>
list= a e I o u
list= a e I o u
Line 2,227: Line 2,228:
16 non-continuous subsequences were found.
16 non-continuous subsequences were found.
</pre>
</pre>
'''output''' &nbsp; when using the following [channel Islands (Great Britain)] as input: &nbsp; <tt> Alderney Guernsey Herm Jersey Sark </tt>
{{out|output|text=&nbsp; when using the following [five channel Islands (Great Britain)] as input: &nbsp; &nbsp; <tt> Alderney &nbsp; Guernsey &nbsp; Herm &nbsp; Jersey &nbsp; Sark </tt>}}
<pre>
<pre>
list= Alderney Guernsey Herm Jersey Sark
list= Alderney Guernsey Herm Jersey Sark
Line 2,250: Line 2,251:
16 non-continuous subsequences were found.
16 non-continuous subsequences were found.
</pre>
</pre>
'''output''' &nbsp; when using the following [six noble gases] as input: &nbsp; <tt> helium neon argon krypton xenon radon </tt>
{{out|output|text=&nbsp; when using the following [six noble gases] as input: &nbsp; &nbsp; <tt> helium &nbsp; neon &nbsp; argon &nbsp; krypton &nbsp; xenon &nbsp; radon </tt>}}
<pre>
<pre>
list= helium neon argon krypton xenon radon
list= helium neon argon krypton xenon radon