Exercism - Triangle
This post shows you how to get Triangle exercise of Exercism.
Preparation
Before we click on our next exercise, let’s see what concepts of DART we need to consider

So we need to use the following concepts.
Helper Methods
Helper methods (often private methods starting with _) encapsulate reusable logic, making code more organized and maintainable.
class Triangle {
// Public method
bool equilateral(num a, num b, num c) {
return _isValid(a, b, c) && a == b && b == c;
}
// Private helper method
bool _isValid(num a, num b, num c) {
// Validation logic
return a > 0 && b > 0 && c > 0;
}
}
Comparison Operators
Comparison operators (==, !=, >=, >) compare values and return boolean results. They’re essential for checking triangle properties.
void main() {
num a = 3, b = 3, c = 3;
// Equality checks
print(a == b); // true
print(a == c); // true
print(b == c); // true
// Inequality checks
print(a != b); // false
print(a != c); // false
// Greater than or equal
print(a + b >= c); // true (6 >= 3)
print(a > 0); // true
}
Logical Operators
Logical operators (&&, ||) combine boolean expressions. && (AND) returns true only if both are true, || (OR) returns true if either is true.
void main() {
num a = 3, b = 3, c = 4;
// AND operator: both must be true
bool allEqual = (a == b) && (b == c);
print(allEqual); // false (a == b is true, but b == c is false)
// OR operator: at least one must be true
bool twoEqual = (a == b) || (b == c) || (a == c);
print(twoEqual); // true (a == b is true)
// Combined with validation
bool valid = (a > 0) && (b > 0) && (c > 0);
print(valid); // true
}
Num Type
The num type is the supertype of both int and double. It’s useful when you need to accept either integer or floating-point values.
void main() {
// Function that accepts num (int or double)
bool checkTriangle(num a, num b, num c) {
return a > 0 && b > 0 && c > 0;
}
// Works with int
checkTriangle(3, 4, 5); // true
// Works with double
checkTriangle(3.5, 4.2, 5.1); // true
}
Expression Bodied Methods
Expression bodied methods use the => syntax to provide a concise way to write methods that return a single expression.
class Triangle {
// Regular method
bool isValid(num a, num b, num c) {
return a > 0 && b > 0 && c > 0;
}
// Expression bodied method (shorter)
bool isValidShort(num a, num b, num c) =>
a > 0 && b > 0 && c > 0;
// Both do the same thing
}
Triangle Inequality
The triangle inequality states that for any triangle with sides a, b, and c, the sum of any two sides must be greater than or equal to the third side.
void main() {
num a = 3, b = 4, c = 5;
// Check triangle inequality
bool valid1 = a + b >= c; // 3 + 4 >= 5 → 7 >= 5 → true
bool valid2 = b + c >= a; // 4 + 5 >= 3 → 9 >= 3 → true
bool valid3 = a + c >= b; // 3 + 5 >= 4 → 8 >= 4 → true
// All must be true
bool isValid = valid1 && valid2 && valid3;
print(isValid); // true
// Invalid triangle
num a2 = 1, b2 = 1, c2 = 3;
bool invalid = (a2 + b2 >= c2) && (b2 + c2 >= a2) && (a2 + c2 >= b2);
print(invalid); // false (1 + 1 = 2 < 3)
}
Introduction
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.
Note
Degenerate triangles are triangles where the sum of the length of two sides is equal to the length of the third side, e.g. 1, 1, 2. We opted to not include tests for degenerate triangles in this exercise. You may handle those situations if you wish to do so, or safely ignore them.
Triangle Inequality
In equations:
Let a, b, and c be sides of the triangle. Then all three of the following expressions must be true:
- a + b ≥ c
- b + c ≥ a
- a + c ≥ b
What are triangle types?
Triangles can be classified by their side lengths:
- Equilateral: All three sides are equal (e.g., 3, 3, 3)
- Isosceles: At least two sides are equal (e.g., 3, 3, 4 or 2, 2, 2)
- Scalene: All three sides are different (e.g., 3, 4, 5)
— Geometry
How can we classify triangles?
To classify triangles:
- Validate triangle: Check that all sides > 0 and triangle inequality holds
- Check equilateral: All three sides are equal (a == b && b == c)
- Check isosceles: At least two sides are equal (a == b || b == c || a == c)
- Check scalene: All sides are different (a != b && b != c && a != c)
The key insight is using a helper method to validate the triangle first, then checking side relationships. Note that an equilateral triangle is also isosceles (has at least two equal sides), but we check equilateral first.
For example, with sides (3, 3, 3):
- Valid: all > 0, triangle inequality holds ✓
- Equilateral: 3 == 3 && 3 == 3 → true ✓
With sides (3, 3, 4):
- Valid: all > 0, triangle inequality holds ✓
- Equilateral: 3 == 3 && 3 == 4 → false
- Isosceles: 3 == 3 || 3 == 4 || 3 == 4 → true ✓
With sides (3, 4, 5):
- Valid: all > 0, triangle inequality holds ✓
- Equilateral: false
- Isosceles: false
- Scalene: 3 != 4 && 4 != 5 && 3 != 5 → true ✓
Solution
class Triangle {
bool equilateral(num a, num b, num c) {
return _isValid(a, b, c) && a == b && b == c;
}
bool isosceles(num a, num b, num c) {
return _isValid(a, b, c) && (a == b || b == c || a == c);
}
bool scalene(num a, num b, num c) {
return _isValid(a, b, c) && a != b && b != c && a != c;
}
bool _isValid(num a, num b, num c) =>
a > 0 && b > 0 && c > 0 &&
a + b >= c && b + c >= a && a + c >= b;
}
Let’s break down the solution:
-
bool _isValid(num a, num b, num c)- Helper method that validates a triangle:- Uses expression-bodied method syntax (
=>) a > 0 && b > 0 && c > 0: Checks all sides are positive- A triangle cannot have zero or negative side lengths
a + b >= c: First triangle inequality check- Sum of first two sides must be >= third side
b + c >= a: Second triangle inequality check- Sum of second and third sides must be >= first side
a + c >= b: Third triangle inequality check- Sum of first and third sides must be >= second side
- Returns
trueonly if all conditions are met
- Uses expression-bodied method syntax (
-
bool equilateral(num a, num b, num c)- Checks if triangle is equilateral:_isValid(a, b, c): First validates it’s a valid trianglea == b && b == c: Checks all three sides are equal- Uses AND operator: all three comparisons must be true
- Returns
trueonly if valid AND all sides equal
-
bool isosceles(num a, num b, num c)- Checks if triangle is isosceles:_isValid(a, b, c): First validates it’s a valid triangle(a == b || b == c || a == c): Checks at least two sides are equal- Uses OR operator: at least one pair must be equal
- Checks all three possible pairs: (a,b), (b,c), (a,c)
- Returns
trueif valid AND at least two sides equal - Note: Equilateral triangles also return true (they have at least two equal sides)
-
bool scalene(num a, num b, num c)- Checks if triangle is scalene:_isValid(a, b, c): First validates it’s a valid trianglea != b && b != c && a != c: Checks all sides are different- Uses AND operator: all three comparisons must be true
- No two sides can be equal
- Returns
trueonly if valid AND all sides different
The solution efficiently classifies triangles by first validating them with a helper method, then checking the specific side relationships. The helper method ensures all triangles meet the basic requirements before classification.
A video tutorial for this exercise is coming soon! In the meantime, check out my YouTube channel for more Dart and Flutter tutorials. 😉
Visit My YouTube Channel