Skip to content

Commit

Permalink
Add triangle (#222)
Browse files Browse the repository at this point in the history
  • Loading branch information
BNAndras authored Apr 16, 2024
1 parent a32c329 commit e219f5d
Show file tree
Hide file tree
Showing 13 changed files with 272 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,14 @@
"prerequisites": [],
"difficulty": 2
},
{
"slug": "triangle",
"name": "Triangle",
"uuid": "82577254-d303-4996-9596-ca88428e6a9a",
"practices": [],
"prerequisites": [],
"difficulty": 2
},
{
"slug": "binary-search",
"name": "Binary Search",
Expand Down
3 changes: 3 additions & 0 deletions exercises/practice/triangle/.docs/instructions.append.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Instructions append

For this exercise, scalene triangles are instead defined as those where only two sides are equal. Equilateral and isosceles triangles are still defined as above.
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
11 changes: 11 additions & 0 deletions exercises/practice/triangle/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
## -*- conf -*-
.rebar3
_build/
ebin/
erl_crash.dump
rebar3.crashdump

tmp
bin/configlet
bin/configlet.exe
CHECKLIST
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": [
"BNAndras"
],
"files": {
"solution": [
"src/triangle.lfe"
],
"test": [
"test/triangle-tests.lfe"
],
"example": [
".meta/example.lfe"
]
},
"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"
}
22 changes: 22 additions & 0 deletions exercises/practice/triangle/.meta/example.lfe
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
(defmodule triangle
(export (kind 3)))

(defun kind
((A B C) (when (and (=< A 0)
(=< B 0)
(=< C 0)))
'false)
((A B C) (when (or (>= C (+ A B))
(>= B (+ A C))
(>= A (+ B C))))
'false)
((A A A)
'equilateral)
((A A _)
'isosceles)
((A _ A)
'isosceles)
((_ A A)
'isosceles)
((_ _ _)
'scalene))
81 changes: 81 additions & 0 deletions exercises/practice/triangle/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# 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"
include = false

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

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

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

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

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

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

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

[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"
21 changes: 21 additions & 0 deletions exercises/practice/triangle/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
ERL := $(shell which erl)
REBAR3 := $(shell which rebar3)

null :=
space := $(null) #
comma := ,

ifeq ($(ERL),)
$(error Can't find Erlang executable 'erl')
else ifeq ($(REBAR3),)
$(error Can't find rebar3)
endif

compile: ; $(REBAR3) compile

clean: ; $(REBAR3) clean

.PHONY: test
test:
$(REBAR3) eunit \
-m $(subst $(space),$(comma),$(basename $(notdir $(wildcard test/*.lfe))))
11 changes: 11 additions & 0 deletions exercises/practice/triangle/rebar.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{plugins, [{rebar3_lfe, "0.4.3"}]}.

{provider_hooks, [{post, [{compile, {lfe, compile}}]}]}.

{deps, [{lfe, "2.1.1"}]}.

{profiles,
[{test,
[{eunit_compile_opts, [{src_dirs, ["src", "test"]}]},
{deps,
[{ltest, "0.13.3"}]}]}]}.
8 changes: 8 additions & 0 deletions exercises/practice/triangle/rebar.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{"1.2.0",
[{<<"lfe">>,{pkg,<<"lfe">>,<<"2.1.1">>},0}]}.
[
{pkg_hash,[
{<<"lfe">>, <<"4A888B26172D198DC7A5AFEB897E8248AF7D56E1638D9C8249AAF933AE811B96">>}]},
{pkg_hash_ext,[
{<<"lfe">>, <<"C484D3B655D40DED58BC41B17B22F173711C681BF36063A234A9BAA9506947E1">>}]}
].
11 changes: 11 additions & 0 deletions exercises/practice/triangle/src/triangle.app.src
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
%% -*- erlang -*-
{application, 'triangle',
[{description, ""},
{vsn, "0.0.1"},
{modules,
['triangle']},
{registered, []},
{applications,
[kernel, stdlib]},
{included_applications, []},
{env, []}]}.
4 changes: 4 additions & 0 deletions exercises/practice/triangle/src/triangle.lfe
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
(defmodule triangle
(export (kind 3)))

; Please implement the kind function.
44 changes: 44 additions & 0 deletions exercises/practice/triangle/test/triangle-tests.lfe
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
(defmodule triangle-tests
(behaviour ltest-unit)
(export all))

(include-lib "ltest/include/ltest-macros.lfe")

(deftest equilateral-triangle-all-sides-are-equal
(is-equal (triangle:kind 2 2 2) 'equilateral))

(deftest equilateral-triangle-all-zero-sides-is-not-a-triangle
(is-not (triangle:kind 0 0 0)))

(deftest equilateral-triangle-sides-may-be-floats
(is-equal (triangle:kind 0.5 0.5 0.5) 'equilateral))

(deftest isosceles-triangle-last-two-sides-are-equal
(is-equal (triangle:kind 3 4 4) 'isosceles))

(deftest isosceles-triangle-first-two-sides-are-equal
(is-equal (triangle:kind 4 4 3) 'isosceles))

(deftest isosceles-triangle-first-and-last-sides-are-equal
(is-equal (triangle:kind 4 3 4) 'isosceles))

(deftest isosceles-first-triangle-inequality
(is-not (triangle:kind 1 2 3)))

(deftest isosceles-second-triangle-inequality
(is-not (triangle:kind 1 3 1)))

(deftest isosceles-third-triangle-inequality
(is-not (triangle:kind 3 1 1)))

(deftest isosceles-may-be-floats
(is-equal (triangle:kind 0.5 0.4 0.5) 'isosceles))

(deftest scalene-no-sides-are-equal
(is-equal (triangle:kind 5 4 6) 'scalene))

(deftest scalene-may-not-violate-triangle-inequality
(is-not (triangle:kind 7 3 2)))

(deftest scalene-may-be-floats
(is-equal (triangle:kind 0.5 0.4 0.6) 'scalene))

0 comments on commit e219f5d

Please sign in to comment.