python - Find array with the maximum sum of elements -


i have problem task: "in given array of integers 𝑁 of size 𝑛 × 𝑛 𝑀 find array of given size 𝑚 × 𝑚, 𝑚 <𝑛 such sum of elements in array 𝑀 largest possible." wanted create loop check every possible 𝑚 × 𝑚 array , sum failed @ cutting list of lists given size. how can cut array of 𝑛 × 𝑛 𝑚 × 𝑚? i'm using build-in functions.

if understand problem correctly, use iloc in pandas. see below example.

import pandas pd import numpy np  df = pd.dataframe(np.arange(16).reshape(4,4))  # blocks of size m=2 in dataframe of size 4 m = 2 n = 4  # prints mxm block of df , corresponding sum in range(0, n-m+1):     j in range(0, n-m+1):         tmp_df = df.iloc[i:i+m, j:j+m]         print(tmp_df)         print('sum:', tmp_df.values.sum(), '\n') 

edit: saw stipulation of built-in functions. if pandas doesn't fit bill, form of data?

second shot below using list comprehensions instead of pandas.

array = [list(range(x, x+4)) x in range(0,13, 4)]  def slice_array(array, m, start_row, start_col):      tmp = [[x x in row[start_col:start_col+m]] \            row in array[start_row:start_row+m]]     return tmp   >>> slice_array(array, m=2, start_row=0, start_col=0)  [[0, 1], [4, 5]]   >>> slice_array(array, m=2, start_row=0, start_col=1)  [[1, 2], [5, 6]] 

Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -