>>> factorial(7) 5040 >>> factorial(-1) Traceback (most recent call last): ... ValueError: Number should not be negative. >>> [factorial(i) for i in range(10)] [1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880]
(num: int)
| 5 | |
| 6 | @lru_cache |
| 7 | def factorial(num: int) -> int: |
| 8 | """ |
| 9 | >>> factorial(7) |
| 10 | 5040 |
| 11 | >>> factorial(-1) |
| 12 | Traceback (most recent call last): |
| 13 | ... |
| 14 | ValueError: Number should not be negative. |
| 15 | >>> [factorial(i) for i in range(10)] |
| 16 | [1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880] |
| 17 | """ |
| 18 | if num < 0: |
| 19 | raise ValueError("Number should not be negative.") |
| 20 | |
| 21 | return 1 if num in (0, 1) else num * factorial(num - 1) |
| 22 | |
| 23 | |
| 24 | if __name__ == "__main__": |
nothing calls this directly
no outgoing calls
no test coverage detected