Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Mountain Scape by imloafer
from typing import List, Tuple
from operator import itemgetter
def mountain_scape(tops: List[Tuple[int, int]]) -> int:
# your code here
area = 0
tops.sort(key=itemgetter(0))
tops_copy = tops[:]
for i in range(len(tops_copy)):
x0, y0 = tops_copy[i]
for j in range(i+1, len(tops_copy)):
x1, y1 = tops_copy[j]
if x1 - y1 <= x0 - y0: # next one contains previous one
try:
tops.remove(tops_copy[i]) # remove previous one
except ValueError:
continue
elif x1 + y1 < x0 + y0: # previous one contains next one
try:
tops.remove(tops_copy[j]) # remove next one
except ValueError:
continue
for i in range(len(tops)-1):
x0, y0 = tops[i]
x1, y1 = tops[i + 1]
a = (x0 + y0 if x1 - y1 >= x0 + y0 else x1- y1) - (x0 - y0) # if x1 - y1 >= x0 + y0 means both triangles don't intersect with each other
area += a ** 2 / 4 + a * (y0 - a/2)
return area + tops[-1][1] ** 2
if __name__ == '__main__':
print("Example:")
# print(mountain_scape([[52,6],[23,5],[19,21],[30,8]]))
print(mountain_scape([[87,11],[44,10],[48,30],[46,18]]))
print(mountain_scape([[24,20],[34,4],[84,2],[82,16],[75,25]]))
# These "asserts" are used for self-checking and not for an auto-testing
assert mountain_scape([[3,3],[5,3],[7,3],[9,3],[11,3],[13,3],[15,3],[17,3],[19,3],[21,3]]) == 54
assert mountain_scape([(0, 2), (5, 3), (7, 5)]) == 29
assert mountain_scape([(1, 3), (5, 3), (5, 5), (8, 4)]) == 37
print("Coding complete? Click 'Check' to earn cool rewards!")
Oct. 28, 2022