Skip to content

Instantly share code, notes, and snippets.

@kofrasa
Created July 21, 2019 17:08
Show Gist options
  • Save kofrasa/360a0e8867d94eafd6850b65dcb9d71c to your computer and use it in GitHub Desktop.
Save kofrasa/360a0e8867d94eafd6850b65dcb9d71c to your computer and use it in GitHub Desktop.
#!/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