Skip to content

Commit

Permalink
Add triangle exercise
Browse files Browse the repository at this point in the history
  • Loading branch information
keiravillekode committed Nov 1, 2024
1 parent eb7ae48 commit c2d929b
Show file tree
Hide file tree
Showing 12 changed files with 3,694 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,14 @@
"prerequisites": [],
"difficulty": 3
},
{
"slug": "triangle",
"name": "Triangle",
"uuid": "30e46a93-4c84-4b5f-8006-ddde0f155b09",
"practices": [],
"prerequisites": [],
"difficulty": 3
},
{
"slug": "atbash-cipher",
"name": "Atbash Cipher",
Expand Down
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
19 changes: 19 additions & 0 deletions exercises/practice/triangle/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"keiravillekode"
],
"files": {
"solution": [
"triangle.s"
],
"test": [
"triangle_test.c"
],
"example": [
".meta/example.s"
]
},
"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"
}
65 changes: 65 additions & 0 deletions exercises/practice/triangle/.meta/example.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
.text
.globl equilateral
.globl isosceles
.globl scalene

/* extern int equilateral(int a, int b, int c); */
equilateral:
cbz x0, reject

cmp x0, x1
bne reject

cmp x0, x2
bne reject

accept:
mov x0, #1
ret

/* extern int isosceles(int a, int b, int c); */
isosceles:
cmp x0, x1
beq triangle

cmp x0, x2
beq triangle

cmp x1, x2
beq triangle

reject:
mov x0, xzr
ret

/* extern int scalene(int a, int b, int c); */
scalene:
cmp x0, x1
beq reject

cmp x0, x2
beq reject

cmp x1, x2
beq reject

triangle:
cbz x0, reject

cbz x1, reject

cbz x2, reject

add x3, x1, x2
cmp x0, x3
bgt reject

add x3, x0, x2
cmp x1, x3
bgt reject

add x3, x0, x1
cmp x2, x3
bgt reject

b accept
76 changes: 76 additions & 0 deletions exercises/practice/triangle/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# 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"
include = false

