Return the persistence of a given number. https://en.wikipedia.org/wiki/Persistence_of_a_number >>> multiplicative_persistence(217) 2 >>> multiplicative_persistence(-1) Traceback (most recent call last): ... ValueError: multiplicative_persistence() does not acc
(num: int)
| 1 | def multiplicative_persistence(num: int) -> int: |
| 2 | """ |
| 3 | Return the persistence of a given number. |
| 4 | |
| 5 | https://en.wikipedia.org/wiki/Persistence_of_a_number |
| 6 | |
| 7 | >>> multiplicative_persistence(217) |
| 8 | 2 |
| 9 | >>> multiplicative_persistence(-1) |
| 10 | Traceback (most recent call last): |
| 11 | ... |
| 12 | ValueError: multiplicative_persistence() does not accept negative values |
| 13 | >>> multiplicative_persistence("long number") |
| 14 | Traceback (most recent call last): |
| 15 | ... |
| 16 | ValueError: multiplicative_persistence() only accepts integral values |
| 17 | """ |
| 18 | |
| 19 | if not isinstance(num, int): |
| 20 | raise ValueError("multiplicative_persistence() only accepts integral values") |
| 21 | if num < 0: |
| 22 | raise ValueError("multiplicative_persistence() does not accept negative values") |
| 23 | |
| 24 | steps = 0 |
| 25 | num_string = str(num) |
| 26 | |
| 27 | while len(num_string) != 1: |
| 28 | numbers = [int(i) for i in num_string] |
| 29 | |
| 30 | total = 1 |
| 31 | for i in range(len(numbers)): |
| 32 | total *= numbers[i] |
| 33 | |
| 34 | num_string = str(total) |
| 35 | |
| 36 | steps += 1 |
| 37 | return steps |
| 38 | |
| 39 | |
| 40 | def additive_persistence(num: int) -> int: |
nothing calls this directly
no outgoing calls
no test coverage detected