-2

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?

Mureinik
  • 297,002
  • 52
  • 306
  • 350
K-Q
  • 133
  • 8
  • 1
    How about starting with the [docs](https://docs.python.org/3/tutorial/datastructures.html#dictionaries)? As my instructors would say on any standard library, "RTFM". – pstatix Oct 20 '17 at 19:22
  • Sounds like you need to read the documentation/a tutorial. The official docs/tutorial are a good place to start. – juanpa.arrivillaga Oct 20 '17 at 19:23

3 Answers3

1

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])))
dawg
  • 98,345
  • 23
  • 131
  • 206
0

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)
Miraj50
  • 4,257
  • 1
  • 21
  • 34
0

Think about your problem set:

  • You're given the container already. So now you need to learn how to access its contents.
  • You already know the function to calculate area (A = L*W).
  • You need to find the minimum area. So you need to keep track of the lowest value.

Given that, you should be able to reason out the process (but not necessarily the implementation). In these situations, get out the good ole' pen and paper and write it down:

smallestAreaSoFar = None 
smallestIsland = None

for each island in my container of islands:
    access island coordinates
    calculateArea(coordinates)
    if we havent seen an area yet:
        smallestAreaSoFar = calculatedArea
        smallestIsland = island
    if we have:
        if smallestAreaSoFar > calculatedArea:
             smallestAreaSoFar = calculatedArea
             smllestIsland = island

output smallestIsland
pstatix
  • 3,611
  • 4
  • 18
  • 40