I have to answer the following question:
8. We have a dictionary containing several rectangular islands:
islands = {
"Banana island" : (3,5,7,6),
"Mango island" : (10,3,19,4),
"Pineapple island" : (8,8,9,20),
"Coconut island" : (2,13,5,9)
}
Write code to calculate which island has the minimum area, using your land_rectangle_area function.
I have created a function for the area:
def land_rectangle_area(x1, y1, x2, y2):
area=abs((int(x1)-int(x2))*(int(y1)-int(y2)))
return(area)
I am unsure how I proceed with this question from here?
There you go. Just iterate through the islands, finding their area and update your answer if the current area is less than minimum.
def land_rectangle_area(x1, y1, x2, y2):
area=abs((int(x1)-int(x2))*(int(y1)-int(y2)))
return(area)
islands = {
"Banana island" : (3,5,7,6),
"Mango island" : (10,3,19,4),
"Pineapple island" : (8,8,9,20),
"Coconut island" : (2,13,5,9)
}
minimum = float('Inf') # equivalent to infinity
ans=''
for i in islands:
a = land_rectangle_area(*islands[i])
if a<minimum:
minimum = a
ans = i
print (ans,minimum)
Output :
('Banana island', 4)