Order by pair comparisons: Difference between revisions

Line 306:
 
=={{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>
// 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 clrsrec fG n g=printfn "EnterIs colours%s seperatedless bythan spaces%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 "%sA sorted to %A using %d comparisonsquestions" clrs (clrs.Split ' '|>Array.choose fN|>Array.sortWith(fun n g->z<-z+1; comparefG 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>
{{out}}
Possible interaction:
Typical outputs:
<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
BlueIs 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
Red [|"Orange"; Yellow Green"Indigo"; "Blue"; Indigo"Yellow"; "Red"; "Green"; "Violet"|] sorted to [|"Violet"; "Red"; "Green"; "Indigo"; "Blue"; "Yellow"; "Orange"|] using 1920 comparisonsquestions
</pre>
 
2,171

edits