Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Turn exercise 023 to practice #2230

Merged
merged 5 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions practice/023/answer/impl.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
let rand_select list n =
let rec extract acc n = function
| [] -> raise Not_found
| h :: t -> if n = 0 then (h, acc @ t) else extract (h :: acc) (n - 1) t
in
let extract_rand list len =
extract [] (Random.int len) list
in
let rec aux n acc list len =
if n = 0 then acc else
let picked, rest = extract_rand list len in
aux (n - 1) (picked :: acc) rest (len - 1)
in
let len = List.length list in
aux (min n len) [] list len

Ozyugoo marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 2 additions & 0 deletions practice/023/answer/test/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
(include_subdirs no)
(test (name run) (libraries ounit2 ex))
3 changes: 3 additions & 0 deletions practice/023/answer/test/run.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module Test = Ex.Make(Ex.Answer)

let () = OUnit2.run_test_tt_main Test.v
2 changes: 2 additions & 0 deletions practice/023/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
(include_subdirs qualified)
(library (name ex) (libraries ounit2))
1 change: 1 addition & 0 deletions practice/023/dune-project
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(lang dune 3.7)
15 changes: 15 additions & 0 deletions practice/023/ex.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
open OUnit2

module type Testable = sig
val rand_select : 'a list -> int -> 'a list
end

module Make(Tested: Testable) : sig val v : test end = struct
let v = "rand_select" >::: [
"nil" >:: (fun _ -> assert_equal [] (Tested.rand_select [] 0));
"cons" >:: (fun _ -> assert_equal ["e"; "c"; "g"] (Tested.rand_select ["a"; "b"; "c"; "d"; "e"; "f"; "g"; "h"] 6));
]
end

module Work : Testable = Work.Impl
module Answer : Testable = Answer.Impl
1 change: 1 addition & 0 deletions practice/023/work/impl.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
let rand_select _ = failwith "Not yet implemented"
2 changes: 2 additions & 0 deletions practice/023/work/test/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
(include_subdirs no)
(test (name run) (libraries ounit2 ex))
3 changes: 3 additions & 0 deletions practice/023/work/test/run.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module Test = Ex.Make(Ex.Work)

let () = OUnit2.run_test_tt_main Test.v
Loading