Order by pair comparisons: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) m (→{{header|Julia}}: comment) |
(→{{header|REXX}}: added the computer programming language REXX.) |
||
Line 357: | Line 357: | ||
12. Is indigo [ less than | greater than | equal to ] blue ? ( < = > ) > |
12. Is indigo [ less than | greater than | equal to ] blue ? ( < = > ) > |
||
sorted => [red orange yellow green blue indigo violet] |
sorted => [red orange yellow green blue indigo violet] |
||
</pre> |
|||
=={{header|REXX}}== |
|||
{{trans|Python}} |
|||
Extra code was added to the REXX program to handle incorrectly formatted answers. |
|||
<lang>/*REXX pgm orders some items based on (correct) answers from a carbon─based life form. */ |
|||
colors= 'violet red green indigo blue yellow orange' |
|||
q= 0; #= 0; $= |
|||
do j=1 for words(colors); q= inSort( word(colors, j), q) |
|||
end /*j*/ /*poise questions the CBLF about order.*/ |
|||
say |
|||
do i=1 for #; say ' query' right(i, length(#) )":" !.i |
|||
end /*i*/ /* [↑] show the list of queries to CBLF*/ |
|||
say |
|||
say 'final ordering: ' $ |
|||
exit 0 |
|||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
|||
getAns: #= # + 1; _= copies('─', 8); y_n= ' Answer y/n' |
|||
do try=0 until ansU='Y' | ansU='N' |
|||
if try>0 then say _ '(***error***) incorrect answer.' |
|||
ask= _ ' is ' center(x,6) " less than " center(word($, mid+1),6) '?' |
|||
say ask y_n; parse pull ans 1 ansU; ansU= space(ans); upper ansU |
|||
end /*until*/; !.#= ask ' ' ans; return |
|||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
|||
inSort: parse arg x, q; hi= words($); lo= 0 |
|||
do q=q-1 while lo<hi; mid= (lo+hi) % 2 |
|||
call getAns; if ansU=='Y' then hi= mid |
|||
else lo= mid + 1 |
|||
end /*q*/ |
|||
$= subword($, 1, lo) x subword($, lo+1); return q</lang> |
|||
{{out|output|text= (only showing the results and eliding the querying/answering):}} |
|||
<pre> |
|||
query 1: ──────── is red less than violet ? y |
|||
query 2: ──────── is green less than violet ? y |
|||
query 3: ──────── is green less than red ? n |
|||
query 4: ──────── is indigo less than green ? n |
|||
query 5: ──────── is indigo less than violet ? y |
|||
query 6: ──────── is blue less than indigo ? y |
|||
query 7: ──────── is blue less than green ? n |
|||
query 8: ──────── is yellow less than blue ? y |
|||
query 9: ──────── is yellow less than green ? y |
|||
query 10: ──────── is yellow less than red ? n |
|||
query 11: ──────── is orange less than blue ? y |
|||
query 12: ──────── is orange less than yellow ? y |
|||
query 13: ──────── is orange less than red ? n |
|||
final ordering: red orange yellow green blue indigo violet |
|||
</pre> |
</pre> |
||