[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"
include = false

[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"
include = false
36 changes: 36 additions & 0 deletions exercises/practice/triangle/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
AS = aarch64-linux-gnu-as
CC = aarch64-linux-gnu-gcc

CFLAGS = -g -Wall -Wextra -pedantic -Werror
LDFLAGS =

ALL_LDFLAGS = -pie -Wl,--fatal-warnings

ALL_CFLAGS = -std=c99 -fPIE $(CFLAGS)
ALL_LDFLAGS += $(LDFLAGS)

C_OBJS = $(patsubst %.c,%.o,$(wildcard *.c))
AS_OBJS = $(patsubst %.s,%.o,$(wildcard *.s))
ALL_OBJS = $(filter-out example.o,$(C_OBJS) $(AS_OBJS) vendor/unity.o)

CC_CMD = $(CC) $(ALL_CFLAGS) -c -o $@ $<

all: tests
qemu-aarch64 -L /usr/aarch64-linux-gnu ./$<

tests: $(ALL_OBJS)
@$(CC) $(ALL_CFLAGS) $(ALL_LDFLAGS) -o $@ $(ALL_OBJS)

%.o: %.s
@$(AS) -o $@ $<

%.o: %.c
@$(CC_CMD)

vendor/unity.o: vendor/unity.c vendor/unity.h vendor/unity_internals.h
@$(CC_CMD)

clean:
@rm -f *.o vendor/*.o tests

.PHONY: all clean
13 changes: 13 additions & 0 deletions exercises/practice/triangle/triangle.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
.text
.globl equilateral
.globl isosceles
.globl scalene

equilateral:
ret

isosceles:
ret

scalene:
ret
123 changes: 123 additions & 0 deletions exercises/practice/triangle/triangle_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
#include "vendor/unity.h"

extern int equilateral(int a, int b, int c);
extern int isosceles(int a, int b, int c);
extern int scalene(int a, int b, int c);

void setUp(void) {
}

void tearDown(void) {
}

void test_equilateral_all_sides_are_equal(void) {
TEST_ASSERT_TRUE(equilateral(2, 2, 2));
}

void test_equilateral_any_side_is_unequal(void) {
TEST_IGNORE();
TEST_ASSERT_FALSE(equilateral(2, 3, 2));
}

void test_equilateral_no_sides_are_equal(void) {
TEST_IGNORE();
TEST_ASSERT_FALSE(equilateral(5, 4, 6));
}

void test_equilateral_all_zero_sides_is_not_a_triangle(void) {
TEST_IGNORE();
TEST_ASSERT_FALSE(equilateral(0, 0, 0));
}

void test_isosceles_last_two_sides_are_equal(void) {
TEST_IGNORE();
TEST_ASSERT_TRUE(isosceles(3, 4, 4));
}

void test_isosceles_first_two_sides_are_equal(void) {
TEST_IGNORE();
TEST_ASSERT_TRUE(isosceles(4, 4, 3));
}

void test_isosceles_first_and_last_sides_are_equal(void) {
TEST_IGNORE();
TEST_ASSERT_TRUE(isosceles(4, 3, 4));
}

void test_isosceles_equilateral_triangles_are_also_isosceles(void) {
TEST_IGNORE();
TEST_ASSERT_TRUE(isosceles(4, 4, 4));
}

void test_isosceles_no_sides_are_equal(void) {
TEST_IGNORE();
TEST_ASSERT_FALSE(isosceles(2, 3, 4));
}

void test_isosceles_first_triangle_inequality_violation(void) {
TEST_IGNORE();
TEST_ASSERT_FALSE(isosceles(1, 1, 3));
}

void test_isosceles_second_triangle_inequality_violation(void) {
TEST_IGNORE();
TEST_ASSERT_FALSE(isosceles(1, 3, 1));
}

void test_isosceles_third_triangle_inequality_violation(void) {
TEST_IGNORE();
TEST_ASSERT_FALSE(isosceles(3, 1, 1));
}

void test_scalene_no_sides_are_equal(void) {
TEST_IGNORE();
TEST_ASSERT_TRUE(scalene(5, 4, 6));
}

void test_scalene_all_sides_are_equal(void) {
TEST_IGNORE();
TEST_ASSERT_FALSE(scalene(4, 4, 4));
}

void test_scalene_first_and_second_sides_are_equal(void) {
TEST_IGNORE();
TEST_ASSERT_FALSE(scalene(4, 4, 3));
}

void test_scalene_first_and_third_sides_are_equal(void) {
TEST_IGNORE();
TEST_ASSERT_FALSE(scalene(3, 4, 3));
}

void test_scalene_second_and_third_sides_are_equal(void) {
TEST_IGNORE();
TEST_ASSERT_FALSE(scalene(4, 3, 3));
}

void test_scalene_may_not_violate_triangle_inequality(void) {
TEST_IGNORE();
TEST_ASSERT_FALSE(scalene(7, 3, 2));
}

int main(void) {
UNITY_BEGIN();
RUN_TEST(test_equilateral_all_sides_are_equal);
RUN_TEST(test_equilateral_any_side_is_unequal);
RUN_TEST(test_equilateral_no_sides_are_equal);
RUN_TEST(test_equilateral_all_zero_sides_is_not_a_triangle);
RUN_TEST(test_isosceles_last_two_sides_are_equal);
RUN_TEST(test_isosceles_first_two_sides_are_equal);
RUN_TEST(test_isosceles_first_and_last_sides_are_equal);
RUN_TEST(test_isosceles_equilateral_triangles_are_also_isosceles);
RUN_TEST(test_isosceles_no_sides_are_equal);
RUN_TEST(test_isosceles_first_triangle_inequality_violation);
RUN_TEST(test_isosceles_second_triangle_inequality_violation);
RUN_TEST(test_isosceles_third_triangle_inequality_violation);
RUN_TEST(test_scalene_no_sides_are_equal);
RUN_TEST(test_scalene_all_sides_are_equal);
RUN_TEST(test_scalene_first_and_second_sides_are_equal);
RUN_TEST(test_scalene_first_and_third_sides_are_equal);
RUN_TEST(test_scalene_second_and_third_sides_are_equal);
RUN_TEST(test_scalene_may_not_violate_triangle_inequality);
return UNITY_END();
}
Loading

0 comments on commit c2d929b

Please sign in to comment.