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

Implement multi-part exercises #402

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
113 changes: 113 additions & 0 deletions demo-repository/exercises/demo3/demo3-etu1/descr.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<p>
This exercise is just a demo for the exercise environment.
</p>

<h2>The task</h2>

<p>
In this test-bed exercise you are asked to implement the basic
integer-arithmetic functions.
</p>


<ol>
<li>
Write a function <code>plus</code> of type <code>int -> int -> int</code>.
<br/>
Ex : val plus 3 4 : int = 7.
</li>
<li>
Write a function <code>minus</code> of type <code>int -> int -> int</code>.
<br/>
Ex : val minus 3 2 : int = 1.
</li>
<li>
Write a function <code>times</code> of type <code>int -> int -> int</code>.
<br/>
Ex : val times 2 6 : int = 12.
</li>
<li>
Write a function <code>divide</code> of type <code>int -> int -> int</code>.
Ex : val divide 8 4 : int = 2.
</li>
<li>
Write a function <code>sum3</code> of type <code>int -> int -> int -> int
</code>.
<br/>
Ex : val sum3 2 4 3 : int = 9.
</li>
<li>
Write a function <code>plus2</code> of type int -> int using <code> plus
</code> which add 2 to another integer.
</li>
</ol>
<p>
Now you have done these quite easy exercises, sure you can train on recursive
fonctions !
</p>

<h2> Recursive exercises </h2>

<p>
In this test-bed exercise you are asked to implement some recursive
integer-arithmetic functions.
</p>
<ol>
<li>
Write a function <code> sum_0_to_n </code> of type int -> int which
add integers from 0 to n, considering n can't be under 0.
<br/>
Ex : val sum_0_to_n 3 : int = 6.
</li>
<li>
Write a function <code> fact </code> of type int -> int which represent
the factorial (n!) function : 4! = 1x2x3x4 = 24.
<br/>
Ex : val fact 5 : int = 120.
</li>
</ol>

<h2> List exercise </h2>

