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

Improved solution for smelodesousa/F5/5-brackets #40

Merged
merged 3 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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` which checks if the text contained in the first parameter has well-placed parentheses. It is suggested that you use 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. A 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 -> 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"
10 changes: 4 additions & 6 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 @@ -14,11 +12,11 @@ let sample_verify () =

let verifyS () =
test_function_2_against_solution
[%ty: char list -> char list -> bool ]
[%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