Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write an F# function countshuffles n that counts how many calls to shuffle on a

ID: 3882365 • Letter: W

Question

Write an F# function countshuffles n that counts how many calls to shuffle on a deck of n distinct "cards" it takes to put the deck back into its original order:

> countshuffles 4;;

val it : int = 2

(To see that this result is correct, note that shuffle [1;2;3;4] = [1;3;2;4], and shuffle [1;3;2;4] = [1;2;3;4].) What is countshuffles 52?

Hint: Define an auxiliary function countaux(deck, target) that takes two lists and returns the number of shuffles it takes to make deck equal to target.

Explanation / Answer

let rec countaux = function | (d,t) when d = t -> 0 | (d,t) -> 1 + countaux(d, shuffle t) let countshuffles n = let deck = [1..n] let target = shuffle deck 1 + countaux(deck, target)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote