Concatenate

  • [deleted]Challenge Author
    + 22 comments
    import numpy as np
    a, b, c = map(int,input().split())
    arrA = np.array([input().split() for _ in range(a)],int)
    arrB = np.array([input().split() for _ in range(b)],int)
    print(np.concatenate((arrA, arrB), axis = 0))
    
    • + 2 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.

      • + 0 comments

        here is problem solution in python programming. https://programs.programmingoneonone.com/2021/02/hackerrank-concatenate-solution-python.html

      • + 0 comments

        UPDATED SOLUTION IS HERE

        https://www.thecscience.com/2021/08/hackerrank-concatenate-in-python-problem-solution.html

    • + 3 comments

      what does _ means in for loop?

      • [deleted]Challenge Author
        + 1 comment

        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.

        • + 2 comments

          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.

          • + 0 comments

            it is taken by user's key-in.

          • + 0 comments

            It is an input by user.

      • + 0 comments

        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.

      • + 0 comments

        It means you are not going to use that.

    • + 2 comments

      While taking the input for the array where does the number of columns parameter ie variable c considered?

      • + 1 comment

        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

        • + 0 comments

          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.

      • + 0 comments

        It's never needed because when we use split() it automatically takes the number of inputs given. So we can easily ignore that.

    • + 0 comments

      Thanks...It worked! :>

    • + 2 comments

      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

      • + 0 comments

        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.

      • + 0 comments

        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.

    • + 1 comment

      nyc way to solve this problem!!!

      • [deleted]Challenge Author
        + 0 comments

        thank you :)

    • + 1 comment

      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 ,.

      • + 0 comments

        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.

    • + 1 comment

      Quick question , Why we have Variable c when it did not used anywhere ,Sorry if its stupid question pretty new to python

      • + 2 comments

        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.

        • + 0 comments

          thanks got it .

        • + 0 comments

          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

    • + 0 comments

      so is there no use of column no.?

    • + 1 comment

      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

      • + 0 comments

        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().

    • + 0 comments

      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 :)

    • + 1 comment

      arrA = np.array([input().split() for _ in range(a)],int)

      what does 'int' do here??

      • + 0 comments

        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)

    • + 0 comments

      why am i getting invalid syntax error using the same code?

    • + 0 comments

      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))

    • + 0 comments

      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.

    • + 0 comments

      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)

    • + 0 comments

      is there any use of c?

    • + 0 comments

      you are not using C why ?

    • + 0 comments

      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))