loading...

Discussion on: Daily Challenge #214 - Persistent Bugger

Collapse
arsho profile image
Ahmedur Rahman Shovon

Python solution with test cases.

from functools import reduce


def persistence(n):
    count = 0
    while True:
        if len(str(n)) == 1:
            return count
        n = reduce(lambda x, y: x*y, [int(i) for i in str(n)], 1)
        count += 1

if __name__ == "__main__":
    test_cases = [39, 999, 4, 5, 52, 377]  
    expected_results = [3, 4, 0, 0, 2, 4]
    total_test_cases = len(test_cases)
    for i in range(total_test_cases):
        test_case = test_cases[i]
        expected_result = expected_results[i]
        result = persistence(test_case)
        assert_message = "persistence({}) returns {}. Expected {}".format(
            test_case, result, expected_result
        )
        assert result == expected_result, assert_message
    print("Passed all {} test cases".format(total_test_cases))