max() function stops for loop - for-loop

I don't know why but for loop after printing max value from array does not work. But if I remove print(max(arr)) it works fine.
Input:
3 3 3 34 5
Code:
arr = map(int, set(input().split()))
print(max(arr))
for i in arr:
print(i)
Expected output:
34
3
34
5
Output:
34

You have exhausted the iterator returned from map(). Instead, create a list from the map():
arr = list(map(int, set(input().split()))) # <-- add list() around map()
print(max(arr))
for i in arr:
print(i)
Prints (for example):
3 3 3 34 5
34
5
3
34

Related

How to obtain efficiently a matrix that adds all elements up to/from (i,j) in Julia

P is a given matrix (i.g., 2-dimensional array). The size of P is I×J.
I want to have two matrices, A and B, such that
The sizes of A and B are the same as the size of P.
Then, I wrote the following code in Julia 1.6.
f(P) = cumsum(cumsum(P,dims=1),dims=2)
g(P) = f( reverse!(reverse!(P,dims=1),dims=2))[end:-1:begin,end:-1:begin]
It works. Here is an example.
P = rand(1:6,3,4)
#3×4 Matrix{Int64}:
# 4 1 1 4
# 2 2 6 6
# 3 1 5 2
f(P)
#3×4 Matrix{Int64}:
# 4 5 6 10
# 6 9 16 26
# 9 13 25 37
g(P)
#3×4 Matrix{Int64}:
# 37 28 24 12
# 27 22 19 8
# 11 8 7 2
Does anybody have a better idea? If there is a faster method, I'd like to know.
Context
I want to expand the above functions f and g to tensors, that it, P is a tensor (i.g., d-dimensional array) whose size is I1×…×Id. I want to make two arrays, A and B, such that

How to extract vectors from a given condition matrix in Octave

I'm trying to extract a matrix with two columns. The first column is the data that I want to group into a vector, while the second column is information about the group.
A =
1 1
2 1
7 2
9 2
7 3
10 3
13 3
1 4
5 4
17 4
1 5
6 5
the result that i seek are
A1 =
1
2
A2 =
7
9
A3 =
7
10
13
A4=
1
5
17
A5 =
1
6
as an illustration, I used the eval function but it didn't give the results I wanted
Assuming that you don't actually need individually named separated variables, the following will put the values into separate cells of a cell array, each of which can be an arbitrary size and which can be then retrieved using cell index syntax. It makes used of logical indexing so that each iteration of the for loop assigns to that cell in B just the values from the first column of A that have the correct number in the second column of A.
num_cells = max (A(:,2));
B = cell (num_cells,1);
for idx = 1:max(A(:,2))
B(idx) = A((A(:,2)==idx),1);
end
B =
{
[1,1] =
1
2
[2,1] =
7
9
[3,1] =
7
10
13
[4,1] =
1
5
17
[5,1] =
1
6
}
Cell arrays are accessed a bit differently than normal numeric arrays. Array indexing (with ()) will return another cell, e.g.:
>> B(1)
ans =
{
[1,1] =
1
2
}
To get the contents of the cell so that you can work with them like any other variable, index them using {}.
>> B{1}
ans =
1
2
How it works:
Use max(A(:,2)) to find out how many array elements are going to be needed. A(:,2) uses subscript notation to indicate every value of A in column 2.
Create an empty cell array B with the right number of cells to contain the separated parts of A. This isn't strictly necessary, but with large amounts of data, things can slow down a lot if you keep adding on to the end of an array. Pre-allocating is usually better.
For each iteration of the for loop, it determines which elements in the 2nd column of A have the value matching the value of idx. This returns a logical array. For example, for the third time through the for loop, idx = 3, and:
>> A_index3 = A(:,2)==3
A_index3 =
0
0
0
0
1
1
1
0
0
0
0
0
That is a logical array of trues/falses indicating which elements equal 3. You are allowed to mix both logical and subscripts when indexing. So using this we can retrieve just those values from the first column:
A(A_index3, 1)
ans =
7
10
13
we get the same result if we do it in a single line without the A_index3 intermediate placeholder:
>> A(A(:,2)==3, 1)
ans =
7
10
13
Putting it in a for loop where 3 is replaced by the loop variable idx, and we assign the answer to the idx location in B, we get all of the values separated into different cells.

