Order by pair comparisons: Difference between revisions

(→‎{{header|F_Sharp|F#}}: marked incorrect: does not prompt user)
Line 199:
Sorted: ["red", "orange", "yellow", "green", "blue", "indigo", "violet"]. Total requests: 14.
</pre>
 
=={{header|Nim}}==
Using a list filled by binary insertion and a custom comparison function.
<lang Nim>import algorithm, strformat, strutils
 
let list = ["violet", "red", "green", "indigo", "blue", "yellow", "orange"]
 
var count = 0
 
proc comp(x, y: string): int =
if x == y: return 0
inc count
while true:
stdout.write &"{count:>2}) Is {x} less than {y} (y/n)? "
let answer = stdin.readLine()[0]
case answer
of 'y': return -1
of 'n': return 1
else: echo "Incorrect answer."
 
var sortedList: seq[string]
 
for elem in list:
sortedList.insert(elem, sortedList.upperBound(elem, comp))
 
echo "Sorted list: ", sortedList.join(", ")</lang>
 
{{out}}
<pre> 1) Is violet less than red (y/n)? n
2) Is violet less than green (y/n)? n
3) Is red less than green (y/n)? n
4) Is red less than indigo (y/n)? n
5) Is green less than indigo (y/n)? y
6) Is red less than blue (y/n)? n
7) Is indigo less than blue (y/n)? n
8) Is green less than blue (y/n)? n
9) Is indigo less than yellow (y/n)? y
10) Is violet less than yellow (y/n)? y
11) Is red less than orange (y/n)? n
12) Is green less than orange (y/n)? y
13) Is indigo less than orange (y/n)? y
Sorted list: blue, green, indigo, orange, red, violet, yellow</pre>
 
=={{header|Perl}}==
Anonymous user