Dutch national flag problem: Difference between revisions

m
→‎{{header|REXX}}: changed/added comments and whitespace, changed indentations.
m (added whitespace before the TOC (table of contents).)
m (→‎{{header|REXX}}: changed/added comments and whitespace, changed indentations.)
Line 1,898:
The REXX solution could've been simplified somewhat by the use of the   '''countstr'''   BIF   (but some older REXX interpreters don't have).
 
<lang rexx>/*REXX program reorders a set of random colored balls into a correct order, which is the*/
/*────────────which is the────────────────────────────────── order of colors on the Dutch flag: red white blue.*/
parse arg N colors /*getobtain optional parametersarguments from the CL. */
if N=',' | N=''"," then N=15 /*Not specified? Then use the default.*/
if colors='' then colors=space( 'red white blue') /*use the" " " " " " default colors? */
#=words(colors) /*count the number of colors specified.*/
@=word(colors, #) word(colors, 1) /*ensure balls aren't already in order.*/
 
do g=3 to N /*generate a random # of colored balls.*/
@=@ word( colors, random(1, #) ) /*append a random color to the @ list.*/
end /*g*/
 
say 'number of colored balls generated = ' N ; say
say center(' original ball order ', length(@), '"'")
say @ ; say
$=; do j=1 for #; ; _=word(colors, j)
_=word(colors, j); $=$ copies(_' ', countWords(_, @))
end /*j*/
say
say center(' sorted ball order ', length(@), '"'")
say space($)
say
do k=2 to N /*verify the balls are in correct order*/
if wordpos(word($,k), colors) >= wordpos(word($,k-1), colors) then iterate
say "The list of sorted balls isn't in proper order!"; exit 13
Line 1,926:
say
say 'The sorted colored ball list has been confirmed as being sorted correctly.'
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────COUNTWORDS subroutine─────────────────────*/
countWords: procedure; parse arg ?,hay; s=1
do r=0 until _==0; _=wordpos(?, hay, s); s=_+1; end; /*r*/; return r</lang>
'''output''' &nbsp; when using the default input:
<pre>
number of colored balls generated = 15
Line 1,946:
 
===colors (as letters)===
<lang rexx>/*REXX program reorders a set of random colored balls into a correct order, which is the*/
/*────────────which is the────────────────────────────────── order of colors on the Dutch flag: red white blue.*/
parse arg N colors /*getobtain optional parametersarguments from the CL. */
if N=',' | N=''"," then N=15 /*Not specified? Then use the default.*/
if colors='' then colors=' "RWB'" /*use default: R=red, W=white, B=blue */
#=length(colors) /*count the number of colors specified.*/
@=right(colors, 1)left(colors, 1) /*ensure balls aren't already in order.*/
 
do g=3 to N /*generate a random # of colored balls.*/
@=@ ||substr( colors, random(1, #), 1) /*append a color (1char) to the @ list.*/
end /*g*/
 
say 'number of colored balls generated = ' N N ; say
say center(' original ball order ', max(30,2*#), '"'")
say @ ; say
$=; do j=1 for #; _=substr(colors, j, 1)
#=length(@) - length( space( translate(@, , _), 0) )
$=$ || copies(_, #)
end /*j*/
say center(' sorted ball order ', max(30, 2*#), '"'")
say $
say
do k=2 to N /*verify the balls are in correct order*/
if pos(substr($,k,1), colors) >= pos(substr($,k-1,1), colors) then iterate
say "The list of sorted balls isn't in proper order!"; exit 13
Line 1,974:
say
say 'The sorted colored ball list has been confirmed as being sorted correctly.'
exit /*stick a fork in it, we're all done. */</lang>
'''output''' &nbsp; when using the default input:
<pre>
number of colored balls generated = 15