This was asked at Microsoft in January of 2008.
Write a function that takes three integers corresponding to the lengths of sides and returns what kind of triangle can be made out of those 3 sides. (Equilateral, etc)
You must also handle the case where no triangle may be formed from those 3 lengths.
18,442 Views |
Keep a count such that count = 2 if two values are the same, and count=3 if all the same.
then count=3 implies equi
=2 - isos
=1 - scalene
def ValidateTraingle(side1, side2, side3)
triangleSides = [side1, side2, side3]
triangleSides = triangleSides.sort
if (triangleSides[2] >= triangleSides[0] + triangleSides[1]) then
# Error in the traingle dimensions
return 4
elsif (triangleSides[0] == triangleSides[1]) and (triangleSides[1] == triangleSides[2]) then
# Equilateral traingle
return 3
elsif (triangleSides[0] == triangleSides[1]) or (triangleSides[1] == triangleSides[2]) or (triangleSides[0] == triangleSides[2]) then
# Isosceles traingle
return 2
elsif (triangleSides[0] != triangleSides[1]) and (triangleSides[1] != triangleSides[2]) then
# Scalene traingle
return 1
end
end
int isTriangle (int x1, int x2, int x3) {
if ((x1 >= x2 + x3) || (x2 >= x1 + x3)
|| (x3 >= x1 + x2)) {
return 0; // no triangle possible
}
if (x1 == x2 == x3) return 1; // Equilateral
if (x1 == x2 || x2 == x3
|| x1 == x3) {
return 2; // Isoceles
}
return 3; //Scalene
}
1) The three integers are a, b and c
2) If not (a+b > c and a+c > b and b+c > a) then input is not valid
3) if a= b =c then return equilateral
4) If ( a= b or a=c or b=c) then return isosceles
5) Arrange variables such that a > b > c and
if sqr(a) = srq(b) + sqr(c) then return right angle triangle
6) Else scalene triangle
enum RectangleType { Scalene, Isosceles, Equilqteral, NotARectangle};
template
struct RectComaparator {
RectComparator(){}
bool compare(const T&lhs, const T& rhs) {
return (lhs > rhs);
}
};
RectangleType
template
detectRectangleType (cosnt T& a, const T& b, const T& c) throws std::runtime_error {
//validate in parameters
if (!a || !b || !c) {
//throw exception
}
//store and sort in array
T store[3]= {a, b, c};
RectComparator comp;
std::sort(store, comp); //in desc order
//store[0] has the greatest value
if (store[0] > store[1] + store [2]) {
return NotARectangle;
}
if ((store[0] == store[1] == store[2])
return Equilateral;
else if (store[0] == store[1] || store[1] == store[2]) return Isosceles;
else
return Scalene;
}
}
alternatively, store the values in a set
Set stores unique sorted values, so if the size of the set is 1 then it is equilateral etc…
I’m basically a php programmer, not much conversant with the microsoft’s scripting technologies/syntaxes. Just gave a try on the logic. Here it follows :
—- —- —- —- —- —- —- —- —-
function typeOfTriangle($x, $y, $z){
$a=array($x,$y,$z);
sort($a);
echo “
Type of triangle that can be drawn with sides with respective lengths as $x, $y, $z : “.($a[2]
“;
}
typeOfTriangle(11,10,17);
typeOfTriangle(11,10,11);
typeOfTriangle(11,11,11);
typeOfTriangle(11,110,11);
typeOfTriangle(11,0,11);
—- —- —- —- —- —- —- —- —-
//output :
Type of triangle that can be drawn with sides with respective lengths as 11, 10, 17 : Scalene
Type of triangle that can be drawn with sides with respective lengths as 11, 10, 11 : Isosceles
Type of triangle that can be drawn with sides with respective lengths as 11, 11, 11 : Equilqteral
Type of triangle that can be drawn with sides with respective lengths as 11, 110, 11 : None
Type of triangle that can be drawn with sides with respective lengths as 11, 0, 11 : None
—- —- —- —- —- —- —- —- —-
[Note : Here assuming following factors : sides’ lengths are numbers (signed/unsigned int/floats) and type of triangle is to be decided on sides and not on angles, in the resultant triangle. ]
regards,
- Manisha
enum TRIANGLE_TYPES
{
TT_NONE = 0×00,
TT_CONFLUENT = 0×01,
TT_COMMON = 0×02,
TT_EQUILATERAL = 0×04,
TT_ISOSCELES = 0×08,
TT_RECTANGULAR = 0×10,
TT_CONFLUENT_ISOSCELES = TT_CONFLUENT | TT_ISOSCELES,
TT_RECTANGULAR_EQUILATERAL = TT_RECTANGULAR | TT_EQUILATERAL,
TT_RECTANGULAR_ISOSCELES = TT_RECTANGULAR | TT_ISOSCELES
};
TRIANGLE_TYPES triangle_kind(int a, int b, int c)
{
TRIANGLE_TYPES type = TT_NONE;
if ( a
Validate(sides[3])
{
sides.sort; //asc order
if(sides[2] > side[0] + side[1])
{
return 0;
}
else
{
if(side[0] == side [2])
{
return equi;
}
elseif(side[0] == side[1])
{
return isoc;
}
return scalene;
}
}
Core logic follws here :
1. A triangle can be drawn only if length of the Longest side is less than the sum of other two sides.
otherwise => None
2. If all the side lengths are distinct => Scalene
3. If only two of them are of equal length => Isosceles
3. If all of them are of same length => Equilqteral
regards,
- Manisha
int arr[3];
sort(arr);
if (arr[0]+arr[1]
I think you should also check for negative inputs.
For example all the functions suggested above will decide on equilateral triangel for an input -1,-1,-1.
The unsigned one will work fine because you have masked the input itself but for complete functionality one should check for negative numbers.
We could give the arguments sizes in such a way which satisfy the a+b >= c, but without angle how can we say it forms a triangle.
panduranga: the way you pose it is ambiquous.
For sure we can say this:
suppose we name the three sides lengths such that:
a
int input[3] (sides of triangle);
sort(input);//by any method
if(input[0]+input[1]==input[2])
//triangle with zero area(st line)
else if(input[0]+input[1]>input[2])
//valid triangle
{
int flag=0;
if(input[0]==input[1])
flag++;
if(input[0]==input[2])
flag++;
switch(flag){
case 0://scalene
break;
case 1:
//isoceles
break;
case 2:
//equilateral
}
}
else
//no valid triangle
Leave an Answer/Comment