Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Rectangles Union by domokdavid
from typing import List, Tuple
def isInside(x: int, y: int, rec: Tuple[int]) -> bool:
left, top, right, bottom = rec
return left <= x < right and top <= y < bottom
def rectangles_union(recs: List[Tuple[int]]) -> int:
if len(recs) == 0:
return 0
left_all = min([x for (x, _, _, _) in recs])
right_all = max([x for (_, _, x, _) in recs])
top_all = min([y for (_, y, _, _) in recs])
bottom_all = max([y for (_, _, _, y) in recs])
area = 0
for x in range(left_all, right_all):
for y in range(top_all, bottom_all):
if any([isInside(x, y, rec) for rec in recs]):
area += 1
return area
if __name__ == '__main__':
print("Example:")
print(rectangles_union([
(6, 3, 8, 10),
(4, 8, 11, 10),
(16, 8, 19, 11)
]))
# These "asserts" are used for self-checking and not for an auto-testing
assert rectangles_union([
(6, 3, 8, 10),
(4, 8, 11, 10),
(16, 8, 19, 11)
]) == 33
assert rectangles_union([
(16, 8, 19, 11)
]) == 9
assert rectangles_union([
(16, 8, 19, 11),
(16, 8, 19, 11)
]) == 9
assert rectangles_union([
(16, 8, 16, 8)
]) == 0
assert rectangles_union([
]) == 0
print("Coding complete? Click 'Check' to earn cool rewards!")
Feb. 20, 2022
Comments: