Order by pair comparisons: Difference between revisions

Content added Content deleted
Line 306: Line 306:


=={{header|F_Sharp|F#}}==
=={{header|F_Sharp|F#}}==
This task uses [https://rosettacode.org/wiki/Factorial_base_numbers_indexing_permutations_of_a_collection#F.23 Factorial base numbers indexing permutations of a collection (F#)]
<lang fsharp>
<lang fsharp>
// Order by pair comparisons. Nigel Galloway: April 23rd., 2021
// Order by pair comparisons. Nigel Galloway: April 23rd., 2021
let clrs=let n=System.Random() in lN2p [|for g in 7..-1..2->n.Next(g)|] [|"Red";"Orange";"Yellow";"Green";"Blue";"Indigo";"Violet"|]
type colours= Violet |Red |Green |Indigo |Blue |Yellow |Orange
let clrs=printfn "Enter colours seperated by spaces"; System.Console.ReadLine()
let rec fG n g=printfn "Is %s less than %s" n g; match System.Console.ReadLine() with "Yes"-> -1|"No"->1 |_->printfn "Enter Yes or No"; fG n g
let mutable z=0 in printfn "%A sorted to %A using %d questions" clrs (clrs|>Array.sortWith(fun n g->z<-z+1; fG n g)) z
let fN g=match g with "Violet"->Some(Violet) |"Red"->Some(Red) |"Green"->Some(Green) |"Indigo"->Some(Indigo) |"Blue"->Some(Blue) |"Yellow"->Some(Yellow) |"Orange"->Some(Orange) |_->None
let mutable z=0 in printfn "%s sorted to %A using %d comparisons" clrs (clrs.Split ' '|>Array.choose fN|>Array.sortWith(fun n g->z<-z+1; compare n g)) z
</lang>
</lang>
{{out}}
{{out}}
Possible interaction:
Typical outputs:
<pre>
<pre>
Is Indigo less than Orange
Enter colours seperated by spaces
Yes
Blue Red Yellow
Is Blue less than Orange
Blue Red Yellow sorted to [|Red; Blue; Yellow|] using 3 comparisons
Yes

Is Blue less than Indigo
Enter colours seperated by spaces
No
Red Orange Yellow Green Blue Indigo Violet
Is Yellow less than Orange
Red Orange Yellow Green Blue Indigo Violet sorted to [|Violet; Red; Green; Indigo; Blue; Yellow; Orange|] using 19 comparisons
Yes
Is Yellow less than Blue
No
Is Red less than Orange
Yes
Is Red less than Yellow
Yes
Is Red less than Blue
Yes
Is Red less than Indigo
Yes
Is Green less than Orange
Yes
Is Green less than Yellow
Yes
Is Green less than Blue
Yes
Is Green less than Indigo
Yes
Is Green less than Red
No
Is Violet less than Orange
Yes
Is Violet less than Yellow
Yes
Is Violet less than Blue
Yes
Is Violet less than Indigo
Yes
Is Violet less than Green
Yes
Is Violet less than Red
Yes
[|"Orange"; "Indigo"; "Blue"; "Yellow"; "Red"; "Green"; "Violet"|] sorted to [|"Violet"; "Red"; "Green"; "Indigo"; "Blue"; "Yellow"; "Orange"|] using 20 questions
</pre>
</pre>