mirror of
https://github.com/theoludwig/programming-challenges.git
synced 2024-12-08 00:45:29 +01:00
feat(solutions): add triangle-type/c/function
This commit is contained in:
parent
122e62aee5
commit
b46ed6f28e
3
challenges/triangle-type/solutions/c/function/README.md
Normal file
3
challenges/triangle-type/solutions/c/function/README.md
Normal file
@ -0,0 +1,3 @@
|
||||
# triangle-type/c/function
|
||||
|
||||
Created by [@Divlo](https://github.com/Divlo) on 30 September 2021.
|
27
challenges/triangle-type/solutions/c/function/solution.c
Normal file
27
challenges/triangle-type/solutions/c/function/solution.c
Normal file
@ -0,0 +1,27 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
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;
|
||||
}
|
@ -1,3 +0,0 @@
|
||||
# triangle-type/typescript/function
|
||||
|
||||
Created by [@Divlo](https://github.com/Divlo) on 30 June 2021.
|
@ -1,54 +0,0 @@
|
||||
import readline from 'node:readline'
|
||||
|
||||
const removeByIndex = <T = unknown>(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)
|
||||
}
|
Loading…
Reference in New Issue
Block a user