Elixir: Return value from for loop

I have a requirement for a for loop in Elixir that returns a calculated value.
Here is my simple example:
a = 0
for i <- 1..10
do
a = a + 1
IO.inspect a
end
IO.inspect a
Here is the output:
warning: variable i is unused
Untitled 15:2
2
2
2
2
2
2
2
2
2
2
1
I know that i is unused and can be used in place of a in this example, but that's not the question. The question is how do you get the for loop to return the variable a = 10?
You cannot do it this way as variables in Elixir are immutable. What your code really does is create a new a inside the for on every iteration, and does not modify the outer a at all, so the outer a remains 1, while the inner one is always 2. For this pattern of initial value + updating the value for each iteration of an enumerable, you can use Enum.reduce/3:
# This code does exactly what your code would have done in a language with mutable variables.
# a is 0 initially
a = Enum.reduce 1..10, 0, fn i, a ->
new_a = a + 1
IO.inspect new_a
# we set a to new_a, which is a + 1 on every iteration
new_a
end
# a here is the final value of a
IO.inspect a
Output:
1
2
3
4
5
6
7
8
9
10
10

how to iterate through one column matrix in matlab

I'm new to MATLAB and i'm trying to figure out how I would iterate over a matrix with only one column to count the occurrence of some number, n. For example, I would like to count how many times '1' appears in the matrix:
1
4
1
88
6
22
1
How could I make a loop that returns '3'? How would I create a loop that counts how many times some loop counter occurs (i.e. start at 0 and increment by one each loop to count how many times the counter occurs in the matrix)?
Thanks
Just use sum
>> a=[1 4 1 88 6 22 1]';
>> n=1;
>> sum(a==n)
ans =
3
a = [1 4 1 88 6 22 1];
count_n = size(a(a==n));
You wouldn't need to run a loop. You could just do it like this:
a = [ 1 4 1 88 6 22 1];
n = 1;
length(find(a(:)==n))

Print numbers in a range

I am trying to print all numbers between 1 and 50, using the following code:
[1..50].each{|n| puts n}
but the console print
[1..50]
I want to print something like this
1
2
3
4
...
50
Try the following code:
(1..50).each { |n| puts n }
The problem is that you're using [] delimiter instead of () one.
You can use [1..10] with a minor tweak:
[*1..10].each{ |i| p i }
outputs:
1
2
3
4
5
6
7
8
9
10
The * (AKA "splat") "explodes" the range into its components, which are then used to populate the array. It's similar to writing (1..10).to_a.
You can also do:
puts [*1..10]
to print the same thing.
So, try:
[*1..10].join(' ') # => "1 2 3 4 5 6 7 8 9 10"
or:
[*1..10] * ' ' # => "1 2 3 4 5 6 7 8 9 10"
To get the output you want.
The error here is that you are creating an Array object with a range as its only element.
> [1..10].size
=> 1
If you want to call methods like each on a range, you have to wrap the range in parentheses to avoid the method being called on the range's last element rather than on the range itself.
=> (1..10).each { |i| print i }
12345678910
Other ways to achieve the same:
(1..50).each { |n| print n }
1.up_to(50) { |n| print n }
50.times { |n| print n }
You can cast your range (in parentheses) to an array ([1 2 3 4 5 6... 48 49 50]) and join each item (e.g. with ' ' if you want all items in one line).
puts (1..50).to_a.join(' ')
# => 1 2 3 4 5 6 7 ... 48 49 50

Resources