ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

Published at Mar 16 2019
·
0 comments

Instructions

Test suite

Solution

Count the rectangles in an ASCII diagram like the one below.

```
+--+
++ |
+-++--+
| | |
+--+--+
```

The above diagram contains 6 rectangles:

```
+-----+
| |
+-----+
```

```
+--+
| |
| |
| |
+--+
```

```
+--+
| |
+--+
```

```
+--+
| |
+--+
```

```
+--+
| |
+--+
```

```
++
++
```

You may assume that the input is always a proper rectangle (i.e. the length of every line equals the length of the first line).

For installation and learning resources, refer to the exercism help page.

To work on the exercises, you will need `Opam`

and `Base`

. Consult opam website for instructions on how to install `opam`

for your OS. Once `opam`

is installed open a terminal window and run the following command to install base:

```
opam install base
```

To run the tests you will need `OUnit`

. Install it using `opam`

:

```
opam install ounit
```

A Makefile is provided with a default target to compile your solution and run the tests. At the command line, type:

```
make
```

`utop`

is a command line program which allows you to run Ocaml code interactively. The easiest way to install it is via opam:

```
opam install utop
```

Consult utop for more detail.

The exercism/ocaml repository on GitHub is the home for all of the Ocaml exercises.

If you have feedback about an exercise, or want to help implementing a new one, head over there and create an issue. We'll do our best to help you!

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

```
open OUnit2
open Rectangles
let ae exp got _test_ctxt = assert_equal exp got ~printer:string_of_int
let tests = [
"no rows" >::
ae 0 (count_rectangles [||]);
"no columns" >::
ae 0 (count_rectangles [|""|]);
"no rectangles" >::
ae 0 (count_rectangles [|" "|]);
"one rectangle" >::
ae 1 (count_rectangles [|"+-+";
"| |";
"+-+"|]);
"two rectangles without shared parts" >::
ae 2 (count_rectangles [|" +-+";
" | |";
"+-+-+";
"| | ";
"+-+ "|]);
"five rectangles with shared parts" >::
ae 5 (count_rectangles [|" +-+";
" | |";
"+-+-+";
"| | |";
"+-+-+"|]);
"rectangle of height 1 is counted" >::
ae 1 (count_rectangles [|"+--+";
"+--+"|]);
"rectangle of width 1 is counted" >::
ae 1 (count_rectangles [|"++";
"||";
"++"|]);
"1x1 square is counted" >::
ae 1 (count_rectangles [|"++";
"++"|]);
"only complete rectangles are counted" >::
ae 1 (count_rectangles [|" +-+";
" |";
"+-+-+";
"| | -";
"+-+-+"|]);
"rectangles can be of different sizes" >::
ae 3 (count_rectangles [|"+------+----+";
"| | |";
"+---+--+ |";
"| | |";
"+---+-------+"|]);
"corner is required for a rectangle to be complete" >::
ae 2 (count_rectangles [|"+------+----+";
"| | |";
"+------+ |";
"| | |";
"+---+-------+"|]);
"large input with many rectangles" >::
ae 60 (count_rectangles [|"+---+--+----+";
"| +--+----+";
"+---+--+ |";
"| +--+----+";
"+---+--+--+-+";
"+---+--+--+-+";
"+------+ | |";
" +-+"|]);
]
let () =
run_test_tt_main ("rectangles tests" >::: tests)
```

```
open Base
let is_vertice c = Char.(c = '+')
let is_h_edge c = Char.(c = '-' || is_vertice c)
let is_v_edge c = Char.(c = '|' || is_vertice c)
let is_rectangle diagram r c r' c' =
let range = Sequence.range
~start:`exclusive
~stop:`exclusive in
is_vertice diagram.(r).[c'] &&
is_vertice diagram.(r').[c] &&
(range c c'
|> Sequence.for_all
~f:(fun i ->
is_h_edge diagram.(r).[i] &&
is_h_edge diagram.(r').[i]
)
) &&
(range r r'
|> Sequence.for_all
~f:(fun i ->
is_v_edge diagram.(i).[c] &&
is_v_edge diagram.(i).[c']
)
)
let count_rectangles diagram =
let height = Array.length diagram in
let width = String.length diagram.(0) in
let count = ref 0 in
for r = 0 to height - 2 do
for c = 0 to width - 2 do
if is_vertice diagram.(r).[c]
then
for r' = r + 1 to height - 1 do
for c' = c + 1 to width - 1 do
if is_vertice diagram.(r').[c'] &&
is_rectangle diagram r c r' c'
then
Int.incr count
done
done
done
done;
!count
let count_rectangles diagram =
if Array.length diagram < 2 ||
String.length diagram.(0) < 2
then 0
else count_rectangles diagram
```

A huge amount can be learned from reading other peopleâ€™s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

- What compromises have been made?
- Are there new concepts here that you could read more about to improve your understanding?

Level up your programming skills with 3,423 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.

## Community comments