Printing array of multi-digit elements in reverse order in ruby - arrays

How to print an array of elements in reverse order not just single digit number but also multi-digit numbers.
[2, 5, 6 7]
It should print the array elements in reverse order as 7 6 5 2 by following a space for each number.
I already wrote the code for this.
puts "Enter the array elements"
arr = gets.strip
arr = arr.split(' ').map(&:to_i)
x = arr.reverse_each {|f| }
z = x.join(" ")
print z.reverse
That is cool with single digit numbers, how can I reverse the multi-digit numbers in an array of inputs given by the user input like:
[45, 76, 87 ] # this should reverse the array as `87 76 45`
[556, 674, 878 ] # this should reverse the array as `878 674 556`
[8797, 7347, 9374 ] # this should reverse the array as `9374 7374 8797`

If you like one-liners:
gets.strip.split(' ').reverse.join(' ')
This will take the input 1 2 3 45 678 9 and convert it to "9 678 45 3 2 1"

Input: [8797, 7347, 9374 ]
Output: "9374 7374 8797"
arr = gets.chomp
arr = arr.split(' ').map(&:to_i)
x = arr.reverse.join(' ')
print x
Use reverse and join chained and it should return a String type that joined your reversed array.

Related

how to output on ruby, two input in one single line

I need output two different array (or range ) which both of them takes by gets.chomp, one of them a string, the other must be integer. But i dont want to transpoze or i dont want to use zip method. because when i take output one of them in a row, the other might be reverse two or more times.
name = $name
num = $num
arr = $arr
puts "num?"
num = gets.chomp.to_i
arr = []
for x in (1..num)
puts "#{x}. name? " #its can be "A" ,"B","C"
name = gets.chomp.to_sym
arr.push (name)
end
for y in (1..100)
arr.each do |z|
print y, " " , z, "\n"
end
end
# i want to outputs like this :
1 A
2 B
3 C
4 A
5 B #reverse time (%5 == 0)
6 A
7 C
8 B
9 A
10 C #reverse time (%5 ==0)
11 A
integers dont reverse anytime, but Strings must be reverse...
Thanks for help..
Rather than using a nested loop, you want to pair your integer Range with your name Array, and then loop over the resulting Array of Arrays. In Ruby we can do this using the #zip method. Also, because the name array may not fill up the entire Range, we can #cycle the name array to convert it to a repeating Enumerator (otherwise, in the example given, 3 through 5 would be paired with nil).
>> arr = ["John", "Jane"]
>> pairs = (1..5).zip(arr.cycle)
#> [[1, "John"], [2, "Jane"], [3, "John"], [4, "Jane"], [5, "John"]]
>> pairs.each { |integer, name| print "#{integer} #{name}\n" }
1 John
2 Jane
3 John
4 Jane
5 John

how to eliminate repeating integers from cell array in matlab

