Currently I'm going through the problems on CodeWars, and I'm stuck on the Persistent Bugger problem.
Write a function, persistence, that takes in a positive parameter num and returns its multiplicative persistence, which is the number of times you must multiply the digits in num until you reach a single digit.
For example:
persistence(39) # returns 3, because 39=27, 27=14, 14=4 # and 4 has only one digit persistence(999) # returns 4, because 999=729, 729=126, # 126=12, and finally 12=2
persistence(4) # returns 0, because 4 is already a one-digit number
I've narrowed down my problem to a recursive function, however I'm having trouble wrapping my head around how to return my iteration counter.
Currently it runs through the program and maintains an accurate count. When it ends up with a single digit value however, it returns to the persistence call, lowering my iteration every time.
def persistence(n, iter=0):
chars = str(n)
if n > 9:
result = 1
for y in chars:
result = result * int(y)
iter += 1
persistence(result, iter)
else:
return iter