We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
it means that you are looping in the determined range but you are not going to use the index or the object during the loop, so you simply loop a certain number of times e.g range(a), but in this case only to read the input from the keyboard,hope i explained it sufficiently.
I don't understand how to create the input that is being taken by the map(input(), ) function? Is it in a file or is it taken from the console by the user entering it? I'm trying to use my pycharm ide to practice the problems.
It is a varible name. You can use i or j instead. Because it wouldn't be used in the loop and only be used to maintain the loop, using "-" instead of "i" or "j" is a good idea.
It is never needed because the input already has the right number (c) of elements per line. Therefore input().split() just returns a list of c elements
Hi, Thanks for this explanation. The test cases are designed so as to only take c number of for the columns elements as input. However, if an user were to enter more space separated no. of inputs than c, wouldn't that be an error on our part in the code? Correct me if I'm missing something.
why we cant print(np.concatentate(arrA,arrB)) why there is need of extra bracket it is showing error only integer scalar arrays can be converted to a scalar index
This error message suggests that a function or operation is expecting an integer scalar index (single integer) as an index, but is instead receiving an array or a non-integer value. This can occur when attempting to index or slice an array with a non-integer or non-scalar value. To resolve this error, ensure that the index being used is a single integer and not an array or non-integer value. If working with arrays, consider using integer indexing or slicing methods to access specific elements or subsets of the array.
You can use map but because we are using numpy anyway it is easier (or at least shorter) to just cast the array elements to int in the same step that you turn the array into a numpy.array.
Why does HackerRank give us c (actually it is called p in the taks)?
Because maybe if you used something else than input().split() you would need it.
Why do we save something to c?
input().split() gives back three output elements and we have to save all of them to something. One could also save them to a, b, _ which is not much more than a visual hint that the third element is never used again.
HackerRank gives the same definition for all the langauges. In Python we are lucky enough, it's easy to get this number with the help of input.split(). No for all the languages it's the case
can you explain it please,
"arrA = np.array([input().split() for _ in range(a)],int) " <-- how this line come to know that column size is 2, no where you used value of "c". second the input is in continuous and separated by space. how it managed to take in two input arrays. expecting reply, please
The posted solution isn't mine but it looks like
it21208 isn't bothering with c because the information is actually redundant. In the given data set, all the input lines representing rows in arrays A and B have 2 columns, so this will automatically be the length of the per-line arrays returned by split() and the width of the matrix created from those by np.array().
You do not need axis = 0 in the last line, it would be shorter like this print(np.concatenate((arrA, arrB))) because it connatenates by default by axis 0 :)
It converts items in the provided list as a first argument to have data type that you specified as a second argument. In this case, np.array method takes a list of strings (first argument) and returns a nmpy array of ints (second argument)
Concatenate
You are viewing a single comment's thread. Return to all comments →
For intelligibility I think it would be best to use the variable names used in the question. i.e. n, m, p instead of a, b, c.
For this question it's fairly obvious, but in future scenarios it may not be.
here is problem solution in python programming. https://programs.programmingoneonone.com/2021/02/hackerrank-concatenate-solution-python.html
UPDATED SOLUTION IS HERE
https://www.thecscience.com/2021/08/hackerrank-concatenate-in-python-problem-solution.html
what does _ means in for loop?
it means that you are looping in the determined range but you are not going to use the index or the object during the loop, so you simply loop a certain number of times e.g range(a), but in this case only to read the input from the keyboard,hope i explained it sufficiently.
I don't understand how to create the input that is being taken by the map(input(), ) function? Is it in a file or is it taken from the console by the user entering it? I'm trying to use my pycharm ide to practice the problems.
it is taken by user's key-in.
It is an input by user.
It is a varible name. You can use i or j instead. Because it wouldn't be used in the loop and only be used to maintain the loop, using "-" instead of "i" or "j" is a good idea.
It means you are not going to use that.
While taking the input for the array where does the number of columns parameter ie variable c considered?
It is never needed because the input already has the right number (c) of elements per line. Therefore input().split() just returns a list of c elements
Hi, Thanks for this explanation. The test cases are designed so as to only take c number of for the columns elements as input. However, if an user were to enter more space separated no. of inputs than c, wouldn't that be an error on our part in the code? Correct me if I'm missing something.
It's never needed because when we use split() it automatically takes the number of inputs given. So we can easily ignore that.
Thanks...It worked! :>
why we cant print(np.concatentate(arrA,arrB)) why there is need of extra bracket it is showing error only integer scalar arrays can be converted to a scalar index
np.concatenate() expects a single argument, a tuple containing all the arrays to glue together.
If you just pass A and B, it looks instead like two array arguments.
putting A and B in an extra set of parentheses creates a tuple containing them.
This error message suggests that a function or operation is expecting an integer scalar index (single integer) as an index, but is instead receiving an array or a non-integer value. This can occur when attempting to index or slice an array with a non-integer or non-scalar value. To resolve this error, ensure that the index being used is a single integer and not an array or non-integer value. If working with arrays, consider using integer indexing or slicing methods to access specific elements or subsets of the array.
nyc way to solve this problem!!!
thank you :)
Hii,can u please explain to me why we cannot use map to convert it into int and how that int is working which is after ,.
You can use map but because we are using numpy anyway it is easier (or at least shorter) to just cast the array elements to int in the same step that you turn the array into a numpy.array.
Quick question , Why we have Variable c when it did not used anywhere ,Sorry if its stupid question pretty new to python
Why does HackerRank give us c (actually it is called p in the taks)? Because maybe if you used something else than
input().split()
you would need it.Why do we save something to c?
input().split()
gives back three output elements and we have to save all of them to something. One could also save them toa, b, _
which is not much more than a visual hint that the third element is never used again.thanks got it .
HackerRank gives the same definition for all the langauges. In Python we are lucky enough, it's easy to get this number with the help of input.split(). No for all the languages it's the case
so is there no use of column no.?
can you explain it please, "arrA = np.array([input().split() for _ in range(a)],int) " <-- how this line come to know that column size is 2, no where you used value of "c". second the input is in continuous and separated by space. how it managed to take in two input arrays. expecting reply, please
The posted solution isn't mine but it looks like it21208 isn't bothering with c because the information is actually redundant. In the given data set, all the input lines representing rows in arrays
A
andB
have 2 columns, so this will automatically be the length of the per-line arrays returned bysplit()
and the width of the matrix created from those bynp.array()
.You do not need axis = 0 in the last line, it would be shorter like this print(np.concatenate((arrA, arrB))) because it connatenates by default by axis 0 :)
arrA = np.array([input().split() for _ in range(a)],int)
what does 'int' do here??
It converts items in the provided list as a first argument to have data type that you specified as a second argument. In this case, np.array method takes a list of strings (first argument) and returns a nmpy array of ints (second argument)
why am i getting invalid syntax error using the same code?
from numpy import array,concatenate
N,M,P = map(int,input().split())
sa =[list(map(int,input().split())) for _ in range(N)]
ta = [list(map(int,input().split())) for _ in range(M)] print(concatenate((array(sa),array(ta)),axis=0))
I don't think p (or in this case c) was used at all. I didn't even understand what p did in the original question.
Did this, open to suggestions.
n, m, p = map(int, input().split())
arr = numpy.array([input().strip().split() for _ in range((n+m))], int) print(arr)
is there any use of c?
you are not using C why ?
x = input().split(' ') a = [ input().split(' ') for i in range(0,(int(x[0])+int(x[1])))] print(numpy.array(a,dtype=numpy.int).reshape(-1,2))