Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Rectangles Union by Oleg_Babaev
def rectangles_union(recs):
def slice_rect(x1,y1,x2,y2):
return {(x1 + dx, y1 + dy) for dx in range(x2 - x1) for dy in range(y2 - y1)}
res = set()
for rec in recs:
res |= slice_rect(*rec)
return len(res)
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!")
April 21, 2021
Comments: