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

# Alekh's solution

## to Rectangles in the Python Track

Published at Oct 19 2020 · 0 comments
Instructions
Test suite
Solution

#### Note:

This exercise has changed since this solution was written.

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).

## Exception messages

Sometimes it is necessary to raise an exception. When you do this, you should include a meaningful error message to indicate what the source of the error is. This makes your code more readable and helps significantly with debugging. Not every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include a message.

To raise a message with an exception, just write it as an argument to the exception type. For example, instead of `raise Exception`, you should write:

``````raise Exception("Meaningful message indicating the source of the error")
``````

## Running the tests

To run the tests, run `pytest rectangles_test.py`

Alternatively, you can tell Python to run the pytest module: `python -m pytest rectangles_test.py`

### Common `pytest` options

• `-v` : enable verbose output
• `-x` : stop running tests on first failure
• `--ff` : run failures from previous test before running other test cases

For other options, see `python -m pytest -h`

## Submitting Exercises

Note that, when trying to submit an exercise, make sure the solution is in the `\$EXERCISM_WORKSPACE/python/rectangles` directory.

You can find your Exercism workspace by running `exercism debug` and looking for the line that starts with `Workspace`.

For more detailed information about running tests, code style and linting, please see Running the Tests.

## Submitting Incomplete Solutions

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

### rectangles_test.py

``````import unittest

from rectangles import rectangles

# Tests adapted from `problem-specifications//canonical-data.json` @ v1.1.0

class RectanglesTest(unittest.TestCase):
def test_no_rows(self):
self.assertEqual(rectangles([]), 0)

def test_no_columns(self):
self.assertEqual(rectangles([""]), 0)

def test_no_rectangles(self):
self.assertEqual(rectangles([" "]), 0)

def test_one_rectangle(self):
self.assertEqual(rectangles(["+-+", "| |", "+-+"]), 1)

def test_two_rectangles_without_shared_parts(self):
self.assertEqual(rectangles(["  +-+", "  | |", "+-+-+", "| |  ", "+-+  "]), 2)

def test_five_rectangles_with_shared_parts(self):
self.assertEqual(rectangles(["  +-+", "  | |", "+-+-+", "| | |", "+-+-+"]), 5)

def test_rectangle_of_height_1_is_counted(self):
self.assertEqual(rectangles(["+--+", "+--+"]), 1)

def test_rectangle_of_width_1_is_counted(self):
self.assertEqual(rectangles(["++", "||", "++"]), 1)

def test_1x1_square_is_counted(self):
self.assertEqual(rectangles(["++", "++"]), 1)

def test_only_complete_rectangles_are_counted(self):
self.assertEqual(rectangles(["  +-+", "    |", "+-+-+", "| | -", "+-+-+"]), 1)

def test_rectangles_can_be_of_different_sizes(self):
self.assertEqual(
rectangles(
[
"+------+----+",
"|      |    |",
"+---+--+    |",
"|   |       |",
"+---+-------+",
]
),
3,
)

def test_corner_is_required_for_a_rectangle_to_be_complete(self):
self.assertEqual(
rectangles(
[
"+------+----+",
"|      |    |",
"+------+    |",
"|   |       |",
"+---+-------+",
]
),
2,
)

def test_large_input_with_many_rectangles(self):
self.assertEqual(
rectangles(
[
"+---+--+----+",
"|   +--+----+",
"+---+--+    |",
"|   +--+----+",
"+---+--+--+-+",
"+---+--+--+-+",
"+------+  | |",
"          +-+",
]
),
60,
)

if __name__ == "__main__":
unittest.main()``````
``````class stk():
def __init__(self, val, x, y):
self.val = val
self.x = x
self.y = y

is_conn = lambda a, b, c:all((lst[i].val==c or lst[i].val==3)for i in range(lst.index(a), lst.index(b), (rlen if(c==2) else 1)))
def find_pairs(lst, c):
pairs = []
for i in lst:
for j in lst[lst.index(i)+1:]:
if(c==2):
if(all(i[k].y==j[k].y for k in range(c)) and all(is_conn(i[k], j[k], c) for k in range(c))):
pairs += [[i, j]]
else:
if(i.x == j.x and is_conn(i, j, c) and i.val==j.val==3):
pairs += [[i,j]]
return pairs

def rectangles(strings):
try:
global rlen, lst
ref = {'+':3,'|':2,'-':1,' ':0}
rlen = len(strings[0])
lst = [stk(ref[strings[i//rlen][i%rlen]], i//rlen,i%rlen) for i in range(len(strings)*rlen)]
return len(find_pairs(find_pairs(lst,1),2))
except:
return 0``````