-
Notifications
You must be signed in to change notification settings - Fork 304
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Turn exercise 024 to practice (#2231)
* Turn exercise 024 to practice * Chore: Modified Turn exercise 024 to practice * Update Turn erexcise 024 to practice
- Loading branch information
Showing
9 changed files
with
54 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
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 | ||
|
||
let range a b = | ||
let rec aux acc high low = | ||
if high >= low then | ||
aux (high :: acc) (high - 1) low | ||
else acc | ||
in | ||
if a < b then aux [] b a else List.rev (aux [] a b) | ||
|
||
let lotto_select n m = rand_select (range 1 m) n |
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,2 @@ | ||
(include_subdirs no) | ||
(test (name run) (libraries ounit2 ex)) |
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,3 @@ | ||
module Test = Ex.Make(Ex.Answer) | ||
|
||
let () = OUnit2.run_test_tt_main Test.v |
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,2 @@ | ||
(include_subdirs qualified) | ||
(library (name ex) (libraries ounit2)) |
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 @@ | ||
(lang dune 3.7) |
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,15 @@ | ||
open OUnit2 | ||
|
||
module type Testable = sig | ||
val lotto_select : int -> int -> int list | ||
end | ||
|
||
module Make(Tested: Testable) : sig val v : test end = struct | ||
let v = "lotto_select" >::: [ | ||
"nil" >:: (fun _ -> assert_equal [] (Tested.lotto_select 0 0)); | ||
"cons" >:: (fun _ -> assert_equal [20; 28; 45; 16; 24; 38] (Tested.lotto_select 6 49)); | ||
] | ||
end | ||
|
||
module Work : Testable = Work.Impl | ||
module Answer : Testable = Answer.Impl |
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 @@ | ||
let lotto_select _ = failwith "Not yet implemented" |
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,2 @@ | ||
(include_subdirs no) | ||
(test (name run) (libraries ounit2 ex)) |
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,3 @@ | ||
module Test = Ex.Make(Ex.Work) | ||
|
||
let () = OUnit2.run_test_tt_main Test.v |