Created
July 21, 2019 17:08
-
-
Save kofrasa/360a0e8867d94eafd6850b65dcb9d71c to your computer and use it in GitHub Desktop.
This file contains 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
#!/usr/bin/env python | |
def product_k_consecutive(arr, k): | |
last = 1 # store last product | |
result = [] | |
for i, n in enumerate(arr): | |
acc = n*last | |
if i >= k: | |
div = arr[i-k] | |
if div != 0: | |
acc /= div | |
last = acc | |
result.append(acc) | |
return result | |
def main(): | |
k = 3 | |
nums = [1, 3, 3, 6, 5, 7, 0, -3] | |
output = product_k_consecutive(nums, k) | |
print(output) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment