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

[New Exercise]: Rectangles #324

Merged
merged 1 commit into from
Dec 14, 2023
Merged
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 @@ -968,6 +968,14 @@
"prerequisites": [],
"difficulty": 5
},
{
"slug": "rectangles",
"name": "Rectangles",
"uuid": "0c624e88-1baf-4760-b0ff-6a138e62113f",
"practices": [],
"prerequisites": [],
"difficulty": 6
},
{
"slug": "two-bucket",
"name": "Two Bucket",
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).
94 changes: 94 additions & 0 deletions exercises/practice/rectangles/.meta/Rectangles.example.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
Function Get-Rectangles() {
<#
.SYNOPSIS
Count the rectangles in an ASCII diagram.

.DESCRIPTION
Given an ASCII diagram, count all the rectangles exist in it.
A complete rectangle should have 4 corners and 4 valid sides.
Example of possilbe sides:
Valid: "+--+", "++"
Invalid: "+-- +", "-+"

.PARAMETER Strings
An array of string that represent an ASCII diagram.
You may assume that the input is always a proper rectangle (every string in the array has equal length).

.EXAMPLE
$diagram = @(
"+-+-+",
"+-+-+"
)

Get-Rectangles -Strings $diagram
Returns: 3
#>
[CmdletBinding()]
Param(
[string[]]$Strings
)
if ($Strings.Count -eq 0) {return 0}
$validEdges = ExtractEdge $Strings
CountRectangle @validEdges
}

Function CountRectangle() {
<#
.DESCRIPTION
From the extracted edges, do some logic to check if the 4 edges can create a rectangle.
#>
param (
[object[]] $HorizontalEdges,
[hashtable] $VerticalEdges
)
$hozEdgesCount = $HorizontalEdges.Count
$count = 0
for ($i = 0; $i -lt $hozEdgesCount; $i++) {
for ($j = $i+1; $j -lt $hozEdgesCount; $j++) {
$topEdge = $HorizontalEdges[$i]
$botEdge = $HorizontalEdges[$j]
if ($topEdge[1] -eq $botEdge[1] -and $topEdge[-1] -eq $botEdge[-1]) {
$leftEdge = "$($topEdge[0])$($topEdge[1])$($botEdge[0])$($botEdge[1])"
$rightEdge = "$($topEdge[2])$($topEdge[3])$($botEdge[2])$($botEdge[3])"
if ($VerticalEdges.ContainsKey($leftEdge) -and $VerticalEdges.ContainsKey($rightEdge)) {
$count++
}
}
}
}
$count
}

Function ExtractEdge([string[]]$strings) {
<#
.DESCRIPTION
From the array of strings, extract all the valid edge (side) and store them into two groups : horizontal and vertical.
#>
$validEdges = @{
HorizontalEdges = @()
VerticalEdges = [ordered]@{}
}
for ($l = 0; $l -lt $strings.Count; $l++) {
$line = $strings[$l]
for ($i = 0; $i -lt $line.Length; $i++) {
if ($line[$i] -eq "+") {
#horizontal
for ($j = $i+1; $j -lt $line.Length; $j++) {
$hozEdge = $line.Substring($i, $j-$i+1)
if($hozEdge -match '^\+(-|\+)*\+$') {
$validEdges.HorizontalEdges += ,@($l,$i,$l,$j)
}
}
#vertical
$vertEdge = "$($line[$i])"
for ($l2 = $l+1; $l2 -lt $strings.Count; $l2++) {
$vertEdge += $strings[$l2][$i]
if($vertEdge -match '^\+(\||\+)*\+$') {
$validEdges.VerticalEdges["$l$i$l2$i"] = $true
}
}
}
}
}
$validEdges
}
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": [
"glaxxie"
],
"files": {
"solution": [
"Rectangles.ps1"
],
"test": [
"Rectangles.tests.ps1"
],
"example": [
".meta/Rectangles.example.ps1"
]
},
"blurb": "Count the rectangles in an ASCII diagram."
}
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"
31 changes: 31 additions & 0 deletions exercises/practice/rectangles/Rectangles.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
Function Get-Rectangles() {
<#
.SYNOPSIS
Count the rectangles in an ASCII diagram.

.DESCRIPTION
Given an ASCII diagram, count all the rectangles exist in it.
A complete rectangle should have 4 corners and 4 valid sides.
Example of possilbe sides:
Valid: "+--+", "++"
Invalid: "+-- +", "-+"

.PARAMETER Strings
An array of string that represent an ASCII diagram.
You may assume that the input is always a proper rectangle (every string in the array has equal length).

.EXAMPLE
$diagram = @(
"+-+-+",
"+-+-+"
)

Get-Rectangles -Strings $diagram
Returns: 3
#>
[CmdletBinding()]
Param(
[string[]]$Strings
)
Throw "Please implement this function"
}
Loading