from functools import reduce
from operator import mul
def persistence(n):
i = 0
while n > 9:
n = reduce(mul, map(int, str(n)), 1)
i += 1
return i
Вариант без дополнительных импортов:
def persistence(n):
i = 0
while n > 9:
n = eval('*'.join(f'{n}'))
i += 1
return i
Вариант без while:
from functools import reduce
from operator import mul
from itertools import count
def persistence(n):
for i in count(0):
if n > 9:
n = reduce(mul, map(int, str(n)), 1)
else:
return i
Проверки корректности результата:
assert persistence(39) == 3
assert persistence(999) == 4
assert persistence(4) == 0