Last active
June 3, 2023 17:46
-
-
Save yoniLavi/ebc70eb8a32ea7010868bce46cb4481e to your computer and use it in GitHub Desktop.
A silly numpy-based solution to https://www.codewars.com/kata/56b0f6243196b9d42d000034
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def factorial_row(n, length): | |
| """Create 1d array whose product is the factorial of n, padded to length""" | |
| return np.pad(np.arange(1, n + 1), (0, length - n), 'constant', constant_values=1) | |
| def sum_factorial(nums): | |
| """Sum the factorials of the numbers in nums.""" | |
| factorial_matrix = np.vstack([factorial_row(n, max(nums)) for n in nums]) | |
| return factorial_matrix.prod(axis=1).sum() | |
| sum_factorial([2, 3]) # Returns 8 (2! + 3!) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment