Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
dumb and ugly solution in Clear category for Similar Triangles by Vasily__Chibilyaev
from itertools import permutations
from typing import List, Tuple
Coords = List[Tuple[int, int]]
def similar_triangles(coords_1: Coords, coords_2: Coords) -> bool:
f = lambda a, b: ((a[0] - b[0])**2 + (a[1] - b[1])**2)**.5
u1, u2, u3 = coords_1
v1, v2, v3 = coords_2
A = [f(u1, u2), f(u1, u3), f(u2, u3)]
B = [f(v1, v2), f(v1, v3), f(v2, v3)]
for x in permutations(A):
for y in permutations(B):
if x[0] / y[0] == x[1] / y[1] and x[1] / y[1] == x[2] / y[2]:
return True
# your code here
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. 14, 2020
Comments: