Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Number of Sides by Tinus_Trotyl
def number_of_sides(triangles):
from itertools import combinations
c = [None, None] # dim a 2 elements array
singles = []
for triangle in triangles: # exclusive-or all the perculair sides of the triangles
for side in zip(triangle, triangle[1:] + triangle[:1]):
single = tuple(sorted(side))
if single in singles:
singles.remove(single)
else:
singles.append(single)
while True:
for sides in combinations(singles, 2): # combine all continuing single sides
sides = sorted(sides)
if sides[0][1] == sides[1][0]: # are both sides connected ?
for n in (0, 1): # calculate slope coefficients (substitude infinity by None)
c[n] = ((sides[n][1][0] - sides[n][0][0]) / (sides[n][1][1] - sides[n][0][1])) \
if not sides[n][0][1] == sides[n][1][1] else None
if c[0] == c[1]: # when the two sides are continuing: combine and replace
singles.remove(sides[0])
singles.remove(sides[1])
singles.append((sides[0][0],sides[1][1]))
break
else:
break
return len(singles)
April 18, 2024
Comments: