Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
With lambda solution in Clear category for Similar Triangles by PistoleroAlan
from typing import List, Tuple
from itertools import combinations
Coords = List[Tuple[int, int]]
distance=lambda c:list((x[1][0]-x[0][0])**2+(x[1][1]-x[0][1])**2 for x in list(combinations(c, 2)))
rapport=lambda d1,d2:list(round(d2[i]/d1[i],2) for i in range(3))
def similar_triangles(coords_1: Coords, coords_2: Coords) -> bool:
distance1,distance2=[],[]
distance1=distance(coords_1)
distance1.sort()
distance2=distance(coords_2)
distance2.sort()
return rapport(distance1,distance2)[:-1]==rapport(distance1,distance2)[1:]
if __name__ == '__main__':
print("Example:")
print(similar_triangles([[0,2],[-2,2],[3,4]],[[2,3],[-7,-3],[8,3]]))
# These "asserts" are used for self-checking and not for an auto-testing
assert similar_triangles([(0, 0), (1, 2), (2, 0)], [(3, 0), (4, 2), (5, 0)]) is True, 'basic'
assert similar_triangles([(0, 0), (1, 2), (2, 0)], [(3, 0), (4, 3), (5, 0)]) is False, 'different #1'
assert similar_triangles([(0, 0), (1, 2), (2, 0)], [(2, 0), (4, 4), (6, 0)]) is True, 'scaling'
assert similar_triangles([(0, 0), (0, 3), (2, 0)], [(3, 0), (5, 3), (5, 0)]) is True, 'reflection'
assert similar_triangles([(1, 0), (1, 2), (2, 0)], [(3, 0), (5, 4), (5, 0)]) is True, 'scaling and reflection'
assert similar_triangles([(1, 0), (1, 3), (2, 0)], [(3, 0), (5, 5), (5, 0)]) is False, 'different #2'
print("Coding complete? Click 'Check' to earn cool rewards!")
Aug. 25, 2023
Comments: