You can use Python's min
function with your land_rectangle_area
function as a key to do this:
def land_rectangle_area(x1, y1, x2, y2):
area=abs((int(x1)-int(x2))*(int(y1)-int(y2)))
return(area)
>>> min(islands.items(), key=lambda (k,t): land_rectangle_area(*t))
('Banana island', (3, 5, 7, 6))
You can find the max
the same way:
>>> max(islands.items(), key=lambda (k,t): land_rectangle_area(*t))
('Pineapple island', (8, 8, 9, 20))
Or, use a list comprehension to transform the tuple into the area:
>>> [(k,land_rectangle_area(*t)) for k,t in islands.items()]
[('Pineapple island', 12), ('Coconut island', 12), ('Banana island', 4), ('Mango island', 9)]
And then take the min
of that:
>>> min([(k,land_rectangle_area(*t)) for k,t in islands.items()], key=lambda t: t[1])
Or sort them smallest to largest:
>>> sorted(islands.items(), key=lambda (k,t): land_rectangle_area(*t))
[('Banana island', (3, 5, 7, 6)), ('Mango island', (10, 3, 19, 4)), ('Pineapple island', (8, 8, 9, 20)), ('Coconut island', (2, 13, 5, 9))]
(Since Coconut Island and Pineapple Island have the same area, either of them could be considered the max
for the function and the sort. You would add another key to be definitive...)
Or you can bypass the named function and just use min
with a key function:
>>> min(islands.items(), key=lambda (k,t): abs((t[0]-t[2])*(t[1]-t[3])))