Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Similar Triangles by dig
from typing import List, Tuple
import math
Coords = List[Tuple[int, int]]
def longitud_costa_quadrat(coord1, coord2):
return math.sqrt((coord2[0]-coord1[0])**2+(coord2[1]-coord1[1])**2)
def similar_triangles(coords_1: Coords, coords_2: Coords) -> bool:
costat1=longitud_costa_quadrat(coords_1[0], coords_1[1])
costat2=longitud_costa_quadrat(coords_1[1], coords_1[2])
costat3=longitud_costa_quadrat(coords_1[0], coords_1[2])
costats=[costat1,costat2,costat3]
#triangle 2
costat4=longitud_costa_quadrat(coords_2[0], coords_2[1])
costat5=longitud_costa_quadrat(coords_2[1], coords_2[2])
costat6=longitud_costa_quadrat(coords_2[0], coords_2[2])
costats2=[costat4,costat5,costat6]
costats.sort()
costats2.sort()
if costats[0]/costats2[0] == costats[1]/costats2[1] and costats[0]/costats2[0]==costats[2]/costats2[2] and costats[1]/costats2[1]==costats[2]/costats2[2]:
return True
else:
return False
if __name__ == '__main__':
print("Example:")
print(similar_triangles([(0, 0), (1, 2), (2, 0)], [(3, 0), (4, 2), (5, 0)]))
# 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!")
Feb. 28, 2023