Sum of variabels from each for loop iterations - arrays

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

Related

How do I iterate through matrix elements matlab

I have the task of creating a piece of matlab code that uses the sieve of Eratosthenes to find the list of prime numbers up to N. I have created a loop that finds the non primes and then finds the index value of them in the list of 2 to N. How do I get my program to take these index values element by element and set the corresponding positions in my zero matrix to one?
Also for my assignment I cannot use the in built isprime functions.
My code so far:
function [p,c] = sieve(N)
N = input('Please type an integer greater than 1: ');
a = ones(1,N); %Non-primes are set to 0
for k = 2:N
How does k:k:end work, I'm guessing it adds k until it reaches N.
Thanks
Assuming your matrix of zeros is called "numbersthatareprime" and your prime indices are called "primeindices":
numbersthatareprime(primeindices)=1
That's just a matter of using your array to index into your vector. As such, create a vector of all zeros that is N in length, then assuming you have the list of prime numbers up to N which is called prim, just do:
vec = zeros(1, N);
vec(prim) = 1;
OK
As OP may still be confused, I'll just give a new answer (not dissimilar to my previous wrong one)
x=zeros(100,1);
for i=2:100;
x(2*i:i:end)=1;
end; find(~x)
You just need to go from 2*i rather than i....
You don't really need a matrix. Just a list of values!
X=zeros(10000,1);
for i=2:100
X(i:i:end) = 1
end
Here, the indexing i:i:end means
[2,4,6,8,...] when i==2
[3,6,9,...] when i==3
etc
So it sets all the multiples of 2, then all the multiples of 3 etc., creating your seive.
Note that you only need to go up to sqrt(N).
Then you can just do find(X) to get the primes!

How to solve while loop in array?

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

Endless loop for Write a function first_n_evens(n) that returns an array of the first N many even numbers

I can't figure out how to correctly complete this question.
Write a function first_n_evens(n) that returns an array of the first N many even numbers, starting from 0.
Please help me understand what I am doing wrong.
Many Thanks
def first_n_evens(n)
array = []
number = 0
while array.count <= n
if number % 2 == 0
array.push(number)
number += 1
end
end
return array
end
test = first_n_evens(5)
puts test
This line:
number += 1
Is inside of your if block. If number is even, number gets incremented. If number is odd, then the if condition isn't met, and nothing happens.
Move that line outside of the if..end block, and you should be okay.

Number of ways such that sum of k elements equal to p

Given series of integers having relation where a number is equal to sum of previous 2 numbers and starting integer is 1
Series ->1,2,3,5,8,13,21,34,55
find the number of ways such that sum of k elements equal to p.We can use an element any number of times.
p=8
k=4.
So,number of ways would be 4.Those are,
1,1,1,5
1,1,3,3
1,2,2,3
2,2,2,2
I am able to sove this question through recursion.I sense dynamic programming here but i am not getting how to do it.Can it be done in much lesser time???
EDIT I forgot to mention that the sequence of the numbers does not matter and will be counted once. for ex=3->(1,2)and(2,1).here number of ways would be 1 only.
EDIT: Poster has changed the original problem since this was posted. My algorithm still works, but maybe can be improved upon. Original problem had n arbitrary input numbers (he has now modified it to be a Fibonacci series). To apply my algorithm to the modified post, truncate the series by taking only elements less than p (assume there are n of them).
Here's an n^(k/2) algorithm. (n is the number of elements in the series)
Use a table of length p, such that table[i] contains all combinations of k/2 elements that sum to i. For example, in the example data that you provided, table[4] contains {1,3} and {2,2}.
EDIT: If the space is prohibitive, this same algorithm can be done with an ordered linked lists, where you only store the non-empty table entries. The linked list has to be both directions: forward and backwards, which makes the final step of the algorithm cleaner.
Once this table is computed, then we get all solutions by combining every table[j] with every table[p-j], whenever both are non-empty.
To get the table, initialize the entire thing to empty. Then:
For i_1 = 0 to n-1:
For i_2 = i_1 to n-1:
...
For i_k/2 = i_k/2-1 to n-1:
sum = series[i_1] + ... + series[i_k/2]
if sum <= p:
store {i_1, i_2, ... , i_k/2 } in table[sum]
This "variable number of loops" looks impossible to implement, but actually it can be done with an array of length k/2 that keeps track of where each i_` is.
Let's go back to your data and see how our table would look:
table[2] = {1,1}
table[3] = {1,2}
table[4] = {1,3} and {2,2}
table[5] = {2,3}
table[6] = {1,5}
table[7] = {2,5}
table[8] = {3,5}
Solutions are found by combining table[2] with table[6], table[3] with table[5], and table[4] with table[4]. Thus, solutions are: {1,1,1,5} {1,2,2,3}, {1,1,3,3}, {2,2,2,2}, {1,3,2,2}.
You can use dynamic programming. Let C(p, k) be the number of ways that sum k element equal to p and a be the array of elements. Then
C(p, k) = C(p - a[0], k - 1) + C(p - a[1], k - 1) + .... + C(p - a[n-1], k - 1)
Then, you can use memorization to speed up your code.
Hint:
Your problem is well-known. It is the sum set problem, a variation of knapsack problem. Check this pretty good explanation. sum-set problem

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!

Resources