Skip to content

Commit

Permalink
Improved solution for smelodesousa/F5/5-brackets (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
zazedd and zazedd authored Feb 9, 2024
1 parent 5b94c8c commit d9e3a6e
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 22 deletions.
4 changes: 2 additions & 2 deletions exercises/smelodesousa/F5/5-brackets/descr.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ Note that, as a result, concatenating two texts with well-placed parentheses or

# Goal

Define a function `verify : char list -> char list -> bool` which checks if the text contained in the first parameter has well-placed parentheses. It is suggested that you use the second parameter (another `char list`, which we will define the accumulator) to _accumulate_ the intermediate scan results. Therefore, the accumulator is empty at the beginning of the analysis, and if during the check the accumulator has a parenthesis '(' as its first element, then the current status of the analysis still waits for a parenthesis ')' which corresponds to the closing of the parenthesis in the accumulator. A proper use of this accumulator makes checking much easier!
Define a function `verify : char list -> bool` that checks if the text contained in the first parameter has well-placed parentheses. If you aren't familiar with OCaml's list iterators, feel free to write a local function involving an accumulator to _accumulate_ the intermediate scan results. Therefore, the accumulator is empty at the beginning of the analysis, and if during the check the accumulator has a parenthesis '(' as its first element, then the current status of the analysis still waits for a parenthesis ')' which corresponds to the closing of the parenthesis in the accumulator. Proper use of this accumulator makes checking much easier!

For example:

`verify ['a';'(';'a';'b';'(';'b';')';'c';'(';'o';'k';'a';')';'n';')';'h'] [] = true`
`verify ['a';'(';'a';'b';'(';'b';')';'c';'(';'o';'k';'a';')';'n';')';'h'] = true`
4 changes: 4 additions & 0 deletions exercises/smelodesousa/F5/5-brackets/meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
[
"Rui Barata",
"[email protected]"
],
[
"Leonardo Santos",
"[email protected]"
]
],
"backward_exercises": [
Expand Down
17 changes: 5 additions & 12 deletions exercises/smelodesousa/F5/5-brackets/solution.ml
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
let verify text accumulator =
let clean_accumulator acc =
match acc with
| c1::c2::t -> if c1 = ')' && c2 = '(' then t else acc
| c1::[] -> acc
| [] -> acc in
let rec verify_aux text accumulator =
match text with
| c::t when (c = '(' || c = ')') -> verify_aux t (clean_accumulator (c::accumulator))
| c::t when (c <> '(' || c <> ')') -> verify_aux t accumulator
| [] -> accumulator in
(verify_aux text accumulator) = []
let verify text =
List.fold_left
(fun acc c -> if acc < 0 then acc else match c with '(' -> acc + 1 | ')' -> acc - 1 | _ -> acc)
0 text
= 0
2 changes: 1 addition & 1 deletion exercises/smelodesousa/F5/5-brackets/template.ml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
let verify text accumulator =
let verify text =
failwith "Replace with your solution"
12 changes: 5 additions & 7 deletions exercises/smelodesousa/F5/5-brackets/test.ml
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
open Test_lib
open Report

let sample_verify_right () =
([], [])
let sample_verify_wrong () =
([], [])
let sample_verify_right () = []
let sample_verify_wrong () = []

let sample_verify () =
let () = Random.self_init () in
Expand All @@ -13,12 +11,12 @@ let sample_verify () =
else sample_verify_wrong ()

let verifyS () =
test_function_2_against_solution
[%ty: char list -> char list -> bool ]
test_function_1_against_solution
[%ty: char list -> bool ]
"verify"
~sampler: sample_verify
~gen: 0
[(['a';'(';'a';'b';'(';'b';')';'c';'(';'o';'k';'a';')';'n';')';'h'], []); (['a';'(';'a';'b';'(';'b';')';'c';'(';'o';'k';'a';'n';')';'h'], [])]
[[')'; '(']; ['a';'(';'a';'b';'(';'b';')';'c';'(';'o';'k';'a';')';'n';')';'h']; ['a';'(';'a';'b';'(';'b';')';'c';'(';'o';'k';'a';'n';')';'h']]

let () =
set_result @@
Expand Down

0 comments on commit d9e3a6e

Please sign in to comment.