<ol>
<il>
Write a function <code> list_0_to_n </code> of type <code> int -> int list
</code>.
This function must create a list containing integers between 0 and n. If
n<0, the function must return a Failure exception.
<br/>
Ex :list_0_to_n 4 -> [0;1;2;3;4]
</il>
<il>
Write a function <code> forall </code> of type <code> 'a list ->
('a -> bool) -> bool </code>, that return true if (all) the elements of
the list verify a predicat.
<br/>
Ex : forall [0;2;4] (fun x -> (x%2)=0) -> true
</il>
<il>
Write a function <code> cat_list </code> of type <code>'a list -> 'a list ->
'a list </code>. This function concatenate two list to make only one.
<br/>
Ex : cat_list ["a";"b"] ["c";"d";"e"] -> ["a";"b";"c";"d";"e"]
</il>
</ol>





<p>
<br/>
Feel free to introduce more errors and to stress the system,
the resulting grade for this exercise will not be taken into account
in the global grade and you might submit as many solutions as you
wish.
<br/>
If you end up writing an infinite computation, the system will
detect it after a while and ask you to stop the script. It will
slow your browser down until that point, since everything is done
on your side, via your JavaScript engine.
So don't worry, you can try and break the system as much as you
want, it should not break anything on our servers.
</p>
1 change: 1 addition & 0 deletions demo-repository/exercises/demo3/demo3-etu1/max_score.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
59
2 changes: 2 additions & 0 deletions demo-repository/exercises/demo3/demo3-etu1/prelude.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
(* Some code is loaded in the toplevel before your code. *)
let greetings = "Hello world!"
Empty file.
11 changes: 11 additions & 0 deletions demo-repository/exercises/demo3/demo3-etu1/solution.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

(* First part : simple functions on integers *)

let plus = (+);;

let minus = (-);;

let times a b = a*b;;

let divide = (/);;

1 change: 1 addition & 0 deletions demo-repository/exercises/demo3/demo3-etu1/template.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(* Write here your code *)
54 changes: 54 additions & 0 deletions demo-repository/exercises/demo3/demo3-etu1/test.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
open Test_lib
open Report

module Mutation_test = Mutation_test.Make (Test_lib)
open Mutation_test

let test_plus () =
test_function_2_against_solution
[%ty : int -> int -> int ] "plus"
[ (1, 1) ; (2, 2) ; (10, -10) ]
(* @
test_unit_tests_2
[%ty : int -> int -> int ] "plus"
[ ("Subtracts instead of adding", 1, fun x y -> x - y) ] *)

(* Au dessus (en commentaire) : test des tests de l'élève *)

let test_minus () =
test_function_2_against_solution
[%ty : int -> int -> int ] "minus"
[ (1, 1) ; (4, -2) ; (0, 10) ]


let test_times () =
test_function_2_against_solution
[%ty : int -> int -> int ] "times"
[ (1, 3) ; (2, 4) ; (3, 0) ]


let test_divide () =
test_function_2_against_solution
[%ty : int -> int -> int ] "divide"
[ (12, 4) ; (12, 5) ; (3, 0) ]




let () =
set_result @@
ast_sanity_check code_ast @@ fun () ->
[ Section
([ Text "Function:" ; Code "plus" ],
test_plus ()) ;
Section
([ Text "Function:" ; Code "minus" ],
test_minus ()) ;
Section
([ Text "Function:" ; Code "times" ],
test_times ()) ;
Section
([ Text "Function:" ; Code "divide" ],
test_divide ()) ;

]
113 changes: 113 additions & 0 deletions demo-repository/exercises/demo3/demo3-etu2/descr.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<p>
This exercise is just a demo for the exercise environment.
</p>

<h2>The task</h2>

<p>
In this test-bed exercise you are asked to implement the basic
integer-arithmetic functions.
</p>


<ol>
<li>
Write a function <code>plus</code> of type <code>int -> int -> int</code>.
<br/>
Ex : val plus 3 4 : int = 7.
</li>
<li>
Write a function <code>minus</code> of type <code>int -> int -> int</code>.
<br/>
Ex : val minus 3 2 : int = 1.
</li>
<li>
Write a function <code>times</code> of type <code>int -> int -> int</code>.
<br/>
Ex : val times 2 6 : int = 12.
</li>
<li>
Write a function <code>divide</code> of type <code>int -> int -> int</code>.
Ex : val divide 8 4 : int = 2.
</li>
<li>
Write a function <code>sum3</code> of type <code>int -> int -> int -> int
</code>.
<br/>
Ex : val sum3 2 4 3 : int = 9.
</li>
<li>
Write a function <code>plus2</code> of type int -> int using <code> plus
</code> which add 2 to another integer.
</li>
</ol>
<p>
Now you have done these quite easy exercises, sure you can train on recursive
fonctions !
</p>

<h2> Recursive exercises </h2>

<p>
In this test-bed exercise you are asked to implement some recursive
integer-arithmetic functions.
</p>
<ol>
<li>
Write a function <code> sum_0_to_n </code> of type int -> int which
add integers from 0 to n, considering n can't be under 0.
<br/>
Ex : val sum_0_to_n 3 : int = 6.
</li>
<li>
Write a function <code> fact </code> of type int -> int which represent
the factorial (n!) function : 4! = 1x2x3x4 = 24.
<br/>
Ex : val fact 5 : int = 120.
</li>
</ol>

<h2> List exercise </h2>

<ol>
<il>
Write a function <code> list_0_to_n </code> of type <code> int -> int list
</code>.
This function must create a list containing integers between 0 and n. If
n<0, the function must return a Failure exception.
<br/>
Ex :list_0_to_n 4 -> [0;1;2;3;4]
</il>
<il>
Write a function <code> forall </code> of type <code> 'a list ->
('a -> bool) -> bool </code>, that return true if (all) the elements of
the list verify a predicat.
<br/>
Ex : forall [0;2;4] (fun x -> (x%2)=0) -> true
</il>
<il>
Write a function <code> cat_list </code> of type <code>'a list -> 'a list ->
'a list </code>. This function concatenate two list to make only one.
<br/>
Ex : cat_list ["a";"b"] ["c";"d";"e"] -> ["a";"b";"c";"d";"e"]
</il>
</ol>





<p>
<br/>
Feel free to introduce more errors and to stress the system,
the resulting grade for this exercise will not be taken into account
in the global grade and you might submit as many solutions as you
wish.
<br/>
If you end up writing an infinite computation, the system will
detect it after a while and ask you to stop the script. It will
slow your browser down until that point, since everything is done
on your side, via your JavaScript engine.
So don't worry, you can try and break the system as much as you
want, it should not break anything on our servers.
</p>
1 change: 1 addition & 0 deletions demo-repository/exercises/demo3/demo3-etu2/max_score.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
59
2 changes: 2 additions & 0 deletions demo-repository/exercises/demo3/demo3-etu2/prelude.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
(* Some code is loaded in the toplevel before your code. *)
let greetings2 = "Hello world! This is second part of demo3"
Empty file.
20 changes: 20 additions & 0 deletions demo-repository/exercises/demo3/demo3-etu2/solution.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

(* First part : simple functions on integers *)

let sum3 a b c = a+b+c;;

let plus2 a = a+2 ;;

(* Second part : recursive functions on integers *)

let rec sum_0_to_n n =
if n<0 then failwith "sum_0_to_n : arg < 0 not allowed" else
match n with
|0-> 0
|n -> n+sum_0_to_n (n-1);;

let rec fact n =
match n with
| 0 | 1 -> 1
| n -> n*fact (n-1);;

1 change: 1 addition & 0 deletions demo-repository/exercises/demo3/demo3-etu2/template.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(* Write here your code *)
63 changes: 63 additions & 0 deletions demo-repository/exercises/demo3/demo3-etu2/test.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
open Test_lib
open Report

module Mutation_test = Mutation_test.Make (Test_lib)
open Mutation_test

let test_sum3 () =
test_function_against_solution
~gen:2 (* only 2 random test cases *)
[%funty: int -> int -> int -> int] (* function type *)
"sum3" (* function name *)
(* list of additional, explicit test cases *)
[ 10 @: 20 @:!! 30 ;
-1 @: -2 @:!! -3 ]

let test_plus2 () =
test_function_1_against_solution
[%ty: int -> int] (* function type *)
"plus2"
~gen:2
[ 12;6 ]


let test_sum_0_to_n () =
test_function_1_against_solution
[%ty : int -> int ]
"sum_0_to_n"
~gen:4
~test:(test_eq_exn
(fun exn1 exn2 -> match exn1, exn2 with
Failure _, Failure _ -> true | _, _ -> false))
[-4]

let test_fact () =
Section ([ Text "Function" ; Code "fact"],
test_function_1_against_solution
[%ty : int -> int ]
"fact"
~sampler:(fun () -> Random.int 11)
~gen:4
[]
)





let () =
set_result @@
ast_sanity_check code_ast @@ fun () ->
[ Section
([ Text "Function:" ; Code "sum3" ],
test_sum3 ()) ;
Section
([ Text "Function:" ; Code "plus2"],
test_plus2 ());
Section
([ Text "Function:" ; Code "sum_0_to_n"],
test_sum_0_to_n ());

test_fact ();

]
Loading