-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
36 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/- | ||
# Problem 24 | ||
(Easy 🌟) Lotto: Draw `N` different random numbers from the set `1..M`. | ||
-/ | ||
|
||
-- You can use this function | ||
#check List.range | ||
|
||
def diffSelect (count range : Nat) : IO (List Nat) := do | ||
-- sorry | ||
if count > range then | ||
dbg_trace s!"can't draw {count} different numbers from 1..{range}" | ||
pure [] | ||
else | ||
let mut univ := List.range range |>.drop 1 | ||
let mut result : List Nat := [] | ||
for _ in [0 : count] do | ||
let (element, rest) ← extractOne univ | ||
univ := rest | ||
result := element :: result | ||
pure result | ||
where | ||
extractOne (univ : List Nat) : IO (Nat × List Nat) := do | ||
let index ← IO.rand 0 univ.length | ||
let element := univ.get! index | ||
let rest := univ.take index ++ univ.drop (index + 1) | ||
pure (element, rest) | ||
-- sorry | ||
|
||
#eval diffSelect 24 22 | ||
|
||
#eval diffSelect 6 49 | ||
|
||
#eval diffSelect 1998 1999 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters