DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 966,155 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
dev.to staff
dev.to staff

Posted on

Daily Challenge #84 - Third Angle of a Triangle

You are given two angles of a triangle in degrees.

Write a function to return the third angle. Only positive integers will be tested.

Sample Tests:

thirdAngle(30, 60)
thirdAngle(60, 60)
thirdAngle(43, 78)
thirdAngle(10, 20)


This challenge comes from user5651159. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!

Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!

Top comments (15)

Collapse
 
konaarctic profile image
Kona Arctic • Edited on

ANSI C

#include <math.h>

double third_angle ( double first_angle , double second_angle ) {
    first_angle = fmod( first_angle , 360.0 );  // Get absolute
    second_angle = fmod( second_angle , 360.0 );

    if ( first_angle + second_angle >= 180 )
        return 0.0;  // That's not a triangle! OwO

    return ( 180 - first_angle - second_angle );
}

int main ( int argc , char ** argv ) {
    if ( 
        ( int ) third_angle( 30 , 60 ) == 90 &&
        ( int ) third_angle( 60 , 60 ) == 60 &&
        ( int ) third_angle( 43 , 78 ) == 59 &&
        ( int ) third_angle( 10 , 20 ) == 150 )
            return 0;  // It works!

    return 101;  // Or not
}

Run with ~$ gcc -lm <file_name> && ./a.out

Collapse
 
kvharish profile image
K.V.Harish • Edited on

My solution in js

const thirdAngle = (angle1, angle2) => (angle1 && angle2 && (angle3 = 180 - (angle1 + angle2)) > 0) ? angle3 : 'Invalid - Sum of all angles of a triangle should be 180 degrees';
Collapse
 
thepeoplesbourgeois profile image
Josh

What happens if I enter thirdAngle(0,0)?

Collapse
 
kvharish profile image
K.V.Harish • Edited on

Good catch. Edited. But the challenge is to deal with positive integers so didn't take 0 into account as it is neither positive nor negative integer.

Thread Thread
 
thepeoplesbourgeois profile image
Josh

True enough. But why code purely for specs when coding for the edge and error cases takes just a few more minutes?

Collapse
 
pmkroeker profile image
Peter

In Go!

func thirdAngle(a, b int) (int, error) {
    if (a + b) >= 180 {
        return 0, errors.New("Input angles sum is greater than or equal to 180 degress, this cannot be a triangle")
    }
    if a == 0 || b == 0 {
        return 0, errors.New("Input angle cannot be zero, cannot be a triangle")
    }
    angle := 360 - a - b // sum of angles must be 360 degress
    return angle, nil
}

Go Playground

Collapse
 
mbaas2 profile image
Michael Baas • Edited on

Solution in APL

(using Dyalog APL)

thirdAngle←{r←180-+/⍡ β‹„ (r<0)∨0∊⍡: 'This is not a triangle!' β‹„ r}

Tests:

      thirdAngle 30 60
90
      thirdAngle 60 60
60
      thirdAngle 43 78
59
      thirdAngle 10 20
150

Try it online!

Collapse
 
thepeoplesbourgeois profile image
Josh

This oughta do it for Elixir.

defmodule Triangle do
  def third_angle(a, b) when is_integer(a + b) # will not qualify if either isn't an integer
                         and a + b < 180 
                         and a > 0
                         and b > 0 do
    180 - a - b
  end
end
Collapse
 
willsmart profile image
willsmart

Keeping it simple in bash

function thirdAngle {
  echo $((180 - $1 - $2))
}

Running through the examples on the cmd:

> thirdAngle 30 60
90

> thirdAngle 60 60
60

> thirdAngle 43 78
59

> thirdAngle 10 20
150
Collapse
 
willsmart profile image
willsmart

BTW the challenge on Kata only tests for valid triangles,

function otherAngle(a, b) {
  return 180-(a+b);
}

was sufficient in JS

Collapse
 
aminnairi profile image
Amin

Elm

import List exposing (all, sum)
import Maybe exposing (andThen, map)


isAngle : Int -> Bool
isAngle angle =
    angle > 0 && angle < 180


maybeAngles : List Int -> Maybe (List Int)
maybeAngles angles =
    if all isAngle angles && sum angles < 180 then
        Just angles

    else
        Nothing


third : Int -> Int -> Maybe Int
third first second =
    maybeAngles [ first, second ]
        |> map (sum >> (-) 180)

Test it online

Here.

Collapse
 
delixx profile image
DeLiXx • Edited on
function thirdAngle(a,b) {
    return 180-a-b;
}
Collapse
 
lormayna profile image
lormayna • Edited on
Collapse
 
thepeoplesbourgeois profile image
Josh • Edited on

What happens if I enter thirdAngle(0,0)?

Collapse
 
lormayna profile image
lormayna

you are right, I just wrote it very fast and I did not think about this case.
TDD will help a lot in these situations.

DEV has this feature:

Settings

Go to your customization settings to nudge your home feed to show content more relevant to your developer experience level. πŸ›