I have a big square, which is made of small square-tiles of fixed size.
The area of these small square-tiles are known.
One of the tile is shown on the upper left corner.
Now,
Each square can be broken down into 4 subsquares. And each square has a key which identifies the square.
There might be many empty squares inside the big-square. In those cases the key doesn't exist and the area is considered as zero.
The smallest tile have a key length of 3.
I want to recursively find the area of any square given a key.
This is what I am trying. But it is not giving me the right solution.
findAreaRecursive(self, key, maxDepth=3):
if len(Key) == maxDepth:
if self.keyExists(key):
return self.getAreaValue(key)
else:
return 0
else:
keyChild0 = key + '0'
keyChild1 = key + '1'
keyChild2 = key + '2'
keyChild3 = key + '3'
if self.keyExists(keyChild0):
areaChild0 = self.findAreaRecursive(keyChild0, maxDepth)
else:
areaChild0 = 0
if self.keyExists(keyChild1):
areaChild1 = self.findAreaRecursive(keyChild1, maxDepth)
else:
areaChild1 = 0
if self.keyExists(keyChild2):
areaChild2 = self.findAreaRecursive(keyChild2, maxDepth)
else:
areaChild2 = 0
if self.keyExists(keyChild3):
areaChild3 = self.findAreaRecursive(keyChild3, maxDepth)
else:
areaChild3 = 0
return areaChild0 + areaChild1 + areaChild2 + areaChild3
What am I doing wrong. I am new to recursion. Any help is welcome.