Published at Jun 01 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).

Refer to the exercism help page for Rust installation and learning resources.

Execute the tests with:

```
$ cargo test
```

All but the first test have been ignored. After you get the first test to
pass, open the tests source file which is located in the `tests`

directory
and remove the `#[ignore]`

flag from the next test and get the tests to pass
again. Each separate test is a function with `#[test]`

flag above it.
Continue, until you pass every test.

If you wish to run all tests without editing the tests source file, use:

```
$ cargo test -- --ignored
```

To run a specific test, for example `some_test`

, you can use:

```
$ cargo test some_test
```

If the specific test is ignored use:

```
$ cargo test some_test -- --ignored
```

To learn more about Rust tests refer to the online test documentation

Make sure to read the Modules chapter if you haven't already, it will help you with organizing your files.

After you have solved the exercise, please consider using the additional utilities, described in the installation guide, to further refine your final solution.

To format your solution, inside the solution directory use

```
cargo fmt
```

To see, if your solution contains some common ineffective use cases, inside the solution directory use

```
cargo clippy --all-targets
```

Generally you should submit all files in which you implemented your solution (`src/lib.rs`

in most cases). If you are using any external crates, please consider submitting the `Cargo.toml`

file. This will make the review process faster and clearer.

The exercism/rust repository on GitHub is the home for all of the Rust exercises. If you have feedback about an exercise, or want to help implement new exercises, head over there and create an issue. Members of the rust track team are happy to help!

If you want to know more about Exercism, take a look at the contribution guide.

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

```
use rectangles::count;
#[test]
fn test_zero_area_1() {
let lines = &[];
assert_eq!(0, count(lines))
}
#[test]
#[ignore]
fn test_zero_area_2() {
let lines = &[""];
assert_eq!(0, count(lines))
}
#[test]
#[ignore]
fn test_empty_area() {
let lines = &[" "];
assert_eq!(0, count(lines))
}
#[test]
#[ignore]
fn test_one_rectangle() {
let lines = &[
"+-+",
"| |",
"+-+",
];
assert_eq!(1, count(lines))
}
#[test]
#[ignore]
fn test_two_rectangles_no_shared_parts() {
let lines = &[
" +-+",
" | |",
"+-+-+",
"| | ",
"+-+ "
];
assert_eq!(2, count(lines))
}
#[test]
#[ignore]
fn test_five_rectangles_three_regions() {
let lines = &[
" +-+",
" | |",
"+-+-+",
"| | |",
"+-+-+"
];
assert_eq!(5, count(lines))
}
#[test]
#[ignore]
fn rectangle_of_height_1() {
let lines = &[
"+--+",
"+--+"
];
assert_eq!(1, count(lines))
}
#[test]
#[ignore]
fn rectangle_of_width_1() {
let lines = &[
"++",
"||",
"++"
];
assert_eq!(1, count(lines))
}
#[test]
#[ignore]
fn unit_square() {
let lines = &[
"++",
"++"
];
assert_eq!(1, count(lines))
}
#[test]
#[ignore]
fn test_incomplete_rectangles() {
let lines = &[
" +-+",
" |",
"+-+-+",
"| | -",
"+-+-+"
];
assert_eq!(1, count(lines))
}
#[test]
#[ignore]
fn test_complicated() {
let lines = &[
"+------+----+",
"| | |",
"+---+--+ |",
"| | |",
"+---+-------+"
];
assert_eq!(3, count(lines))
}
#[test]
#[ignore]
fn test_not_so_complicated() {
let lines = &[
"+------+----+",
"| | |",
"+------+ |",
"| | |",
"+---+-------+"
];
assert_eq!(2, count(lines))
}
#[test]
#[ignore]
fn test_large_input_with_many_rectangles() {
let lines = &[
"+---+--+----+",
"| +--+----+",
"+---+--+ |",
"| +--+----+",
"+---+--+--+-+",
"+---+--+--+-+",
"+------+ | |",
" +-+"
];
assert_eq!(60, count(lines))
}
```

```
type Point = (usize, usize);
use std::collections::HashSet;
pub fn count(lines: &[&str]) -> u32 {
if lines.is_empty() {
return 0;
}
let mut corners: HashSet<Point> = HashSet::new();
for i in 0..lines.len() {
for j in 0..lines[0].len() {
if &lines[i][j..=j] == "+" {
corners.insert((i, j));
}
}
}
let found: Vec<(Point, Point)> = Vec::new();
let mut count = 0;
for i in corners.iter() {
for j in corners.iter() {
if !(found.contains(&(*i, *j))) {
if j.0 > i.0 && j.1 > i.1 && is_rectancle(lines, i.0, i.1, j.0, j.1) {
count += 1;
}
}
}
}
count
}
fn is_rectancle(lines: &[&str], i: usize, j: usize, m: usize, n: usize) -> bool {
// diagnonal points should also be '+'
if &lines[i][n..=n] != "+" || &lines[m][j..=j] != "+" {
return false;
}
let is_valid_row = |a: &str| {
if a != "+" && a != "-" {
return false;
}
true
};
let is_valid_col = |a: &str| {
if a != "+" && a != "|" {
return false;
}
true
};
// valid characters are |
for k in lines.iter().take(m).skip(i) {
if !is_valid_col(&k[j..=j]) || !is_valid_col(&k[n..=n]) {
return false;
}
}
for k in j..n {
if !is_valid_row(&lines[i][k..=k]) || !is_valid_row(&lines[m][k..=k]) {
return false;
}
}
true
}
```

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,048 exercises across 51 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.

## Community comments