100 prisoners: Difference between revisions

F# "smartChoice" modified to use functional approach (though less efficient)
(Add F# version solution for 100 prisoners task)
(F# "smartChoice" modified to use functional approach (though less efficient))
Line 2,136:
// strategy optimizing drawer opening
let smartChoice max prisoner (drawers' : int array) =
inner 1 prisoner
let rec inner count selection =
|> Seq.unfold (fun selection ->
let card = drawers'.[selection-1]
Some (card, card))
if count > max then false // CASE 1: no more tries
|> Seq.take max
else if card = prisoner then true // CASE 2: prisoner found his/her card
|> Seq.contains prisoner
else inner (count+1) card // CASE 3: continue trying
inner 1 prisoner
let smartChoices (drawers' : int array) =
seq { 1..100 }
Anonymous user