Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
using ZIP, MAP, lAMBDA, SORTED and ALL Elmaddah idea solution in Clear category for Similar Triangles by info.leadx
from typing import List, Tuple
Coords = List[Tuple[int, int]]
dist = lambda a, b : ((a[0] - b[0]) ** 2 + (a[1] - b[1]) ** 2)**.5
def similar_triangles(a: Coords, b: Coords) -> bool:
dists1 = sorted([ dist(a[i-1], a[i]) for i in range(len(a)) ])
dists2 = sorted([ dist(b[i-1], b[i]) for i in range(len(b)) ])
taux = [a/b for a,b in zip(dists1, dists2)]
return all(map(lambda x: x == taux[0], taux))
if __name__ == '__main__':
print("Example:")
print(similar_triangles([(0, 0), (4, 0), (4, 3)], [(0, 0), (-4, -3), (0, -3)]))
print(similar_triangles([(0, 0), (4, 0), (4, 3)], [(0, 0), (4, 3), (0, 3)]))
print(similar_triangles([(0, 0), (1, 2), (2, 0)], [(3, 0), (4, 2), (5, 0)]))
print(similar_triangles([(0, 0), (1, 2), (2, 0)], [(3, 0), (4, 2), (5, 0)]))
print(similar_triangles([(0, 0), (1, 2), (2, 0)], [(3, 0), (4, 3), (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!")
May 3, 2020