Wednesday, July 26, 2017

Algorithm: Possible Triangle

July 26, 2017

Introduction


It is a good practice to document personal growth in terms of coding skills. Julia looked up her gmail box and found an email to read back to October 2009. Julia read the code she wrote 8 years ago, and there are so many issues in her code. What she likes to do is to review the code and write down her feelings.

Proverbs 24:16, for the righteous falls seven times and rises again, but the wicked stumble in times of calamity. 

Just be super patient to yourself. The bible teaching always use seven, or seven of seven to allow thing happen. Learn from the past mistake, it is the first step to move forward.

The problem statement is the following:

How to determine if three numbers can be a triangle, which is one with three lines with same length, two of them with same length.


Algorithm practice 



Here is the code written in October 2009, the link is here.

Plan to write the function and give some code review compared to the previous one.

It is a long journey, after 8 years, Julia went back to her gmail box and found this algorithm. She did fail the phone screen with the code; so she reviewed her own code, and wrote a new one. Here it is the new version, it took her 10 minutes to write. Julia will write down some code review as well.

Code review



Code written in October 2009, the link is here.
Code written in July 2017, the link is the new version.
Highlights of code review:

1. Function name should be meaningful. PossibleTriangle function name is better than the name called "test". It should be more than just to check if the numbers can form triangle, afterwards, it should be checked if the three edges are the same length, called equilateral; or two edges are the same length, called isosceles; or none of two is the same, called scalene.

2. edge case, exclude negative values;

3. Line 40, declare an array, line 41, sort the array. We like to make it simple to check any two edges's sum is bigger than the third edge.
We only need to compare two smaller edges's sum to the largest edge.

4. line 44, add comment to explain the checking and the logic of trigonometry.

5. line 51, line 52, declare two bool variables, check the triangle is isosceles only if it is not a equilateral.

6. Add some test cases to make sure that code works, pass unit test cases. C# Code is here with 4 test cases.
Julia learned a few thing after watching the pluralsight.com course: "code for humans". Julia also learned to use Array class to sort the number first, and then use explicit bool variable to define two things.

Actionable Item


Plan to watch another course on pluralsight.com provided by Cory House, which is called "Becoming an Outlier: Reprogramming the developer's mind".

9/11/2017 8:58 pm
Review the blog possible triangle and get some ideas about the algorithm.

6/22/2018
I created a gist based on the email I found related to this phone screen. It is very interesting to read what I wrote back in 2009. Here is the gist.

No comments:

Post a Comment