Check if any square (with one colored cell) can be divided into two equal parts in Python

Suppose, we are provided with a square that is a size of n. The n sized square is further divided into n2 smaller squares. The smaller squares are of unit size and one of the squares is colored with a unique color.

Now if we cut the bigger square into two equal parts, we have to cut it in such a way so that the cutting line does not have any points in common with that uniquely colored small square. We have to also consider the fact that the newly cut two pieces are mirror images of one another. So, we have to find out if cutting a square like that is possible given the conditions. We have the value of n and the position of the colored square in the bigger square.

So, if the input is like size = 50, colored_row_pos = 25, colored_col_pos = 25, then the output will be "Cutting is not possible."

To solve this, we will follow these steps −

  • middle := floor value of size /2
  • if (middle is same as colored_row_pos or middle is same as colored_row_pos - 1) and (middle is same as colored_col_pos or middle is same as colored_col_pos - 1) , then
    • return False
  • otherwise,
    • return True

Let us see the following implementation to get better understanding −


 Live Demo

def solve(size, colored_row_pos, colored_col_pos) :
   middle = size // 2
   if (middle == colored_row_pos or middle == colored_row_pos - 1) and (middle ==    colored_col_pos or middle == colored_col_pos - 1) :
      print("Cutting is not possible")
   else :
      print("Cutting is possible")
size = 50
colored_row_pos, colored_col_pos = 25, 25 
solve(size, colored_row_pos, colored_col_pos)


50, 25, 25


Cutting is not possible

Updated on: 30-Dec-2020


Kickstart Your Career

Get certified by completing the course

Get Started