From b46ed6f28eeb2d59400c2da3a77902e6c523feca Mon Sep 17 00:00:00 2001 From: Divlo Date: Thu, 30 Sep 2021 18:16:39 +0200 Subject: [PATCH] feat(solutions): add `triangle-type/c/function` --- .../solutions/c/function/README.md | 3 ++ .../solutions/c/function/solution.c | 27 ++++++++++ .../solutions/typescript/function/README.md | 3 -- .../solutions/typescript/function/solution.ts | 54 ------------------- 4 files changed, 30 insertions(+), 57 deletions(-) create mode 100644 challenges/triangle-type/solutions/c/function/README.md create mode 100644 challenges/triangle-type/solutions/c/function/solution.c delete mode 100644 challenges/triangle-type/solutions/typescript/function/README.md delete mode 100644 challenges/triangle-type/solutions/typescript/function/solution.ts diff --git a/challenges/triangle-type/solutions/c/function/README.md b/challenges/triangle-type/solutions/c/function/README.md new file mode 100644 index 0000000..f0f67c9 --- /dev/null +++ b/challenges/triangle-type/solutions/c/function/README.md @@ -0,0 +1,3 @@ +# triangle-type/c/function + +Created by [@Divlo](https://github.com/Divlo) on 30 September 2021. diff --git a/challenges/triangle-type/solutions/c/function/solution.c b/challenges/triangle-type/solutions/c/function/solution.c new file mode 100644 index 0000000..5a784c0 --- /dev/null +++ b/challenges/triangle-type/solutions/c/function/solution.c @@ -0,0 +1,27 @@ +#include +#include + +char* triangle_type(int triangle_sides[3]) { + if ((triangle_sides[0] + triangle_sides[1] < triangle_sides[2]) || (triangle_sides[2] + triangle_sides[0] < triangle_sides[1]) || (triangle_sides[2] + triangle_sides[1] < triangle_sides[0])) { + return "impossible"; + } + + if (triangle_sides[0] == triangle_sides[1] && triangle_sides[1] == triangle_sides[2]) { + return "equilateral"; + } + + if (triangle_sides[0] == triangle_sides[1] || triangle_sides[1] == triangle_sides[2] || triangle_sides[2] == triangle_sides[0]) { + return "isosceles"; + } + + return "scalene"; +} + +int main() { + int triangle_sides[3] = {}; + for (size_t index = 0; index < 3; index++) { + scanf("%d", &triangle_sides[index]); + } + printf("%s\n", triangle_type(triangle_sides)); + return EXIT_SUCCESS; +} diff --git a/challenges/triangle-type/solutions/typescript/function/README.md b/challenges/triangle-type/solutions/typescript/function/README.md deleted file mode 100644 index 05085c5..0000000 --- a/challenges/triangle-type/solutions/typescript/function/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# triangle-type/typescript/function - -Created by [@Divlo](https://github.com/Divlo) on 30 June 2021. diff --git a/challenges/triangle-type/solutions/typescript/function/solution.ts b/challenges/triangle-type/solutions/typescript/function/solution.ts deleted file mode 100644 index 4b980b6..0000000 --- a/challenges/triangle-type/solutions/typescript/function/solution.ts +++ /dev/null @@ -1,54 +0,0 @@ -import readline from 'node:readline' - -const removeByIndex = (array: T[], index: number): T[] => { - const result = [...array] - result.splice(index, 1) - return result -} - -type TriangleType = 'impossible' | 'equilateral' | 'isosceles' | 'scalene' - -class Triangle { - public sides: number[] - public type: TriangleType - - constructor(sides: number[]) { - this.sides = sides - this.type = this.getType() - } - - private getType(): TriangleType { - const largestSide = Math.max(...this.sides) - const largestSideIndex = this.sides.findIndex((side) => side === largestSide) - const otherSides = removeByIndex(this.sides, largestSideIndex) - const isImpossible = otherSides[0] + otherSides[1] < largestSide - if (isImpossible) { - return 'impossible' - } - const uniqueSides = [...new Set(this.sides)] - const isEquilateral = uniqueSides.length === 1 - const isIsocseles = uniqueSides.length === 2 - if (isEquilateral) { - return 'equilateral' - } - if (isIsocseles) { - return 'isosceles' - } - return 'scalene' - } -} - -const triangleSides: number[] = [] -const readlineInterface = readline.createInterface({ - input: process.stdin, - output: process.stdout -}) -readlineInterface.on('line', (value) => { - triangleSides.push(Number(value)) -}) -readlineInterface.on('close', solution) - -function solution(): void { - const triangle = new Triangle(triangleSides) - console.log(triangle.type) -}