Now I am wondering how to move a window forward by 4 uints or other number of units instead of only one unit each time. Could you please give me clues about it?
My code is like this:
import sys
INT_MIN = -sys.maxsize - 1
def maxSum(arr,n,k):
max_sum = INT_MIN
for i in range(n-k+1):
current_sum = 0
for j in range(k):
current_sum = current_sum + arr[i+j]
print(current_sum)
max_sum = max(current_sum, max_sum)
return max_sum
arr = [1,4,2,10,2,3,1,0,20,1,1,2,3,4,5,6,3,2,4,5,1,4,5]
k = 4
n = len(arr)
You mostly have a correct answer already. You wrote
for i in range(n - k + 1):
All that is needed is to specify a step
of k
:
for i in range(0, n - k + 1, k):
nit: this code
current_sum = 0
for j in range(k):
current_sum = current_sum + arr[i + j]
could be written with slice notation as current_sum = sum(arr[i:i + k])
.
Pandas offers an implementation that might be of interest to you.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.