Array Mathematics

  • + 7 comments
    import numpy as np
    N, M = list(map(int, input().split()))
    
    a1 = np.array([input().split() for _ in range(N)], int)
    a2 = np.array([input().split() for _ in range(N)], int)
    
    print(*[eval('a1'+i+'a2') for i in ['+','-','*','//','%','**']], sep='\n')
    
    • + 2 comments

      Hey, please could you explain why a1 and a2 are different? How do 2 different arrays come out of the same 2 lines of code?

      Thanks

      • + 1 comment

        The value you obtain from input() depends on what the user types in once the code is active. In our case, hackerrank automatically types in the values for us. In the first example given by this problem, the first line of input that hackerrank automatically puts in when input() is called is '1 4' (which is converted to [1,4] using the split() and map() commands). The next time input() is called, it will ask for another submission, which hackerrank will automatically put in '1 2 3 4' for this example, and so on and so forth.

        In other words, a1 = np.array([input().split() for _ in range(N)], int) calls the first N inputs, creating an array (in the example, N=1, so it only calls '1 2 3 4'). a2 = np.array([input().split() for _ in range(N)], int) calls the next N inputs, which is why it creates a different array (in this example, it would return '5 6 7 8' since that's the next input and N is still = 1).

        • + 0 comments

          In this case, N = 1 and M = 4.

          Could you plase explain why we have looped over N and not M? From my understanding, a1 and a2 need to be 2 arrays with 4 elements each. This means we should loop over M.

      • + 0 comments

        The values the user or the computer will put in this case will be different to a1 and a2.

    • + 3 comments

      what is this sep='\n' mean??

      • + 0 comments

        '\n' is the new line escape sequence, and it's specified as the sep argument of the print function -- this is the character/sequences that separates elements of the output.

        Adding sep='\n' to the print statement prints each element of the output on a new line.

      • + 0 comments

        If you're ever unsure of what a function is doing in example code you should look at the documentation. Print is a built-in function of python so you can simply google "python documentation print" :

        https://docs.python.org/3/library/functions.html#print

        As seen in the link, "sep", or "separator", is the parameter that specifies what separate the series of outputs by. The default is ' ' (a space), which will be used when you specific.

        In this example, "hi" and "there" are outputs. print("hi", "there", sep="x") => hixthere

        "\n" means new line, so the outputs are each on a new line.

      • + 0 comments

        sep='\n' means it will separate each input by by a newline character(escape sequence '\n').

    • + 1 comment

      WOW!! This is amazing.

    • + 0 comments

      I am not getting why we have to set the range(N) as N=1 Please can someone explain in detail?

    • + 0 comments

      why range N, INSTEAD OF M?

    • + 0 comments

      *[eval('a1'+i+'a2') could you explain what does this means?

    • + 0 comments

      Loved the way you used eval in this program.