i have [words*sentences]matrix where sentences have integers that represent sentence numbers from a text document from this matrix i have constructed 1D array of [1*N] which shows words and in which sentences they occur number wise.
once above step is done i have taken intersection to check which words occur together in which sentences the code is as follows:
OccursTogether = cell(length(Out1));
for ii=1:length(Out1)
for jj=ii+1:length(Out1)
OccursTogether{ii,jj} = intersect(Out1{ii},Out1{jj});
end
end
celldisp(OccursTogether)
the sample output is as follows which shows 1st word occurs in sentence
{5 10 16} same word occurs with 2nd word in sentence {11 12 13} and word 1 occurs with word 3 in sentence {9 14} and so on till the Nth word:
OccursTogether{1,1} = 5 10 16
OccursTogether{2,1} = 5 12 16
OccursTogether{3,1} = 9 14
now what i want is to show output in one line based upon OccursTogether cell array without repeating sentence numbers as below:
output: {5 9 10 12 14 16}
any help would me appreciated..
If I understand correctly:
result = unique([OccursTogether{:}]);
In your example this gives
result =
5 9 10 12 14 16
Here is a way using cellfun and cell2mat. The idea is to vertically concatenate each cell form the cell array, convert it to a matrix and apply the unique function.
So the first step:
a = cellfun(#(x) x.',OccursTogether,'uni',false)
Takes each cell and transpose it, thus making a n x 1 vector. The result is a cell array containing vertical vectors:
a =
[3x1 double]
[3x1 double]
[2x1 double]
Now we can use cell2mat to convert to a numeric matrix since the dimensions will fit and finally apply unique. Otherwise that would not be possible (eg using 1x3 and 1x2 vectors).
In 1 line that looks like this:
output = unique(cell2mat(cellfun(#(x) x.',OccursTogether,'uni',false)))
output =
5
9
10
12
14
16
Hope this is what you meant! If not please tell me.

Array filtering based on cell content

I have a cell of length n where each number is a numeric array of varying length.
eg
C = { [ 1 2 3] ; [ 4 1 ] ; [ 28 5 15] }
And a 4xn numeric array
eg
A = [[ 1 2 3 4] ; [ 5 6 7 8 ] ; [ 9 10 11 12]]
I'd like to filter the numeric array A based on the content in cell C.
The filter may be to return all rows in A which have a 28 in the corresponding element in C.
ans = [ 9 10 11 12 ]
Or, the filter may be to return all rows in A which have a 1 in the first column of C or a 5 in the second column of C.
ans = [[ 1 2 3 4] ; [ 9 10 11 12]]
Hope this makes sense! It's the correlation the vectors in the cell to the main array which I'm struggling with
Cellfun makes this relatively straightforward - design the function that returns a logical vector matching your filter requirements (i.e., it maps each vector in C to a single logical scalar depending on the conditions), and make this the first input to cellfun. Your cell array is the second input. The output of this will be your nx1 "filter" vector. Then apply this along the dimension of A that has length n, and use a colon operator in the other dimension.
First one:
A(cellfun(#(x) ismember(28, x), C), :);
ans =
9 10 11 12
Second one:
A(cellfun(#(x) (x(1)==1) || (x(2)==5), C), :)
ans =
1 2 3 4
9 10 11 12

Getting Original Indices of Reordered Array

I have an array of numbers A, and I want to create a new array B representing the indicies of A after A is reordered from smallest to largest.
For example:
A = [50 10 60 90 30];
The reordered A would then be:
A = [10 30 50 60 90];
And I want to get the output:
B = [2 5 1 3 4];
These indices in B therefore correspond to the original indices in A, but are written in the order of the reordered A.
How can I do this?
Use the second output of sort:
[A_sorted, B] = sort(A);

how to convert a number array to a single string in matlab

How can i convert a numeric array to a single string, where array numbers will be separated with a comma ?
(e.g. convert A=[1 2 3] to a string '1,2,3')
Moreover, is there any way to apply the same above in case that matrix A contains variables in a for loop?
(e.g.
for i=1,10
A(i)=[1 1 i+1];
end
As variable i varies, I need to obtain a string '1,1,i+1'
thanks a lot !
There is a num2str() function
>> test =[123 124 125] % 3 element vector
test =
123 124 125
>> num2str(test) % 1 element string
ans =
123 124 125
and also a function to write ASCII delimited files
the process can easily be reversed with the str2num function, as dan pointed out
I think you need this:
for i=1:10
disp(['1,1,',num2str(i+1)])
end
Note: Try to avoid 'i' as the iteration variable.
The output:
1,1,2
1,1,3
1,1,4
1,1,5
1,1,6
1,1,7
1,1,8
1,1,9
1,1,10
1,1,11
for i=1:10
s = sprintf('%d,', A);
S{i} = s(1:end-1);
end
The function mat2str does just that:
>> A = [1 2 3];
>> mat2str(A)
ans =
[1 2 3]

Resources