Taking x number integers and putting them into an array - arrays

I need to take x amount of input lines (x is specified by the user) then put them into a 2d array. Each line contains x amount of integers separated by spaces.
For example;
Input:
3
4 3 1
6 5 2
9 7 3
I need to take that input and put them into a 2d array, how do I do this?

Assuming your numbers are separated exactly by one space:
n = int(input('enter size'))
print([[int(i) for i in input().split(' ')]
for __ in range(n)])

Suppose you have your input stored in a file named 'input.txt'
n=2 #number specified by user
with open('input.txt', 'r') as file:
result = [[int(char) for char in lines.split(' ')]for lines in file.read().splitlines()[:2*n:2] ]

Related

Take two integers a,b and print all the even numbers in between them, excluding the input integers

Take two integers a,b and print all the even numbers in between them, excluding the input integers.
Constraints:
0 <= a < b <= 100
Input:
Two integers each in a new line.
Output:
Each line in the output contains an even integer between a,b in ascending order.
Example:
Input:
3
12
Output:
4
6
8
10
I could not find it's logic.
I don't get the question that well, but here's what I got out of it:
for i in range(a + 1, b):
if i % 2 == 0:
print(i)
This loop goes through every letter between your 2 constraints (a and b), checks if its divisible by 2, and if it is (which means it's even), it prints it.

Find unique string size

Given two strings A and B of equal lengths n.
For every index 0<=i<n I can swap characters of A and B.
We need to minimize the number of unique characters in each of A and B.
if num(A) = unique characters in string A
and num(B) = unique characters in string B
after all the operations.
print max(num(A), num(B)).
eg:
1). input: A= ababa B= babab output: 1
2). input: A= abaaa B= baabb output: 2
3). input: A= abcdefb B= bfedcba output: 4
In 3). case, initially max(n(abcdefb),n(bfedcba)) = 6. You can modify the two strings such that they are "bbcdcbb" and "afedefa". String A has 3 unique characters and String B has 4 unique characters. Hence answer is 4. You cannot do better.

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.

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]

C: how to store integers by reading line by line with a random layout?

I need to read a file and store each number (int) in a variable, when it sees \n or a "-" (the minus sign means that it should store the numbers from 1 to 5 (1-5)) it needs to store it into the next variable. How should I proceed?
I was thinking of using fgets() but I can't find a way to do what I want.
The input looks like this:
0
0
5 10
4
2 4
5-10 2 3 4 6 7-9
4 3
These are x y positions.
I'd use fscanf to read one int at a time, and when it's negative, it is obviously the second part of a range. Or is -4--2 a valid input?

Resources