{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "HackerRanK_Exercises.ipynb",
"provenance": [],
"collapsed_sections": [],
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
""
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "emFFMHtc8zOD",
"colab_type": "text"
},
"source": [
"# HackerRank complete exercise problems solved."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "CutEKjWCyFGN",
"colab_type": "text"
},
"source": [
"![alt text](https://miro.medium.com/max/672/1*_64-aCeFjiMDxFZbu4jB-g.png)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "oD6P1G-Z88SC",
"colab_type": "text"
},
"source": [
"## Task 1: Arrays\n",
"\n",
"You are given a space separated list of numbers. \n",
"\n",
"Your task is to print a reversed NumPy array with the element type float.\n",
"\n",
"**Input Format**\n",
"\n",
"A single line of input containing space separated numbers.\n",
"\n",
"**Output Format**\n",
"\n",
"Print the reverse NumPy array with type float.\n",
"\n",
"**Sample Input**\n",
"\n",
"1 2 3 4 -8 -10\n",
"\n",
"**Sample Output**\n",
"\n",
"[-10. -8. 4. 3. 2. 1.] "
]
},
{
"cell_type": "code",
"metadata": {
"id": "BbBhxQbO8i9s",
"colab_type": "code",
"outputId": "2d3acbdb-55cb-43aa-8dcd-caf6f5a564d8",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 52
}
},
"source": [
"# The solution for the above problem task is give by me !!!\n",
"\n",
"import numpy\n",
"\n",
"def arrays(arr):\n",
" arr1 = numpy.array(arr, dtype = float)\n",
" arr2 = numpy.array(arr[::-1], dtype = float)\n",
" return arr2\n",
"\n",
"arr = input().strip().split(' ')\n",
"result = arrays(arr)\n",
"print(result)"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"1 2 3 4 -8 -10\n",
"[-10. -8. 4. 3. 2. 1.]\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "KACMSNmmxUjQ",
"colab_type": "text"
},
"source": [
"\n",
"\n",
"---\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "v2vbLUFL9fC7",
"colab_type": "text"
},
"source": [
"## Task 2: Shape and Reshape\n",
"\n",
"You are given a space separated list of nine integers. Your task is to convert this list into a X NumPy array.\n",
"\n",
"**Input Format**\n",
"\n",
"A single line of input containing space separated integers.\n",
"\n",
"**Output Format**\n",
"\n",
"Print the X NumPy array.\n",
"\n",
"**Sample Input**\n",
"\n",
"1 2 3 4 5 6 7 8 9\n",
"\n",
"**Sample Output**\n",
"\n",
"[ [ 1 2 3 ]\n",
"\n",
"[ 4 5 6 ]\n",
"\n",
"[ 7 8 9 ] ]"
]
},
{
"cell_type": "code",
"metadata": {
"id": "uoZRNBOM90nF",
"colab_type": "code",
"outputId": "3d6e4bdd-3269-48c7-936e-cde974fb6544",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 87
}
},
"source": [
"# The solution for the above problem task is give by me !!!\n",
"\n",
"import numpy\n",
"\n",
"def arrays(arr):\n",
" arr1 = numpy.array(arr, dtype = int)\n",
" arr2 = arr1.reshape(3, 3)\n",
" return arr2\n",
"\n",
"arr = input().strip().split(' ')\n",
"result = arrays(arr)\n",
"print(result)"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"1 2 3 4 5 6 7 8 9\n",
"[[1 2 3]\n",
" [4 5 6]\n",
" [7 8 9]]\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "mBJRTdJ_xZLw",
"colab_type": "text"
},
"source": [
"\n",
"\n",
"---\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "g6LHiXOC-FrS",
"colab_type": "text"
},
"source": [
"## Task 3 : Zeros and Ones\n",
"\n",
"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.\n",
"\n",
"**Input Format**\n",
"\n",
"A single line containing the space-separated integers.\n",
"\n",
"**Output Format**\n",
"\n",
"First, print the array using the numpy.zeros tool and then print the array with the numpy.ones tool.\n",
"\n",
"**Sample Input 0**\n",
"\n",
"3 3 3\n",
"\n",
"**Sample Output 0**\n",
"\n",
"[[[0 0 0]\n",
"\n",
" [0 0 0]\n",
" \n",
" [0 0 0]]\n",
"\n",
"\n",
"---\n",
"\n",
"\n",
"\n",
" [[0 0 0]\n",
" \n",
" [0 0 0]\n",
" \n",
" [0 0 0]]\n",
" \n",
"\n",
"---\n",
"\n",
"\n",
"\n",
" [[0 0 0]\n",
" \n",
" [0 0 0]\n",
" \n",
" [0 0 0]]]\n",
" \n",
" \n",
"\n",
"---\n",
"\n",
"\n",
" \n",
"[[[1 1 1]\n",
"\n",
" [1 1 1]\n",
" \n",
" [1 1 1]]\n",
" \n",
"\n",
"---\n",
"\n",
"\n",
"\n",
" [[1 1 1]\n",
" \n",
" [1 1 1]\n",
" \n",
" [1 1 1]]\n",
" \n",
"\n",
"\n",
"---\n",
"\n",
"\n",
" [[1 1 1]\n",
" \n",
" [1 1 1]\n",
" \n",
" [1 1 1]]]"
]
},
{
"cell_type": "code",
"metadata": {
"id": "Wmwottph-jiV",
"colab_type": "code",
"outputId": "07ac8127-779d-4d1c-9a30-f95f8f64accf",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 416
}
},
"source": [
"# 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.\n",
"# https://www.geeksforgeeks.org/g-fact-41-multiple-return-values-in-python/\n",
"\n",
"import numpy\n",
"\n",
"def arrays(arr):\n",
" arr1 = numpy.array(arr, dtype = int)\n",
" arr2 = numpy.zeros(arr1, dtype = int)\n",
" arr3 = numpy.ones(arr1, dtype = int)\n",
" return arr2, arr3\n",
"\n",
"arr = input().strip().split(' ')\n",
"arr2, arr3 = arrays(arr)\n",
"\n",
"print(arr2)\n",
"print(arr3)"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"3 3 3\n",
"[[[0 0 0]\n",
" [0 0 0]\n",
" [0 0 0]]\n",
"\n",
" [[0 0 0]\n",
" [0 0 0]\n",
" [0 0 0]]\n",
"\n",
" [[0 0 0]\n",
" [0 0 0]\n",
" [0 0 0]]]\n",
"[[[1 1 1]\n",
" [1 1 1]\n",
" [1 1 1]]\n",
"\n",
" [[1 1 1]\n",
" [1 1 1]\n",
" [1 1 1]]\n",
"\n",
" [[1 1 1]\n",
" [1 1 1]\n",
" [1 1 1]]]\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "xPLZ2BbUxdps",
"colab_type": "text"
},
"source": [
"\n",
"\n",
"---\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Kk48_89v-pHW",
"colab_type": "text"
},
"source": [
"## Task 4 : Array Mathematics\n",
"\n",
"You are given two integer arrays, and of dimensions X. \n",
"\n",
"Your task is to perform the following operations:\n",
"\n",
"Add ( + )\n",
"Subtract ( - )\n",
"Multiply ( * )\n",
"Integer Division ( / )\n",
"Mod ( % )\n",
"Power ( ** )\n",
"\n",
"**Input Format**\n",
"\n",
"The first line contains two space separated integers, and . \n",
"The next lines contains space separated integers of array . \n",
"The following lines contains space separated integers of array .\n",
"\n",
"**Output Format**\n",
"\n",
"Print the result of each operation in the given order under Task.\n",
"\n",
"**Sample Input**\n",
"\n",
"1 4\n",
"\n",
"1 2 3 4\n",
"\n",
"5 6 7 8\n",
"\n",
"**Sample Output**\n",
"\n",
"[[ 6 8 10 12]]\n",
"\n",
"[[-4 -4 -4 -4]]\n",
"\n",
"[[ 5 12 21 32]]\n",
"\n",
"[[0 0 0 0]]\n",
"\n",
"[[1 2 3 4]]\n",
"\n",
"[[ 1 64 2187 65536]] \n",
"\n",
"Use // for division in Python 3.\n",
"\n",
"**Apparently I got only 10.00 out of 20.00 because there was some issue with the looping**."
]
},
{
"cell_type": "code",
"metadata": {
"id": "mJSa-vhh--RZ",
"colab_type": "code",
"outputId": "3199e913-01f4-4bc1-8a86-13a5da016435",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 173
}
},
"source": [
"import numpy\n",
"def arrays(arr, arr1, arr2):\n",
" arr = numpy.array(arr, dtype = int)\n",
" arr1 = numpy.array([arr1], dtype = int)\n",
" arr2 = numpy.array([arr2], dtype = int)\n",
"\n",
" add = numpy.add(arr1,arr2)\n",
" sub = numpy.subtract(arr1, arr2)\n",
" mul = numpy.multiply(arr1, arr2)\n",
" div = numpy.floor_divide(arr1, arr2)\n",
" mod = numpy.mod(arr1, arr2)\n",
" power = numpy.power(arr1, arr2)\n",
" return add,sub,mul,div,mod,power\n",
"\n",
"arr = input().strip().split(' ')\n",
"\n",
"arr1 = input().strip().split(' ')\n",
"arr2 = input().strip().split(' ')\n",
"add,sub,mul,div,mod,power = arrays(arr, arr1, arr2)\n",
"print(add)\n",
"print(sub)\n",
"print(mul)\n",
"print(div)\n",
"print(mod)\n",
"print(power)"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"1 4\n",
"1 2 3 4\n",
"5 6 7 8\n",
"[[ 6 8 10 12]]\n",
"[[-4 -4 -4 -4]]\n",
"[[ 5 12 21 32]]\n",
"[[0 0 0 0]]\n",
"[[1 2 3 4]]\n",
"[[ 1 64 2187 65536]]\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "lNA1ASjfxiY_",
"colab_type": "text"
},
"source": [
"\n",
"\n",
"---\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "U4rAnvBo_NgN",
"colab_type": "text"
},
"source": [
"## Task 5 : Floor, Ceil and Rint\n",
"\n",
"**Input Format**\n",
"\n",
"A single line of input containing the space separated elements of array .\n",
"\n",
"**Output Format**\n",
"\n",
"On the first line, print the of A. \n",
"On the second line, print the of A. \n",
"On the third line, print the of A.\n",
"\n",
"**Sample Input**\n",
"\n",
"1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 9.9 \n",
"\n",
"**Sample Output**\n",
"\n",
"[ 1. 2. 3. 4. 5. 6. 7. 8. 9.]\n",
"\n",
"[ 2. 3. 4. 5. 6. 7. 8. 9. 10.]\n",
"\n",
"[ 1. 2. 3. 4. 6. 7. 8. 9. 10.]\n",
"\n",
"**But I had to cheat in this example, because Hackerank has lost its mind in this exercise, because its asking for weired space throughout,**\n",
"\n",
"**The exercise, which is irrelevant. So I had to sneek in one of the discussion foroums, and get the spacing problem corrected.**"
]
},
{
"cell_type": "code",
"metadata": {
"id": "vlbcI3pe_bpm",
"colab_type": "code",
"outputId": "725dce4c-ac13-4a46-f075-164c4dfa61fc",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 87
}
},
"source": [
"import numpy\n",
"numpy.set_printoptions(sign=' ')\n",
"def arrays(arr):\n",
" arr1 = numpy.array(arr, dtype = float)\n",
"\n",
" floor = numpy.floor(arr1)\n",
" ceil = numpy.ceil(arr1)\n",
" rint = numpy.rint(arr1)\n",
"\n",
" return floor, ceil, rint \n",
"arr = input().strip().split(' ')\n",
"floor, ceil, rint = arrays(arr)\n",
"print(floor)\n",
"print(ceil)\n",
"print(rint)"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 9.9\n",
"[ 1. 2. 3. 4. 5. 6. 7. 8. 9.]\n",
"[ 2. 3. 4. 5. 6. 7. 8. 9. 10.]\n",
"[ 1. 2. 3. 4. 6. 7. 8. 9. 10.]\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "24yVDTdDxlZn",
"colab_type": "text"
},
"source": [
"\n",
"\n",
"---\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "5j3i_HPZ_fWE",
"colab_type": "text"
},
"source": [
"## Task 6 : Mean, Var, and Std\n",
" \n",
"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,\n",
"but multiple input using loops, so I had take the help of the discussion forums.\n",
"\n",
"\n",
"You are given a 2-D array of size X. \n",
"Your task is to find:\n",
"\n",
"The mean along axis \n",
"The var along axis \n",
"The std along axis \n",
"\n",
"**Input Format**\n",
"\n",
"The first line contains the space separated values of and . \n",
"The next lines contains space separated integers.\n",
"\n",
"**Output Format**\n",
"\n",
"First, print the mean. \n",
"Second, print the var. \n",
"Third, print the std.\n",
"\n",
"**Sample Input**\n",
"\n",
"2 2\n",
"1 2\n",
"3 4\n",
"\n",
"**Sample Output**\n",
"\n",
"[ 1.5 3.5]\n",
"\n",
"[ 1. 1.]\n",
"\n",
"1.11803398875"
]
},
{
"cell_type": "code",
"metadata": {
"id": "8PZf9C3y_wj6",
"colab_type": "code",
"outputId": "4feea795-0157-4d3f-fdfd-121e59a1e848",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 121
}
},
"source": [
"import numpy\n",
"numpy.set_printoptions(legacy='1.13')\n",
"\n",
"N, M = map(int, input().split())\n",
"A = numpy.array([input().split() for i in range(N)], dtype =int)\n",
"print(numpy.mean(A, axis =1))\n",
"print(numpy.var(A, axis =0))\n",
"print(numpy.std(A, axis = None))"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"2 2\n",
"1 2\n",
"3 4\n",
"[ 1.5 3.5]\n",
"[ 1. 1.]\n",
"1.11803398875\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "uexqv9XmxoYf",
"colab_type": "text"
},
"source": [
"\n",
"\n",
"---\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "nvI9cHL4_1Zx",
"colab_type": "text"
},
"source": [
"## Task 7 : Sum and Prod\n",
"\n",
"You are given a 2-D array with dimensions X. \n",
"Your task is to perform the tool over axis and then find the of that result.\n",
"\n",
"**Input Format**\n",
"\n",
"The first line of input contains space separated values of and . \n",
"The next lines contains space separated integers.\n",
"\n",
"**Output Format**\n",
"\n",
"Compute the sum along axis . Then, print the product of that sum.\n",
"\n",
"**Sample Input**\n",
"\n",
"2 2\n",
"1 2\n",
"3 4\n",
"\n",
"**Sample Output**\n",
"\n",
"24\n",
"\n",
"Explanation\n",
"\n",
"The sum along axis 0 = [4 6] \n",
"\n",
"The product of this sum = 24"
]
},
{
"cell_type": "code",
"metadata": {
"id": "F-R2RZlZ__co",
"colab_type": "code",
"outputId": "cd4bbba7-2af4-447a-882a-2bb96093c00e",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 87
}
},
"source": [
"import numpy\n",
"\n",
"N, M = map(int, input().split())\n",
"\n",
"array = numpy.array([input().split() for i in range(N)], int)\n",
"\n",
"print(numpy.prod(numpy.sum(array, axis = 0), axis = None))\n",
"\n",
"# Hint first calculate the product and then the sum, \n",
"\n",
"# if you do other way you cannot complete the other two test cases.(I g"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"2 2\n",
"1 2\n",
"3 4\n",
"24\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "IipI2eqkxsSM",
"colab_type": "text"
},
"source": [
"\n",
"\n",
"---\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "fjWa3CQ_AElU",
"colab_type": "text"
},
"source": [
"## Task 8 : Eye and Identity\n",
"\n",
"\n",
"Your task is to print an array of size X with its main diagonal elements as 's and 's everywhere else.\n",
"\n",
"**Input Format**\n",
"\n",
"N denotes rows\n",
"M demotes coloumns\n",
"\n",
"**Output Format**\n",
"\n",
"Print the desired N X M array.\n",
"\n",
"**Sample Input**\n",
"\n",
"3 3\n",
"\n",
"**Sample Output**\n",
"\n",
"[[ 1. 0. 0.]\n",
"\n",
" [ 0. 1. 0.]\n",
" \n",
" [ 0. 0. 1.]]"
]
},
{
"cell_type": "code",
"metadata": {
"id": "3ITnHAgdANEW",
"colab_type": "code",
"outputId": "84608a2e-c65c-4ea8-dd2c-b4933ffe3b2f",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 87
}
},
"source": [
"import numpy\n",
"numpy.set_printoptions(sign=' ')\n",
"\n",
"N, M = map(int, input().split())\n",
"eye = numpy.eye(N, M)\n",
"print(eye)"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"3 3\n",
"[[ 1. 0. 0.]\n",
" [ 0. 1. 0.]\n",
" [ 0. 0. 1.]]\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "5G68phISxuaq",
"colab_type": "text"
},
"source": [
"\n",
"\n",
"---\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Pr43MTccAPgH",
"colab_type": "text"
},
"source": [
"## Task 9 : Linear Algebra\n",
"\n",
"You are given a square matrix with dimensions X. Your task is to find the determinant.\n",
"\n",
"**Input Format**\n",
"\n",
"The first line contains the integer . \n",
"The next lines contains the space separated elements of array .\n",
"\n",
"**Output Format**\n",
"\n",
"Print the determinant of .\n",
"\n",
"**Sample Input**\n",
"\n",
"2\n",
"1.1 1.1\n",
"1.1 1.1\n",
"\n",
"**Sample Output**\n",
"\n",
"0.0"
]
},
{
"cell_type": "code",
"metadata": {
"id": "tNVR6XrlAVQD",
"colab_type": "code",
"outputId": "9c8578bc-534d-4e7f-df29-0bec615d5cb2",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 87
}
},
"source": [
"import numpy\n",
"\n",
"numpy.set_printoptions(legacy='1.13')\n",
"\n",
"N = int(input())\n",
"\n",
"A = numpy.array([input().split() for i in range(N)], float)\n",
"print(numpy.linalg.det(A))"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"2\n",
"1.1 1.1\n",
"1.1 1.1\n",
"0.0\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "rR-GR9Chxv5A",
"colab_type": "text"
},
"source": [
"\n",
"\n",
"---\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "yD5tKzcwAZzD",
"colab_type": "text"
},
"source": [
"## Task 10 : Inner and Outer\n",
"\n",
"You are given two arrays: and . \n",
"Your task is to compute their inner and outer product.\n",
"\n",
"**Input Format**\n",
"\n",
"The first line contains the space separated elements of array . \n",
"The second line contains the space separated elements of array .\n",
"\n",
"**Output Format**\n",
"\n",
"First, print the inner product. \n",
"Second, print the outer product.\n",
"\n",
"**Sample Input**\n",
"\n",
"0 1\n",
"2 3\n",
"\n",
"**Sample Output**\n",
"\n",
"3\n",
"\n",
"[[0 0]\n",
"\n",
" [2 3]]\n",
" "
]
},
{
"cell_type": "code",
"metadata": {
"id": "K-Tn9wQCAjy1",
"colab_type": "code",
"outputId": "3fe4481c-46aa-41fe-baaf-c39c4a2a5909",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 139
}
},
"source": [
"import numpy\n",
"\n",
"a = numpy.array(input().split() , int)\n",
"b = numpy.array(input().split() , int)\n",
"print(\"---------------------------\")\n",
"print(numpy.inner(a, b))\n",
"print(\"---------------------------\")\n",
"print(numpy.outer(a, b))"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"0 1\n",
"2 3\n",
"---------------------------\n",
"3\n",
"---------------------------\n",
"[[0 0]\n",
" [2 3]]\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "KQHZnER6xyqV",
"colab_type": "text"
},
"source": [
"\n",
"\n",
"---\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "8XXFWoPVAnmg",
"colab_type": "text"
},
"source": [
"## Task 11 : Polynomials\n",
"\n",
"You are given the coefficients of a polynomial P. \n",
"Your task is to find the value of P at point x.\n",
"\n",
"I**nput Format**\n",
"\n",
"The first line contains the space separated value of the coefficients in P. \n",
"The second line contains the value of x.\n",
"\n",
"**Output Format**\n",
"\n",
"Print the desired value.\n",
"\n",
"**Sample Input**\n",
"\n",
"1.1 2 3\n",
"\n",
"0\n",
"\n",
"**Sample Output**\n",
"\n",
"3.0"
]
},
{
"cell_type": "code",
"metadata": {
"id": "janrfVQeAv17",
"colab_type": "code",
"outputId": "fc05fab2-27e6-49c1-b135-f4c1da2a3abb",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 69
}
},
"source": [
"import numpy\n",
"m = numpy.array(input().split(), float)\n",
"n = float(input())\n",
"print(numpy.polyval(m, n))"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"1.1 2 3\n",
"0\n",
"3.0\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "N2qzokL0x0_K",
"colab_type": "text"
},
"source": [
"\n",
"\n",
"---\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "7QOkQYzuA0HH",
"colab_type": "text"
},
"source": [
"## Task 12 : Dot and Cross\n",
"\n",
"You are given two arrays A and B. Both have dimensions of X. \n",
"\n",
"Your task is to compute their matrix product.\n",
"\n",
"**Input Format**\n",
"\n",
"The first line contains the integer N. \n",
"The next lines N contains N space separated integers of array A . \n",
"The following N lines contains N space separated integers of array B .\n",
"\n",
"**Output Format**\n",
"\n",
"Print the matrix multiplication of and .\n",
"\n",
"**Sample Input**\n",
"\n",
"2\n",
"\n",
"1 2\n",
"\n",
"3 4\n",
"\n",
"1 2\n",
"\n",
"3 4\n",
"\n",
"**Sample Output**\n",
"\n",
"[[ 7 10]\n",
"\n",
" [15 22]]"
]
},
{
"cell_type": "code",
"metadata": {
"id": "NhBIhaqiA_7b",
"colab_type": "code",
"outputId": "59dd4e16-79db-4a2b-c537-7ae9a95612e2",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 139
}
},
"source": [
"import numpy\n",
"N = int(input())\n",
"A = numpy.array([input().split() for i in range(N)], dtype =int)\n",
"B = numpy.array([input().split() for i in range(N)], dtype =int)\n",
"C = numpy.dot(A,B)\n",
"print(C)"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"2\n",
"1 2\n",
"3 4\n",
"1 2\n",
"3 4\n",
"[[ 7 10]\n",
" [15 22]]\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "eQogA7bgx3dz",
"colab_type": "text"
},
"source": [
"\n",
"\n",
"---\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "0yTBlaZDBECD",
"colab_type": "text"
},
"source": [
"## Task 13 : Concatenate\n",
"\n",
"You are given two integer arrays of size N X P and M X P (N & M are rows, and P is the column). \n",
"Your task is to concatenate the arrays along axis 0.\n",
"\n",
"**Input Format**\n",
"\n",
"The first line contains space separated integers N, M and P. \n",
"The next N lines contains the space separated elements of the P columns. \n",
"After that, the next M lines contains the space separated elements of the P columns.\n",
"\n",
"**Output Format**\n",
"\n",
"Print the concatenated array of size (N+M)XP.\n",
"\n",
"**Sample Input**\n",
"\n",
"4 3 2\n",
"\n",
"1 2\n",
"\n",
"1 2 \n",
"\n",
"1 2\n",
"\n",
"1 2\n",
"\n",
"3 4\n",
"\n",
"3 4\n",
"\n",
"3 4 \n",
"\n",
"**Sample Output**\n",
"\n",
"[[1 2]\n",
"\n",
" [1 2]\n",
" \n",
" [1 2]\n",
" \n",
" [1 2]\n",
" \n",
" [3 4]\n",
" \n",
" [3 4]\n",
" \n",
" [3 4]]"
]
},
{
"cell_type": "code",
"metadata": {
"id": "Ph5N0mI3BN0W",
"colab_type": "code",
"outputId": "7eef4e0f-e568-4e3e-c0e9-a9c3b576b7fa",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 277
}
},
"source": [
"import numpy\n",
"\n",
"X, Y, Z = map(int, input().split())\n",
"array = numpy.array([input().split() for i in range(X+Y)], int)\n",
"\n",
"print(array)"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"4 3 2\n",
"1 2\n",
"1 2\n",
"1 2\n",
"1 2\n",
"3 4\n",
"3 4\n",
"3 4\n",
"[[1 2]\n",
" [1 2]\n",
" [1 2]\n",
" [1 2]\n",
" [3 4]\n",
" [3 4]\n",
" [3 4]]\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "06e2n-POx6Ya",
"colab_type": "text"
},
"source": [
"\n",
"\n",
"---\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "DGJAG1guBUX6",
"colab_type": "text"
},
"source": [
"## Task 14 : Transpose and Flatten\n",
"\n",
"You are given a N X M integer array matrix with space separated elements (N = rows and M = columns). \n",
"Your task is to print the transpose and flatten results.\n",
"\n",
"**Input Format**\n",
"\n",
"The first line contains the space separated values of N and M. \n",
"The next lines contains the space separated elements of M columns.\n",
"\n",
"**Output Format**\n",
"\n",
"First, print the transpose array and then print the flatten.\n",
"\n",
"**Sample Input**\n",
"\n",
"2 2\n",
"\n",
"1 2\n",
"\n",
"3 4\n",
"\n",
"**Sample Output**\n",
"\n",
"[[1 3]\n",
"\n",
" [2 4]]\n",
" \n",
"[1 2 3 4]"
]
},
{
"cell_type": "code",
"metadata": {
"id": "S7-38B0VBdTQ",
"colab_type": "code",
"outputId": "ffab02e7-237e-4d04-fdf8-ea19e59ff48b",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 121
}
},
"source": [
"import numpy\n",
"\n",
"n, m = map(int, input().split())\n",
"array = numpy.array([input().strip().split() for _ in range(n)], int)\n",
"print (array.transpose())\n",
"print (array.flatten())"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"2 2\n",
"1 2\n",
"3 4\n",
"[[1 3]\n",
" [2 4]]\n",
"[1 2 3 4]\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "YQfRPr_2x8te",
"colab_type": "text"
},
"source": [
"\n",
"\n",
"---\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "JL4TGrdRBgqu",
"colab_type": "text"
},
"source": [
"## Task 15 : Min and Max\n",
"\n",
"You are given a 2-D array with dimensions N X M. \n",
"Your task is to perform the min function over axis 1 and then find the max of that.\n",
"\n",
"**Input Format**\n",
"\n",
"The first line of input contains the space separated values of N and M . \n",
"The next N lines contains M space separated integers.\n",
"\n",
"**Output Format**\n",
"\n",
"Compute the min along axis 1 and then print the max of that result.\n",
"\n",
"**Sample Input**\n",
"\n",
"4 2\n",
"\n",
"2 5\n",
"\n",
"3 7\n",
"\n",
"1 3\n",
"\n",
"4 0\n",
"\n",
"**Sample Output**\n",
"\n",
"3\n",
"\n",
"**Explanation**\n",
"\n",
"The min along axis 1 = [2, 3, 1, 0]\n",
"\n",
"The max of [2, 3, 1, 0] = 3"
]
},
{
"cell_type": "code",
"metadata": {
"id": "FvdBYhHBBrGC",
"colab_type": "code",
"outputId": "3c84232a-981e-4e9b-c2e0-bc5769a9a825",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 121
}
},
"source": [
"import numpy\n",
"N, M = map(int, input().split())\n",
"\n",
"A = numpy.array([input().split() for i in range(N)],dtype = int)\n",
"print(numpy.max(numpy.min(A, axis=1), axis=0))"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"4 2\n",
"2 5\n",
"3 7\n",
"1 3\n",
"4 0\n",
"3\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "rkc2mAAhx_vw",
"colab_type": "text"
},
"source": [
"\n",
"\n",
"---\n",
"\n"
]
}
]
}