Reputation: 45
I'm testing if the word number is the power of 2 by log function. If the word number is low, I append the word by '.' until the length becomes the power of 2. Eg, If the word(W) is 'abcd', the len(W) = 4, which is 2^2. The function will stop. If the W is 'abcdef', the len(W) = 6, I want to append the word by '.', so it becomes 'abcdef..' The new W length is 8, which is 2^3.
Please check where I need to make changes.
import math
def powerOf2(W):
logP = math.log2(len(W))
if isinstance(logP, int):
return W
else:
W.append('.')
powerOf2(W)
When I run
W = list('abcd')
powerOf2(W)
The program stops with "maximum recursion". I thought the condition is true. What seems to be a problem?
Working script. I changed the isinstance function.
import math
def powerOf2(W):
logP = math.log2(len(W))
if (logP).is_integer():
return W
else:
W.append('.')
powerOf2(W)
Upvotes: 0
Views: 47
Reputation: 337
You need to return the recursion:
import math
def powerOf2(W):
logP = math.log2(len(W))
if isinstance(logP, int):
return W
else:
W.append('.')
return powerOf2(W)
Upvotes: 2