You are given a space separated list of numbers.
Your task is to print a reversed NumPy array with the element type float.
Input Format
A single line of input containing space separated numbers.
Output Format
Print the reverse NumPy array with type float.
Sample Input
1 2 3 4 -8 -10
Sample Output
[-10. -8. 4. 3. 2. 1.]
# The solution for the above problem task is give by me !!!
import numpy
def arrays(arr):
arr1 = numpy.array(arr, dtype = float)
arr2 = numpy.array(arr[::-1], dtype = float)
return arr2
arr = input().strip().split(' ')
result = arrays(arr)
print(result)
1 2 3 4 -8 -10 [-10. -8. 4. 3. 2. 1.]
You are given a space separated list of nine integers. Your task is to convert this list into a X NumPy array.
Input Format
A single line of input containing space separated integers.
Output Format
Print the X NumPy array.
Sample Input
1 2 3 4 5 6 7 8 9
Sample Output
[ [ 1 2 3 ]
[ 4 5 6 ]
[ 7 8 9 ] ]
# The solution for the above problem task is give by me !!!
import numpy
def arrays(arr):
arr1 = numpy.array(arr, dtype = int)
arr2 = arr1.reshape(3, 3)
return arr2
arr = input().strip().split(' ')
result = arrays(arr)
print(result)
1 2 3 4 5 6 7 8 9 [[1 2 3] [4 5 6] [7 8 9]]
You are given the shape of the array in the form of space-separated integers, each integer representing the size of different dimensions, your task is to print an array of the given shape and integer type using the tools numpy.zeros and numpy.ones.
Input Format
A single line containing the space-separated integers.
Output Format
First, print the array using the numpy.zeros tool and then print the array with the numpy.ones tool.
Sample Input 0
3 3 3
Sample Output 0
[[[0 0 0]
[0 0 0]
[0 0 0]]
[[0 0 0]
[0 0 0]
[0 0 0]]
[[0 0 0]
[0 0 0]
[0 0 0]]]
[[[1 1 1]
[1 1 1]
[1 1 1]]
[[1 1 1]
[1 1 1]
[1 1 1]]
[[1 1 1]
[1 1 1]
[1 1 1]]]
# to be frank there is only one part that I got the solution for, that is how to return multiple values. And the logic part is mine.
# https://www.geeksforgeeks.org/g-fact-41-multiple-return-values-in-python/
import numpy
def arrays(arr):
arr1 = numpy.array(arr, dtype = int)
arr2 = numpy.zeros(arr1, dtype = int)
arr3 = numpy.ones(arr1, dtype = int)
return arr2, arr3
arr = input().strip().split(' ')
arr2, arr3 = arrays(arr)
print(arr2)
print(arr3)
3 3 3 [[[0 0 0] [0 0 0] [0 0 0]] [[0 0 0] [0 0 0] [0 0 0]] [[0 0 0] [0 0 0] [0 0 0]]] [[[1 1 1] [1 1 1] [1 1 1]] [[1 1 1] [1 1 1] [1 1 1]] [[1 1 1] [1 1 1] [1 1 1]]]
You are given two integer arrays, and of dimensions X.
Your task is to perform the following operations:
Add ( + ) Subtract ( - ) Multiply ( * ) Integer Division ( / ) Mod ( % ) Power ( ** )
Input Format
The first line contains two space separated integers, and . The next lines contains space separated integers of array . The following lines contains space separated integers of array .
Output Format
Print the result of each operation in the given order under Task.
Sample Input
1 4
1 2 3 4
5 6 7 8
Sample Output
[[ 6 8 10 12]]
[[-4 -4 -4 -4]]
[[ 5 12 21 32]]
[[0 0 0 0]]
[[1 2 3 4]]
[[ 1 64 2187 65536]]
Use // for division in Python 3.
Apparently I got only 10.00 out of 20.00 because there was some issue with the looping.
import numpy
def arrays(arr, arr1, arr2):
arr = numpy.array(arr, dtype = int)
arr1 = numpy.array([arr1], dtype = int)
arr2 = numpy.array([arr2], dtype = int)
add = numpy.add(arr1,arr2)
sub = numpy.subtract(arr1, arr2)
mul = numpy.multiply(arr1, arr2)
div = numpy.floor_divide(arr1, arr2)
mod = numpy.mod(arr1, arr2)
power = numpy.power(arr1, arr2)
return add,sub,mul,div,mod,power
arr = input().strip().split(' ')
arr1 = input().strip().split(' ')
arr2 = input().strip().split(' ')
add,sub,mul,div,mod,power = arrays(arr, arr1, arr2)
print(add)
print(sub)
print(mul)
print(div)
print(mod)
print(power)
1 4 1 2 3 4 5 6 7 8 [[ 6 8 10 12]] [[-4 -4 -4 -4]] [[ 5 12 21 32]] [[0 0 0 0]] [[1 2 3 4]] [[ 1 64 2187 65536]]
Input Format
A single line of input containing the space separated elements of array .
Output Format
On the first line, print the of A. On the second line, print the of A. On the third line, print the of A.
Sample Input
1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 9.9
Sample Output
[ 1. 2. 3. 4. 5. 6. 7. 8. 9.]
[ 2. 3. 4. 5. 6. 7. 8. 9. 10.]
[ 1. 2. 3. 4. 6. 7. 8. 9. 10.]
But I had to cheat in this example, because Hackerank has lost its mind in this exercise, because its asking for weired space throughout,
The exercise, which is irrelevant. So I had to sneek in one of the discussion foroums, and get the spacing problem corrected.
import numpy
numpy.set_printoptions(sign=' ')
def arrays(arr):
arr1 = numpy.array(arr, dtype = float)
floor = numpy.floor(arr1)
ceil = numpy.ceil(arr1)
rint = numpy.rint(arr1)
return floor, ceil, rint
arr = input().strip().split(' ')
floor, ceil, rint = arrays(arr)
print(floor)
print(ceil)
print(rint)
1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 9.9 [ 1. 2. 3. 4. 5. 6. 7. 8. 9.] [ 2. 3. 4. 5. 6. 7. 8. 9. 10.] [ 1. 2. 3. 4. 6. 7. 8. 9. 10.]
I would like to tell you guys one thing, is that I kind cheated for a reason, I was not geting how to take the input of the arrays uding for loop, but multiple input using loops, so I had take the help of the discussion forums.
You are given a 2-D array of size X. Your task is to find:
The mean along axis The var along axis The std along axis
Input Format
The first line contains the space separated values of and . The next lines contains space separated integers.
Output Format
First, print the mean. Second, print the var. Third, print the std.
Sample Input
2 2 1 2 3 4
Sample Output
[ 1.5 3.5]
[ 1. 1.]
1.11803398875
import numpy
numpy.set_printoptions(legacy='1.13')
N, M = map(int, input().split())
A = numpy.array([input().split() for i in range(N)], dtype =int)
print(numpy.mean(A, axis =1))
print(numpy.var(A, axis =0))
print(numpy.std(A, axis = None))
2 2 1 2 3 4 [ 1.5 3.5] [ 1. 1.] 1.11803398875
You are given a 2-D array with dimensions X. Your task is to perform the tool over axis and then find the of that result.
Input Format
The first line of input contains space separated values of and . The next lines contains space separated integers.
Output Format
Compute the sum along axis . Then, print the product of that sum.
Sample Input
2 2 1 2 3 4
Sample Output
24
Explanation
The sum along axis 0 = [4 6]
The product of this sum = 24
import numpy
N, M = map(int, input().split())
array = numpy.array([input().split() for i in range(N)], int)
print(numpy.prod(numpy.sum(array, axis = 0), axis = None))
# Hint first calculate the product and then the sum,
# if you do other way you cannot complete the other two test cases.(I g
2 2 1 2 3 4 24
Your task is to print an array of size X with its main diagonal elements as 's and 's everywhere else.
Input Format
N denotes rows M demotes coloumns
Output Format
Print the desired N X M array.
Sample Input
3 3
Sample Output
[[ 1. 0. 0.]
[ 0. 1. 0.]
[ 0. 0. 1.]]
import numpy
numpy.set_printoptions(sign=' ')
N, M = map(int, input().split())
eye = numpy.eye(N, M)
print(eye)
3 3 [[ 1. 0. 0.] [ 0. 1. 0.] [ 0. 0. 1.]]
You are given a square matrix with dimensions X. Your task is to find the determinant.
Input Format
The first line contains the integer . The next lines contains the space separated elements of array .
Output Format
Print the determinant of .
Sample Input
2 1.1 1.1 1.1 1.1
Sample Output
0.0
import numpy
numpy.set_printoptions(legacy='1.13')
N = int(input())
A = numpy.array([input().split() for i in range(N)], float)
print(numpy.linalg.det(A))
2 1.1 1.1 1.1 1.1 0.0
You are given two arrays: and . Your task is to compute their inner and outer product.
Input Format
The first line contains the space separated elements of array . The second line contains the space separated elements of array .
Output Format
First, print the inner product. Second, print the outer product.
Sample Input
0 1 2 3
Sample Output
3
[[0 0]
[2 3]]
import numpy
a = numpy.array(input().split() , int)
b = numpy.array(input().split() , int)
print("---------------------------")
print(numpy.inner(a, b))
print("---------------------------")
print(numpy.outer(a, b))
0 1 2 3 --------------------------- 3 --------------------------- [[0 0] [2 3]]
You are given the coefficients of a polynomial P. Your task is to find the value of P at point x.
Input Format
The first line contains the space separated value of the coefficients in P. The second line contains the value of x.
Output Format
Print the desired value.
Sample Input
1.1 2 3
0
Sample Output
3.0
import numpy
m = numpy.array(input().split(), float)
n = float(input())
print(numpy.polyval(m, n))
1.1 2 3 0 3.0
You are given two arrays A and B. Both have dimensions of X.
Your task is to compute their matrix product.
Input Format
The first line contains the integer N. The next lines N contains N space separated integers of array A . The following N lines contains N space separated integers of array B .
Output Format
Print the matrix multiplication of and .
Sample Input
2
1 2
3 4
1 2
3 4
Sample Output
[[ 7 10]
[15 22]]
import numpy
N = int(input())
A = numpy.array([input().split() for i in range(N)], dtype =int)
B = numpy.array([input().split() for i in range(N)], dtype =int)
C = numpy.dot(A,B)
print(C)
2 1 2 3 4 1 2 3 4 [[ 7 10] [15 22]]
You are given two integer arrays of size N X P and M X P (N & M are rows, and P is the column). Your task is to concatenate the arrays along axis 0.
Input Format
The first line contains space separated integers N, M and P. The next N lines contains the space separated elements of the P columns. After that, the next M lines contains the space separated elements of the P columns.
Output Format
Print the concatenated array of size (N+M)XP.
Sample Input
4 3 2
1 2
1 2
1 2
1 2
3 4
3 4
3 4
Sample Output
[[1 2]
[1 2]
[1 2]
[1 2]
[3 4]
[3 4]
[3 4]]
import numpy
X, Y, Z = map(int, input().split())
array = numpy.array([input().split() for i in range(X+Y)], int)
print(array)
4 3 2 1 2 1 2 1 2 1 2 3 4 3 4 3 4 [[1 2] [1 2] [1 2] [1 2] [3 4] [3 4] [3 4]]
You are given a N X M integer array matrix with space separated elements (N = rows and M = columns). Your task is to print the transpose and flatten results.
Input Format
The first line contains the space separated values of N and M. The next lines contains the space separated elements of M columns.
Output Format
First, print the transpose array and then print the flatten.
Sample Input
2 2
1 2
3 4
Sample Output
[[1 3]
[2 4]]
[1 2 3 4]
import numpy
n, m = map(int, input().split())
array = numpy.array([input().strip().split() for _ in range(n)], int)
print (array.transpose())
print (array.flatten())
2 2 1 2 3 4 [[1 3] [2 4]] [1 2 3 4]
You are given a 2-D array with dimensions N X M. Your task is to perform the min function over axis 1 and then find the max of that.
Input Format
The first line of input contains the space separated values of N and M . The next N lines contains M space separated integers.
Output Format
Compute the min along axis 1 and then print the max of that result.
Sample Input
4 2
2 5
3 7
1 3
4 0
Sample Output
3
Explanation
The min along axis 1 = [2, 3, 1, 0]
The max of [2, 3, 1, 0] = 3
import numpy
N, M = map(int, input().split())
A = numpy.array([input().split() for i in range(N)],dtype = int)
print(numpy.max(numpy.min(A, axis=1), axis=0))
4 2 2 5 3 7 1 3 4 0 3