How to solve while loop in array? - arrays

Hello could anyone tell me how to solve this one, it's so simple but I'm new in programming.
f([9,8,6,2])

For what it's worth, the function is looping through the given array [9,8,6,2] and adding the numbers together. It's essentially a sum of the values of the given array.
The var s is initially zero (0) (int s = 0). It loops through each value in the array and adds it to itself (s = s + arr[i]). As the loop continues, the value of s continues to grow.
Therefore, the returned value of the function would be 25:
s = 9 + 8 + 6 + 2

Related

Sum of variabels from each for loop iterations

I want to solve this exercise https://www.codewars.com/kata/5648b12ce68d9daa6b000099/train/python
How can I count result of count variable from first and second loop together ?
Result from first loop is 4-2 = 2, now I want sum of the 2 + result from second loop 10-6= 4. So the total sum should be 6 . Thanks for your tips :)
array =[(4,2), (10,6)]
print((array))
print(array[1])
for x in array:
count = (x[0] - x[1])
print (count)
I am not sure if this process is good for the exercise but I want it try this way :)
Thanks :)
your approach doesn't look too bad. Iterating over the bus stop to find the difference of people leaving and joining the bus. You only have to add one variable that adds up these differences. I called it counter, which counts the current number of people on the bus. It starts with a value of zero. Then in each iteration, we add the difference to the counter variable. If more people leave the bus than enter it the counter will be decreased as current_difference is negative.
Here is an example implementation:
array =[(4,2), (10,6)]
counter = 0
for x in array:
current_difference = (x[0] - x[1])
print (current_difference)
counter = counter + current_difference
print(counter)
Result:
6

Matlab : Confusion over find() function

