Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Compare (scaled) lengths solution in Clear category for Similar Triangles by caldeius
from typing import List, Tuple
Coords = List[Tuple[int, int]]
def length(P1, P2):
return float(P1[0]-P2[0])**2 + (P1[1]-P2[1])**2
def similar_triangles(coords_1: Coords, coords_2: Coords) -> bool:
if len(coords_1) != len(coords_2):
return False
sides = len(coords_1)
lengths1 = sorted([length(coords_1[x], coords_1[y]) for x in range (sides-1) for y in range(x+1, sides)])
lengths2 = sorted([length(coords_2[x], coords_2[y]) for x in range (sides-1) for y in range(x+1, sides)])
scale= lengths2[0] / lengths1[0]
scaled_l1 = [l * scale for l in lengths1]
# your code here
return scaled_l1 == lengths2
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. 17, 2020
Comments: