Polygon Area Calculator get_amount_inside Help

I have a question about the get_amount_inside() function that I haven’t seen answered in other discussions.

I initially built the function by doing floor division of the outer area by the inner area

def get_amount_inside(self, shape)
    return self.get_area() // shape.get_area()

But thinking about the problem in the head I realized that for some instances you would have to readjust the remaining 1x1 squares to accurately fit another rectangle and square. Despite this issue the function would return 2 instead of 1.

For Example a 5x5 square can only contain one 3x3 square but 25 // 9 would return 2 instead of 1.

The prompt does not clarify this but does state No Rotations. So does this mean that my original function does not suffice in correctly returning the expected value in every instance? If so, would the code below work?

  def get_amount_inside(self,shape):
    fieldWidth = self.width
    fieldHeight = self.height
    sectWidth = shape.width
    sectHeight = shape.height
    count = 0
    while fieldWidth >= sectWidth and fieldHeight >= sectHeight:
      fieldWidth -= sectWidth
      fieldHeight -= sectHeight
      count += 1
    return count

EDIT: The solution I made is below.
P.S. the only reason I made this post is because there are solutions being circulated on Polygon Forum that state the wrong answer (The first one I posted). I was just trying to shine light on an issue I found so that other people can maybe try to solve the problem correctly.

def get_amount_inside(self,shape):
    fieldWidth = self.width
    fieldHeight = self.height
    sectWidth = shape.width
    sectHeight = shape.height
    countWidth = 0
    countHeight = 0
    if fieldHeight > sectHeight:
      countHeight = fieldHeight // sectHeight
    if fieldWidth > sectWidth:
      countWidth = fieldWidth // sectWidth
    return countWidth * countHeight

This is a little bit off from the topic of coding but rather a process of problem solving I suppose.
Consider: Why can only one 3x3 square fit inside a 5x5 square?

Hint: You’re currently using the area of the squares to calculate how many fits inside. Perhaps that is not the correct way to calculate it?

Answer: The reason only one 3x3 square fits inside a 5x5 square is because of the length of the sides. In other words, 5 / 3 floored is 1. Repeat for both sides and multiply these two numbers

1 Like

While Andreas gave you most of the solution:

  1. If you already have some code, don’t ask us if it works → test in in the project and see for yourself :wink:
  2. If you want to get an idea on how to solve it, think about some non-trivial examples, do them by hand and ask yourself, what you actually did → then think about how to turn that into a list of commands for a human and dumb-down that for a unthinking machine.

For example: What would your function return for 1x1 squares in a 2x2 area?
The actual answer would be 4.
What would your function return? I think 2.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.