Data scientists or web developers get too busy to learn new technologies, languages, or libraries that sometimes one important side stays ignored. Besides all the tools, It is important to have good programming problem-solving skills also. I decided to provide a series of programming problems and solutions. Today there will be two problems. Bith the problems are taken from Checkio.

#### Similar_Triangles

Given two lists of coordinates of vertices, you need to see if they are similar triangles.

**Input: Two lists of coordinates.**

**Output**: **a bool, True if two triangles are similar and False otherwise. **

An example of an input is ([(0, 0), (1, 2), (2, 0)], [(3, 0), (4, 2), (5, 0)]). The output is True.

#### Solution

Probably, you remember the formula to calculate the length of a triangle if you have two coordinates.

The coordinates of point A in a1 & a2 and the coordinates of point B are b1 & b2. The length of the line AB is:

To solve this problem, we will define a helper function to calculate the arms of the triangle.

Here is the helper function ‘length’:

def length(p1, p2): return ((p1[0] - p2[0])**2 + (p1[1] - p2[1])**2)**0.5

Then we will define the function similar_triangles. In this function, we will make two lists of the lengths of two triangles. When you have the lengths of the two triangles, it is easy to know if they are similar triangles. Here are the similar_triangles:

def similar_triangles(c1, c2): l1 = [] l2 = [] for i in range(0, len(c1)): l1.append(length(c1[i], c1[i-1])) l2.append(length(c2[i], c2[i-1])) return all(l1[i] / l2[i] == l1[i-1] / l2[i-1] for i in range(0, len(l1)))

So, here is the solution to the problem similar_triangles.

#python #programming #python3 #datascience