find() function returns the indices where the elements are non-zero. I tried with different array sizes but both give error :
In an assignment A(I) = B, the number of elements in B and I must be the same.
I am confused because when the array size is same, still I am getting this error.
This is just to understand what went wrong:
LEt,
Example 1: Same array size
A = [20;21;3;45;5;19;1;8;2;1];
B = A;
for i =1:length(B)
pos(i) = find(A == B(i));
end
I should have got pos = [1,2,3,4,5,6,7,8,9,10]. But the loops exits after i = 7, giving `pos = [1,2,3,4,5,6]'
Example 2: Dissimilar array size
C = [20;1;10;3];
for i =1:length(C)
pos(i) = find(A == C(i));
end
Can somebody please explain what is wrong in my understanding and an illustration of how I can work with same and different array length of A and B? Thank you.
The problem is that find(A == 1) returns two indexes, both 7 and 10, and that can't be stored in pos(i), since pos(i) can only hold a single number.
Unfortunately, the generic error message happened to have the same name for the matrices as two of your matrices, which can be confusing before you'we seen it a few times.

minimum number of actions needed to sort an array

I'm trying to practice solving a problem from Codeforces. It is to sort an array by moving the elements of the array either to the beginning or to the end of the array. At first thought i thought it is longest increasing subsequence but it's not working in some cases. For example if the input is 4,1,2,5,3 the LIS is 3 but the answer for the problem is moving 4 to the end of the array and then 5 to the end of the array which gives us 2. Also i was trying out on the example 1,6,4,5,9,8,7,3,2 in this LIS is 1,4,5,9 but the answer for the problem is 7 moves between 1 and 2. I got to know that i should use greedy approach but couldn't quite relate. Could someone help me in this ?
We can see that, to sort the array, each element is only need to be moved at most one.
So, to minimize the number of movement, we need to find the maximum number of element that is not moved. And those element is the longest continuous sequence , which is the sequence (a0, a1, ... an) with a(i + 1) = ai + 1.
For example,
(4,1,2,5,3), longest continuous sequence is (1,2,3)
(5,2,1,3,4), longest continuous sequence is (2,3,4)
So we have our code:
int[]longest = new int[n + 1];
int result = 0;
for(int i = 0; i < n; i++){
longest[data[i]] = longest[data[i] - 1] + 1;
result = max (longest[data[i]] , result);
}
print "Minimum number of move is " + (n - result)
Explanation:
In the code, I am using an array longest which index ith stores the longest continuous sequence, which ends at value i.
So, we can see that longest[i] = longest[i - 1] + 1.
And the result for the longest continuous sequence is the maximum value stored in longest array.
I had solved this problem on Codeforces during the contest itself. Nice problem.
Think 'longest continuous sub-sequence'. The answer is n-longest continuous sub-sequence.
Example:
Take 1 2 3 7 5 6 4. The longest continuous sub-sequence is 1 2 3 4. Now you can shift the remaining elements in a particular order to get the sorted array always. At least that is how I thought of it intuitively
Here is a snippet of the main code:
int n=in.readInt();
int[] a=new int[n+1];
int[] cnt=new int[n+1];
int max=0;
for(int i=0;i<n;i++)
a[i]=in.readInt();
for(int i=0;i<n;i++)
{
cnt[a[i]]=1+cnt[a[i]-1];
max=Math.max(max,cnt[a[i]]);
}
out.printLine((n-max));
Hope that helps!

how to check in matlab if a 3D array has at least one non zero element?

I tried to check if a 3D array is not all zeros using the next code:
notAll_n0_GreaterThan_ni=1;
while notAll_n0_GreaterThan_ni
notAll_n0_GreaterThan_ni=0;
mask=(n0<ni);
numDimensions=ndims(mask);
for dim_ind=1:numDimensions
if any(mask,dim_ind)
notAll_n0_GreaterThan_ni=1;
break;
end
end
if notAll_n0_GreaterThan_ni
n0(mask)=n0(mask)+1;
end
end
It seems I have error in the code because at the end I get for example: n_0(11,3,69)=21 while ni(11,3,69)=21.1556.
I can't find the error. I'll appreciate if someone shows me where I'm wrong and also if there is a simpler way to check existence of nonzero elements in a 3D array.
Let x denote an n-dimensional array. To check if it contains at least one non-zero element, just use
any(x(:))
For example:
>> x = zeros(2,3,4);
>> any(x(:))
ans =
0
>> x(1,2,2) = 5;
>> any(x(:))
ans =
1
Other, more exotic possibilities include:
sum(abs(x(:)))>0
and
nnz(x)>0
This is what you looking for
B = any(your_Array_name_here(:) ==0); no need for loops
the (:) turns the elements of your_Array into a single column vector, so you can use this type of statement on an array of any size
I 've tested this and it works
A = rand(3,7,5) * 5;
B = any(A(:) ==0);

How to build an array by function handle

I am trying to produce something like this in MATLAB with function handle
f=#(x,y)(x(1)*x(2)+y);
c=[2 3 4;5 9 2];
h=[5 1 2];
f(c,h)
The answer should be:
15 11 12
But when I write this code below, it just builds a number not an array.
f=#(x)(x(1)*x(2))
f(c)
answer:
10
Can someone explain me where I went wrong?
I do not know what you expected here. The cause of the problem is quite clear.
a = 1;
b = 2;
c = [3 4];
d = a*b+c;
is a scalar + vector operation which always returns
ans = [a*b+c(1), a*b+c(2)];
however scalar*scalar which was the second case always returns a scalar. What you do is that you multiply the first matrix element of x (or c) with the second element. That is to say element c(1,1)*c(2,1) since matlab works columnwise. If you looks at your values you would probably notice the answer is incorrect as well, if you are trying to do what I think you are. You could try this instead,
f=#(x,y)(x(1,:).*x(2,:)+y);
c=[2 3 4;5 9 2];
h=[5 1 2];
f(c,h)
which multipies the elements on the first row of x with the same column on the second row and then adds y. An anonymous function takes a number of inputs and perform a defined operation, the same as ordinary functions or ordinary codes. You can see them as functions that does not require a call to another m file. The main differences (except that ordinary functions gives more freedom), are how they are handled by matlab and not in the syntax.

Resources