Returning wrong maximum sum sub array(kadane's algorithm), but correct maximum sum
Tag : python , By : user107506
Date : March 29 2020, 07:55 AM
Hope that helps Given below is my code for the kadane's algorithm in Python 2.7 for returning the maximum sub array. Although, i'm getting the correct maximum sum(MSS variable) ,for the given example list, it's returning the wrong sub array. Could someone please explain to me why ? , Your issue is because when you do - ans=subans
ans = []
subans = []
MSS,subsum,i = 0,0,0
while(i<L):
subans.append(A[i])
subsum = sum(subans)
if(subsum<0):
subans=[]
i+=1
else:
print('subsum - ' + str(subsum))
print('MSS - ' + str(MSS))
if(subsum>MSS):
MSS=subsum
ans=list(subans) #more ways to do this, like subans[:] also works, and copy.copy(subans) , etc.
i+=1
else:
i+=1
|
matplotlib basemap hexbin colorbar maximum value higher than maximum value in array
Tag : python , By : GunnarHafdal
Date : March 29 2020, 07:55 AM
this will help I'm using matplotlib and basemap to draw some maps. What I'm doing is diaplying some spatial data with hexbin function. , Try including the vmax argument in hexbin. Here's some example code: import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(1, 100, 1000)
y = x
plt.hexbin(x, y, mincnt = 1, gridsize = 16, vmax = 100, cmap='summer')
|
Find maximum value from 2 dimensional array & addition of all the values before the maximum value & multiply the
Tag : c , By : user186876
Date : March 29 2020, 07:55 AM
I wish this help you Here is my code #include<stdio.h>
void main(){
int a[4][4]={
{10,11,12,13},
{14,15,16,17},
{18,19,20,21},
{22,2,3,3}
};
int max = a[0][0],mIndexF,mIndexE,addition = 0,multiplication = 1,i,j,status=0,k,l;
// this is for find out maximum value
for(i=0;i<4;i++){
for(j=0;j<4;j++){
if(max<a[i][j]){
max = a[i][j];
mIndexF=i;
mIndexE=j;
}
}
}
printf("The maximum value is %d\n", max);
for(k=0;k<4;k++){
for(l=0;l<4;l++){
if((a[k][l]<max) &&(status==0)){
addition+=a[k][l];
}else{
status++;
if(a[k][l]!=max){
multiplication*=a[k][l];
}
}
}
}
printf("Addition is %d\n",addition);
printf("Multiplication is %d",multiplication);
return 0;
}
|
Most efficient method to find maximum element and list of maximum elements from a 2-d array?
Date : March 29 2020, 07:55 AM
may help you . Suppose I have a list arr=[[1,2,3],[4,5,6],[7,8,9]] here maximum element is 9 and maximum elements from 1 d list is [3,6,9]. , If using numpy >>>import numpy as np
>>>array=np.random.rand(3,3)
>>>print(array)
>>>print(array.max(axis=1))
>>>[[ 0.76562624 0.45225107 0.74276688]
[ 0.84390255 0.03384166 0.40036534]
[ 0.00371805 0.47996941 0.15593055]]
>>>[ 0.76562624 0.84390255 0.47996941]
>>>arr=[[1,2,3],[4,5,6],[7,8,9]]
>>>print(list(map(max,arr)))
>>>[3,6,9]
>>>print(max(map(max,arr)))
>>>9
>>>print(array.max())
|
How many times update maximum gets called when trying to find the running maximum in a randomized array of numbers?
Tag : arrays , By : user180941
Date : March 29 2020, 07:55 AM
it fixes the issue Let's consider randomly shuffled array. We want to estimate K - number of times than i-th element is bigger than all of its predecessors. Expected value of K equals to sum of probabilities that i-th element is bigger than all its predecessors. E(K) = Σ12N+1 Pi. constexpr size_t N = 1000;
std::array<int, 2 * N + 1> arr;
std::iota(arr.begin(), arr.end(), -N);
std::random_device rd;
std::mt19937 g(rd());
double moving = 0;
for (double trial = 1; trial < 10001; ++trial) {
std::shuffle(arr.begin(), arr.end(), g);
int called = 0;
int max = std::numeric_limits<int>::min();
for (int i = 1; i < arr.size(); ++i) {
if (arr[i] > max) {
++called;
max = arr[i];
}
}
if (trial > 1) {
moving = moving * ((trial - 1) / trial) + called / trial;
}
else {
moving = called;
}
}
cout << "actual: " << moving << endl;
cout << "expected: " << std::log(2 * N + 1) << " + O(1)" << endl;
actual: 8.1581
expected: 7.6014 + O(1)
|