Course of Raku / Addendum 🆕 / Flow and functions / Control flow and logic / Exercises / Kind of triangle
Solution: Kind of triangle
Here is a possible solution to the task.
Code
sub kind($a, $b, $c) {
return 'invalid' unless $a + $b > $c && $a + $c > $b && $b + $c > $a;
given ($a, $b, $c).Set.elems {
when 1 { 'equilateral' }
when 2 { 'isosceles' }
default { 'scalene' }
}
}
for (3, 3, 3), (3, 3, 5), (3, 4, 5), (1, 2, 10) -> ($a, $b, $c) {
say "$a $b $c: { kind($a, $b, $c) }";
}🦋 You can find the source code in the file triangle-kind.raku.
Output
3 3 3: equilateral
3 3 5: isosceles
3 4 5: scalene
1 2 10: invalidComments
The
unlessguard rejects side lengths that break the triangle inequality before any classification happens.The number of distinct side lengths tells the kind: one means all equal (equilateral), two means exactly one pair equal (isosceles), three means all different (scalene).
Destructuring the loop variable as
-> ($a, $b, $c)unpacks each inner list straight into three named sides.