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]: Triangle #233

Merged
merged 5 commits into from
Jul 26, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,16 @@
"prerequisites": [],
"difficulty": 1
},
{
"slug": "triangle",
"name": "Triangle",
"uuid": "b439d97f-3661-45d4-903d-6c739e4ab05f",
"practices": [
"bools"
],
"prerequisites": [],
"difficulty": 1
},
{
"slug": "etl",
"name": "Etl",
Expand Down
20 changes: 20 additions & 0 deletions exercises/practice/triangle/.docs/instructions.append.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# About Enum
The enum statement allows you to create a strongly typed set of labels.
You can use that enumeration in the code without having to parse or check for spelling errors.

Enumerations are internally represented as integers with a starting value of zero.
By default, PowerShell assigns the first label in the list the value zero, and assigns the remaining labels with consecutive integers.

Example:
```pwsh
Enum Fruits {
APPLE #default internal value 0
ORANGE #default internal value 1
MANGO #default internal value 2
}

$myFavFruit = [Fruits]::APPLE
Write-host $myFavFruit => APPLE
``````

To know more about Enum in powershell please check out [Enum module.](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_enum)
29 changes: 29 additions & 0 deletions exercises/practice/triangle/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Instructions

Determine if a triangle is equilateral, isosceles, or scalene.

An _equilateral_ triangle has all three sides the same length.

An _isosceles_ triangle has at least two sides the same length.
(It is sometimes specified as having exactly two sides the same length, but for the purposes of this exercise we'll say at least two.)

A _scalene_ triangle has all sides of different lengths.

## Note

For a shape to be a triangle at all, all sides have to be of length > 0, and the sum of the lengths of any two sides must be greater than or equal to the length of the third side.

In equations:

Let `a`, `b`, and `c` be sides of the triangle.
Then all three of the following expressions must be true:

```text
a + b ≥ c
b + c ≥ a
a + c ≥ b
```

See [Triangle Inequality][triangle-inequality]

[triangle-inequality]: https://en.wikipedia.org/wiki/Triangle_inequality
46 changes: 46 additions & 0 deletions exercises/practice/triangle/.meta/Triangle.example.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
Enum Triangle {
glaxxie marked this conversation as resolved.
Show resolved Hide resolved
EQUILATERAL
ISOSCELES
SCALENE
}

Function Get-Triangle() {
<#
.SYNOPSIS
Determine if a triangle is EQUILATERAL, ISOSCELES, or SCALENE.

.DESCRIPTION
Given 3 sides of a triangle, return the type of that triangle if it is a valid triangle.

.PARAMETER Sides
The lengths of a triangle's sides.

.EXAMPLE
Get-Triangle -Sides @(1,2,3)
#>

[CmdletBinding()]
Param (
[double[]]$Sides
)
Test-Validity $Sides
switch (($Sides | Select-Object -Unique).Count) {
1 { return [Triangle]::EQUILATERAL }
2 { return [Triangle]::ISOSCELES }
default { return [Triangle]::SCALENE }
}
}

Function Test-Validity() {
[CmdletBinding()]
Param (
[double[]]$Sides
)
if ($Sides | Where-Object {$_ -le 0}) {
Throw "All side lengths must be positive."
}
$sortedSides = $Sides | Sort-Object
if ($sortedSides[0] + $sortedSides[1] -lt $sortedSides[2]) {
Throw "Side lengths violate triangle inequality."
}
}
17 changes: 17 additions & 0 deletions exercises/practice/triangle/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"authors": ["glaxxie"],
"files": {
"solution": [
"Triangle.ps1"
],
"test": [
"Triangle.tests.ps1"
],
"example": [
".meta/Triangle.example.ps1"
]
},
"blurb": "Determine if a triangle is equilateral, isosceles, or scalene.",
"source": "The Ruby Koans triangle project, parts 1 & 2",
"source_url": "https://web.archive.org/web/20220831105330/http://rubykoans.com"
}
73 changes: 73 additions & 0 deletions exercises/practice/triangle/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# 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.

[8b2c43ac-7257-43f9-b552-7631a91988af]
description = "equilateral triangle -> all sides are equal"

[33eb6f87-0498-4ccf-9573-7f8c3ce92b7b]
description = "equilateral triangle -> any side is unequal"

[c6585b7d-a8c0-4ad8-8a34-e21d36f7ad87]
description = "equilateral triangle -> no sides are equal"

[16e8ceb0-eadb-46d1-b892-c50327479251]
description = "equilateral triangle -> all zero sides is not a triangle"

[3022f537-b8e5-4cc1-8f12-fd775827a00c]
description = "equilateral triangle -> sides may be floats"

[cbc612dc-d75a-4c1c-87fc-e2d5edd70b71]
description = "isosceles triangle -> last two sides are equal"

[e388ce93-f25e-4daf-b977-4b7ede992217]
description = "isosceles triangle -> first two sides are equal"

[d2080b79-4523-4c3f-9d42-2da6e81ab30f]
description = "isosceles triangle -> first and last sides are equal"

[8d71e185-2bd7-4841-b7e1-71689a5491d8]
description = "isosceles triangle -> equilateral triangles are also isosceles"

[840ed5f8-366f-43c5-ac69-8f05e6f10bbb]
description = "isosceles triangle -> no sides are equal"

[2eba0cfb-6c65-4c40-8146-30b608905eae]
description = "isosceles triangle -> first triangle inequality violation"

[278469cb-ac6b-41f0-81d4-66d9b828f8ac]
description = "isosceles triangle -> second triangle inequality violation"

[90efb0c7-72bb-4514-b320-3a3892e278ff]
description = "isosceles triangle -> third triangle inequality violation"

[adb4ee20-532f-43dc-8d31-e9271b7ef2bc]
description = "isosceles triangle -> sides may be floats"

[e8b5f09c-ec2e-47c1-abec-f35095733afb]
description = "scalene triangle -> no sides are equal"

[2510001f-b44d-4d18-9872-2303e7977dc1]
description = "scalene triangle -> all sides are equal"

[c6e15a92-90d9-4fb3-90a2-eef64f8d3e1e]
description = "scalene triangle -> first and second sides are equal"

[3da23a91-a166-419a-9abf-baf4868fd985]
description = "scalene triangle -> first and third sides are equal"

[b6a75d98-1fef-4c42-8e9a-9db854ba0a4d]
description = "scalene triangle -> second and third sides are equal"

[70ad5154-0033-48b7-af2c-b8d739cd9fdc]
description = "scalene triangle -> may not violate triangle inequality"

[26d9d59d-f8f1-40d3-ad58-ae4d54123d7d]
description = "scalene triangle -> sides may be floats"
22 changes: 22 additions & 0 deletions exercises/practice/triangle/Triangle.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Function Get-Triangle() {
<#
.SYNOPSIS
Determine if a triangle is equilateral, isosceles, or scalene.

.DESCRIPTION
Given 3 sides of a triangle, return the type of that triangle if it is a valid triangle.

.PARAMETER Sides
The lengths of a triangle's sides.

.EXAMPLE
Get-Triangle -Sides @(1,2,3)
Return: [Triangle]::SCALENE
#>

[CmdletBinding()]
Param (
[double[]]$Sides
)
Throw "Please implement this function"
}
103 changes: 103 additions & 0 deletions exercises/practice/triangle/Triangle.tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
BeforeAll {
. "./Triangle.ps1"
}

Describe "Test Get-Triangle" {
Context "Passing Tests" {
It "equilateral all sides are equal" {
$got = Get-Triangle -Sides @(2, 2, 2)
$want = [Triangle]::EQUILATERAL

$got | Should -BeExactly $want
}

It "equilateral all sides are equal and float" {
$got = Get-Triangle -Sides @(0.5, 0.5, 0.5)
$want = [Triangle]::EQUILATERAL

$got | Should -BeExactly $want
}

It "isosceles first and last sides are equal" {
$got = Get-Triangle -Sides @(3, 4, 3)
$want = [Triangle]::ISOSCELES

$got | Should -BeExactly $want
}

It "isosceles first two sides are equal" {
$got = Get-Triangle -Sides @(4, 4, 3)
$want = [Triangle]::ISOSCELES

$got | Should -BeExactly $want
}

It "isosceles last two sides are equal" {
$got = Get-Triangle -Sides @(3, 4, 4)
$want = [Triangle]::ISOSCELES

$got | Should -BeExactly $want
}

It "isosceles two sides are equal and float" {
$got = Get-Triangle -Sides @(0.5, 0.5, 0.4)
$want = [Triangle]::ISOSCELES

$got | Should -BeExactly $want
}

It "isosceles two big equal sides" {
$got = Get-Triangle -Sides @(10, 10, 2)
$want = [Triangle]::ISOSCELES

$got | Should -BeExactly $want
}

It "scalene no sides are equal descending order" {
$got = Get-Triangle -Sides @(5, 4, 3)
$want = [Triangle]::SCALENE

$got | Should -BeExactly $want
}

It "scalene no sides are equal ascending order" {
$got = Get-Triangle -Sides @(4, 7, 11)
$want = [Triangle]::SCALENE

$got | Should -BeExactly $want
}

It "scalene sides are float" {
$got = Get-Triangle -Sides @(0.4, 0.3, 0.6)
$want = [Triangle]::SCALENE

$got | Should -BeExactly $want
}
}

Context "Invalid Inputs" {
It "all zero sides is not a triangle" {
{ Get-Triangle -Sides @(0, 0, 0)} | Should -Throw "All side lengths must be positive."
}

It "negative sides are illegal" {
{ Get-Triangle -Sides @(3, 4, -5)} | Should -Throw "All side lengths must be positive."
}

It "first isosceles triangle inequality violation" {
{ Get-Triangle -Sides @(1, 1, 3)} | Should -Throw "Side lengths violate triangle inequality."
}

It "second isosceles triangle inequality violation" {
{ Get-Triangle -Sides @(1, 3, 1)} | Should -Throw "Side lengths violate triangle inequality."
}

It "third isosceles triangle inequality violation" {
{ Get-Triangle -Sides @(3, 1, 1)} | Should -Throw "Side lengths violate triangle inequality."
}

It "SCALENE triangle inequality violation" {
{ Get-Triangle -Sides @(7, 3, 2)} | Should -Throw "Side lengths violate triangle inequality."
}
}
}