From a52d2a742b439285896d798339f327f71902852d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A1s=20B=20Nagy?= <20251272+BNAndras@users.noreply.github.com> Date: Thu, 5 Sep 2024 11:48:19 -0700 Subject: [PATCH] Add triangle (#287) --- config.json | 8 + .../practice/triangle/.docs/instructions.md | 29 +++ exercises/practice/triangle/.meta/config.json | 19 ++ .../triangle/.meta/reference/triangle.bal | 46 +++++ exercises/practice/triangle/.meta/tests.toml | 73 ++++++++ exercises/practice/triangle/Ballerina.toml | 5 + exercises/practice/triangle/Dependencies.toml | 38 ++++ .../practice/triangle/tests/triangle_test.bal | 168 ++++++++++++++++++ exercises/practice/triangle/triangle.bal | 12 ++ 9 files changed, 398 insertions(+) create mode 100644 exercises/practice/triangle/.docs/instructions.md create mode 100644 exercises/practice/triangle/.meta/config.json create mode 100644 exercises/practice/triangle/.meta/reference/triangle.bal create mode 100644 exercises/practice/triangle/.meta/tests.toml create mode 100644 exercises/practice/triangle/Ballerina.toml create mode 100644 exercises/practice/triangle/Dependencies.toml create mode 100644 exercises/practice/triangle/tests/triangle_test.bal create mode 100644 exercises/practice/triangle/triangle.bal diff --git a/config.json b/config.json index c9051074..132d43c4 100644 --- a/config.json +++ b/config.json @@ -610,6 +610,14 @@ "prerequisites": [], "difficulty": 2 }, + { + "slug": "triangle", + "name": "Triangle", + "uuid": "e6c705f5-9b34-44a1-9108-d43207bfd97c", + "practices": [], + "prerequisites": [], + "difficulty": 2 + }, { "slug": "scrabble-score", "name": "Scrabble Score", diff --git a/exercises/practice/triangle/.docs/instructions.md b/exercises/practice/triangle/.docs/instructions.md new file mode 100644 index 00000000..ac390087 --- /dev/null +++ b/exercises/practice/triangle/.docs/instructions.md @@ -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 diff --git a/exercises/practice/triangle/.meta/config.json b/exercises/practice/triangle/.meta/config.json new file mode 100644 index 00000000..8a57ecf0 --- /dev/null +++ b/exercises/practice/triangle/.meta/config.json @@ -0,0 +1,19 @@ +{ + "authors": [ + "BNAndras" + ], + "files": { + "solution": [ + "triangle.bal" + ], + "test": [ + "tests/triangle_test.bal" + ], + "example": [ + ".meta/reference/triangle.bal" + ] + }, + "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" +} diff --git a/exercises/practice/triangle/.meta/reference/triangle.bal b/exercises/practice/triangle/.meta/reference/triangle.bal new file mode 100644 index 00000000..4af8d05c --- /dev/null +++ b/exercises/practice/triangle/.meta/reference/triangle.bal @@ -0,0 +1,46 @@ +enum TriangleType { + EQUILATERAL, + ISOSCELES, + SCALENE +} + +# Determines the type of triangle based on the sides +# +# + sides - The sides of the triangle +# + t - The type of triangle +# + return - Whether the sides form the triangle type +function kind(float[]|int[] sides, TriangleType t) returns boolean { + if !valid(sides) { + return false; + } + + match t { + EQUILATERAL => { + return sides[0] == sides[1] && sides[1] == sides[2]; + } + ISOSCELES => { + return sides[0] == sides[1] || sides[1] == sides[2] || sides[0] == sides[2]; + } + SCALENE => { + return sides[0] != sides[1] && sides[1] != sides[2] && sides[0] != sides[2]; + } + } + + return false; +} + +# Determines if the sides form a valid triangle +# +# + sides - The sides of the triangle +# + return - Whether the sides form a valid triangle +function valid(int[]|float[] sides) returns boolean { + if sides is float[] { + float[] sorted = sides.sort(); + return sorted.every(function(float num) returns boolean => num > 0.0) && sorted[0] + sorted[1] > sorted[2]; + } else if sides is int[]{ + int[] sorted = sides.sort(); + return sorted.every(function(int num) returns boolean => num > 0) && sorted[0] + sorted[1] > sorted[2]; + } + + return false; +} \ No newline at end of file diff --git a/exercises/practice/triangle/.meta/tests.toml b/exercises/practice/triangle/.meta/tests.toml new file mode 100644 index 00000000..7db09164 --- /dev/null +++ b/exercises/practice/triangle/.meta/tests.toml @@ -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" diff --git a/exercises/practice/triangle/Ballerina.toml b/exercises/practice/triangle/Ballerina.toml new file mode 100644 index 00000000..420efbd0 --- /dev/null +++ b/exercises/practice/triangle/Ballerina.toml @@ -0,0 +1,5 @@ +[package] +org = "ballerina_exercism" +name = "triangle" +version = "0.1.0" +distribution = "2201.5.0" diff --git a/exercises/practice/triangle/Dependencies.toml b/exercises/practice/triangle/Dependencies.toml new file mode 100644 index 00000000..543fe970 --- /dev/null +++ b/exercises/practice/triangle/Dependencies.toml @@ -0,0 +1,38 @@ +# AUTO-GENERATED FILE. DO NOT MODIFY. + +# This file is auto-generated by Ballerina for managing dependency versions. +# It should not be modified by hand. + +[ballerina] +dependencies-toml-version = "2" +distribution-version = "2201.5.0" + +[[package]] +org = "ballerina" +name = "jballerina.java" +version = "0.0.0" +scope = "testOnly" + +[[package]] +org = "ballerina" +name = "test" +version = "0.0.0" +scope = "testOnly" +dependencies = [ + {org = "ballerina", name = "jballerina.java"} +] +modules = [ + {org = "ballerina", packageName = "test", moduleName = "test"} +] + +[[package]] +org = "ballerina_exercism" +name = "triangle" +version = "0.1.0" +dependencies = [ + {org = "ballerina", name = "test"} +] +modules = [ + {org = "ballerina_exercism", packageName = "triangle", moduleName = "triangle"} +] + diff --git a/exercises/practice/triangle/tests/triangle_test.bal b/exercises/practice/triangle/tests/triangle_test.bal new file mode 100644 index 00000000..5a5dae7f --- /dev/null +++ b/exercises/practice/triangle/tests/triangle_test.bal @@ -0,0 +1,168 @@ +import ballerina/test; + + +@test:Config {} +function testEquilateralTriangleAllSidesAreEqual() { + int[] sides = [2, 2, 2]; + test:assertTrue(kind(sides, EQUILATERAL)); +} + +@test:Config { + enable: false +} +function testEquilateralTriangleAnySideIsUnequal() { + int[] sides = [2, 3, 2]; + test:assertFalse(kind(sides, EQUILATERAL)); +} + +@test:Config { + enable: false +} +function testEquilateralTriangleNoSidesAreEqual() { + int[] sides = [5, 4, 6]; + test:assertFalse(kind(sides, EQUILATERAL)); +} + +@test:Config { + enable: false +} +function testEquilateralTriangleAllZeroSidesIsNotATriangle() { + int[] sides = [0, 0, 0]; + test:assertFalse(kind(sides, EQUILATERAL)); +} + +@test:Config { + enable: false +} +function testEquilateralTriangleSidesMayBeFloats() { + float[] sides = [0.5, 0.5, 0.5]; + test:assertTrue(kind(sides, EQUILATERAL)); +} + +@test:Config { + enable: false +} +function testIsoscelesTriangleLastTwoSidesAreEqual() { + int[] sides = [3, 4, 4]; + test:assertTrue(kind(sides, ISOSCELES)); +} + +@test:Config { + enable: false +} +function testIsoscelesTriangleFirstTwoSidesAreEqual() { + int[] sides = [4, 4, 3]; + test:assertTrue(kind(sides, ISOSCELES)); +} + +@test:Config { + enable: false +} +function testIsoscelesTriangleFirstAndLastSidesAreEqual() { + int[] sides = [4, 3, 4]; + test:assertTrue(kind(sides, ISOSCELES)); +} + +@test:Config { + enable: false +} +function testIsoscelesTriangleEquilateralTrianglesAreAlsoIsosceles() { + int[] sides = [4, 4, 4]; + test:assertTrue(kind(sides, ISOSCELES)); +} + +@test:Config { + enable: false +} +function testIsoscelesTriangleNoSidesAreEqual() { + int[] sides = [2, 3, 4]; + test:assertFalse(kind(sides, ISOSCELES)); +} + +@test:Config { + enable: false +} +function testIsoscelesTriangleFirstTriangleInequalityViolation() { + int[] sides = [1, 1, 3]; + test:assertFalse(kind(sides, ISOSCELES)); +} + +@test:Config { + enable: false +} +function testIsoscelesTriangleSecondTriangleInequalityViolation() { + int[] sides = [1, 3, 1]; + test:assertFalse(kind(sides, ISOSCELES)); +} + +@test:Config { + enable: false +} +function testIsoscelesTriangleThirdTriangleInequalityViolation() { + int[] sides = [3, 1, 1]; + test:assertFalse(kind(sides, ISOSCELES)); +} + +@test:Config { + enable: false +} +function testIsoscelesTriangleSidesMayBeFloats() { + float[] sides = [0.5, 0.4, 0.5]; + test:assertTrue(kind(sides, ISOSCELES)); +} + +@test:Config { + enable: false +} +function testScaleneTriangleNoSidesAreEqual() { + int[] sides = [5, 4, 6]; + test:assertTrue(kind(sides, SCALENE)); +} + +@test:Config { + enable: false +} +function testScaleneTriangleAllSidesAreEqual() { + int[] sides = [4, 4, 4]; + test:assertFalse(kind(sides, SCALENE)); +} + +@test:Config { + enable: false +} +function testScaleneTriangleFirstAndSecondSidesAreEqual() { + int[] sides = [4, 4, 3]; + test:assertFalse(kind(sides, SCALENE)); +} + +@test:Config { + enable: false +} +function testScaleneTriangleFirstAndThirdSidesAreEqual() { + int[] sides = [3, 4, 3]; + test:assertFalse(kind(sides, SCALENE)); +} + +@test:Config { + enable: false +} +function testScaleneTriangleSecondAndThirdSidesAreEqual() { + int[] sides = [4, 3, 3]; + test:assertFalse(kind(sides, SCALENE)); +} + +@test:Config { + enable: false +} +function testScaleneTriangleMayNotViolateTriangleInequality() { + int[] sides = [7, 3, 2]; + test:assertFalse(kind(sides, SCALENE)); +} + +@test:Config { + enable: false +} +function testScaleneTriangleSidesMayBeFloats() { + float[] sides = [0.5, 0.4, 0.6]; + test:assertTrue(kind(sides, SCALENE)); +} \ No newline at end of file diff --git a/exercises/practice/triangle/triangle.bal b/exercises/practice/triangle/triangle.bal new file mode 100644 index 00000000..51d3b34c --- /dev/null +++ b/exercises/practice/triangle/triangle.bal @@ -0,0 +1,12 @@ +enum TriangleType { + // TODO: implement this enum +} + +# Determines the type of triangle based on the sides +# +# + sides - The sides of the triangle +# + t - The type of triangle +# + return - Whether the sides form the triangle type +function kind(float[]|int[] sides, TriangleType t) returns boolean { + // TODO: implement this function +} \ No newline at end of file