Skip to content

Commit

Permalink
add Problem24
Browse files Browse the repository at this point in the history
  • Loading branch information
Seasawher committed Mar 24, 2024
1 parent d9bd611 commit a609afb
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
1 change: 1 addition & 0 deletions Src.lean
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import Src.Problem20
import Src.Problem21
import Src.Problem22
import Src.Problem23
import Src.Problem24
import Src.Problem3
import Src.Problem31
import Src.Problem32
Expand Down
34 changes: 34 additions & 0 deletions Src/Problem24.lean
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
1 change: 1 addition & 0 deletions md/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
* [21: Insert an element](./build/Problem21.md)
* [22: Integers within a range](./build/Problem22.md)
* [23: Random selection](./build/Problem23.md)
* [24: Draw N diffrent random numbers](./build/Problem24.md)

# 31-40: Arithmetic

Expand Down

0 comments on commit a609afb

Please sign in to comment.