Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Creative category for Rectangles Union by Kurush
from typing import List, Tuple
def rectangles_union(recs: List[Tuple[int]]) -> int:
if recs == []: return 0
x_min = min(rect[0] for rect in recs)
y_min = min(rect[1] for rect in recs)
x_max = max(rect[2] for rect in recs)
y_max = max(rect[3] for rect in recs)
S = 0
for x in range(x_min, x_max + 1):
for y in range(y_min, y_max + 1):
if list(filter(lambda rect: rect[0] <= x < rect[2] and rect[1] <= y < rect[3], recs)) != []:
S += 1
return S
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)
]) == 24
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!")
May 24, 2021