Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -982,6 +982,14 @@
"control_flow_loops"
]
},
{
"slug": "rectangles",
"name": "Rectangles",
"uuid": "61fdcaf4-c2f6-4d13-a5ed-743997d567a6",
"practices": [],
"prerequisites": [],
"difficulty": 7
},
{
"slug": "bank-account",
"name": "Bank Account",
Expand Down
63 changes: 63 additions & 0 deletions exercises/practice/rectangles/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Instructions

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

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

The above diagram contains these 6 rectangles:

```text


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

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

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


```

```text


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

```text


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

```text

++
++


```

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).
17 changes: 17 additions & 0 deletions exercises/practice/rectangles/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"authors": [
"keiravillekode"
],
"files": {
"solution": [
"rectangles.el"
],
"test": [
"rectangles-test.el"
],
"example": [
".meta/example.el"
]
},
"blurb": "Count the rectangles in an ASCII diagram."
}
36 changes: 36 additions & 0 deletions exercises/practice/rectangles/.meta/example.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
;;; rectangles.el --- Rectangles (exercism) -*- lexical-binding: t; -*-

;;; Commentary:

;;; Code:

(require 'cl-lib)

(defun bottom (row left right)
(cl-loop for col from left to right
always (memq (aref row col) '(?+ ?-))))

(defun sides (rows left right)
(cl-loop for rows on rows
for row = (car rows)
while (and (memq (aref row left) '(?+ ?|))
(memq (aref row right) '(?+ ?|)))
when (and (= ?+ (aref row left))
(= ?+ (aref row right)))
count (bottom row left right)))

(defun top (first rest)
(cl-loop for right from 1 below (length first)
when (= ?+ (aref first right))
sum (cl-loop for left from (1- right) downto 0
while (memq (aref first left) '(?+ ?-))
when (= ?+ (aref first left))
sum (sides rest left right))))

(defun rectangles (strings)
(cl-loop for strings on strings
sum (top (car strings) (cdr strings))))


(provide 'rectangles)
;;; rectangles.el ends here
52 changes: 52 additions & 0 deletions exercises/practice/rectangles/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

[485b7bab-4150-40aa-a8db-73013427d08c]
description = "no rows"

[076929ed-27e8-45dc-b14b-08279944dc49]
description = "no columns"

[0a8abbd1-a0a4-4180-aa4e-65c1b1a073fa]
description = "no rectangles"

[a4ba42e9-4e7f-4973-b7c7-4ce0760ac6cd]
description = "one rectangle"

[ced06550-83da-4d23-98b7-d24152e0db93]
description = "two rectangles without shared parts"

[5942d69a-a07c-41c8-8b93-2d13877c706a]
description = "five rectangles with shared parts"

[82d70be4-ab37-4bf2-a433-e33778d3bbf1]
description = "rectangle of height 1 is counted"

[57f1bc0e-2782-401e-ab12-7c01d8bfc2e0]
description = "rectangle of width 1 is counted"

[ef0bb65c-bd80-4561-9535-efc4067054f9]
description = "1x1 square is counted"

[e1e1d444-e926-4d30-9bf3-7d8ec9a9e330]
description = "only complete rectangles are counted"

[ca021a84-1281-4a56-9b9b-af14113933a4]
description = "rectangles can be of different sizes"

[51f689a7-ef3f-41ae-aa2f-5ea09ad897ff]
description = "corner is required for a rectangle to be complete"

[d78fe379-8c1b-4d3c-bdf7-29bfb6f6dc66]
description = "large input with many rectangles"

[6ef24e0f-d191-46da-b929-4faca24b4cd2]
description = "rectangles must have four sides"
147 changes: 147 additions & 0 deletions exercises/practice/rectangles/rectangles-test.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
;;; rectangles-test.el --- Tests for Rectangles (exercism) -*- lexical-binding: t; -*-

;;; Commentary:

;;; Code:


(load-file "rectangles.el")
(declare-function rectangles "rectangles.el" (strings))


(ert-deftest no-rows ()
(should (= 0
(rectangles '()))))


(ert-deftest no-columns ()
(should (= 0
(rectangles '("")))))


(ert-deftest no-rectangles ()
(should (= 0
(rectangles '(" ")))))


(ert-deftest one-rectangle ()
(should (= 1
(rectangles '("+-+"
"| |"
"+-+")))))


(ert-deftest two-rectangles-without-shared-parts ()
(should (= 2
(rectangles '(" +-+"
" | |"
"+-+-+"
"| | "
"+-+ ")))))


(ert-deftest five-rectangles-with-shared-parts ()
(should (= 5
(rectangles '(" +-+"
" | |"
"+-+-+"
"| | |"
"+-+-+")))))


(ert-deftest rectangle-of-height-1-is-counted ()
(should (= 1
(rectangles '("+--+"
"+--+")))))


(ert-deftest rectangle-of-width-1-is-counted ()
(should (= 1
(rectangles '("++"
"||"
"++")))))


(ert-deftest 1x1-square-is-counted ()
(should (= 1
(rectangles '("++"
"++")))))


(ert-deftest only-complete-rectangles-are-counted ()
(should (= 1
(rectangles '(" +-+"
" |"
"+-+-+"
"| | -"
"+-+-+")))))


(ert-deftest rectangles-can-be-of-different-sizes ()
(should (= 3
(rectangles '("+------+----+"
"| | |"
"+---+--+ |"
"| | |"
"+---+-------+")))))


(ert-deftest corner-is-required-for-a-rectangle-to-be-complete ()
(should (= 2
(rectangles '("+------+----+"
"| | |"
"+------+ |"
"| | |"
"+---+-------+")))))


(ert-deftest large-input-with-many-rectangles ()
(should (= 60
(rectangles '("+---+--+----+"
"| +--+----+"
"+---+--+ |"
"| +--+----+"
"+---+--+--+-+"
"+---+--+--+-+"
"+------+ | |"
" +-+")))))


(ert-deftest rectangles-must-have-four-sides ()
(should (= 5
(rectangles '("+-+ +-+"
"| | | |"
"+-+-+-+"
" | | "
"+-+-+-+"
"| | | |"
"+-+ +-+")))))


(ert-deftest very-large-input ()
(should (= 2063
(rectangles '(" +-----+--------+ +-----+ "
"++---++-----+--------+---++-----++"
"||+--++-----+-+-++ | || ||"
"||| || +-+-++-+ | || ||"
"||| || | | || | | || ||"
"||| +++-----+-+-++-+-+---++-+ ||"
"||| ||| | | || | |+--++-+-+ ||"
"||| +++---+-+-+-++-+-++--++-+ | ||"
"||| |||+--+-+-+-+| | |+--++---+ ||"
"||| |||| | | | || | |+-+|| ||"
"||+-++++--+-+++-++-+-++-+++---++||"
"|| |+++--+-+++-+--+-+| ||| ||||"
"+++-+++++---++--+-++-++-+++---+|||"
" |+-+++++---++--+ || || ||| ||||"
" | +++++---++--+-++-++-++++ ||||"
" | |||| |+----++-++-++++--+++|"
" | |+++---+| || || || || |"
"+++ |||+---++----+| || || || |"
"||| +++----++----++-++-++----++-+"
"+++---++----++-----+-++-++----++ "
" +-+ ")))))


(provide 'rectangles-test)
;;; rectangles-test.el ends here
14 changes: 14 additions & 0 deletions exercises/practice/rectangles/rectangles.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
;;; rectangles.el --- Rectangles (exercism) -*- lexical-binding: t; -*-

;;; Commentary:

;;; Code:


(defun rectangles (strings)
(error "Delete this S-Expression and write your own implementation"))


(provide 'rectangles)
;;; rectangles.el ends here