|
1 | 1 | { |
2 | 2 | "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# [Array Mathematics](https://www.hackerrank.com/challenges/np-array-mathematics/problem?h_r=next-challenge&h_v=zen)\n", |
| 8 | + "<pre>\n", |
| 9 | + "Basic mathematical functions operate element-wise on arrays. They are available both as operator overloads and as functions in the NumPy module.\n", |
| 10 | + "\n", |
| 11 | + "import numpy\n", |
| 12 | + "\n", |
| 13 | + "a = numpy.array([1,2,3,4], float)\n", |
| 14 | + "b = numpy.array([5,6,7,8], float)\n", |
| 15 | + "\n", |
| 16 | + "print a + b #[ 6. 8. 10. 12.]\n", |
| 17 | + "print numpy.add(a, b) #[ 6. 8. 10. 12.]\n", |
| 18 | + "\n", |
| 19 | + "print a - b #[-4. -4. -4. -4.]\n", |
| 20 | + "print numpy.subtract(a, b) #[-4. -4. -4. -4.]\n", |
| 21 | + "\n", |
| 22 | + "print a * b #[ 5. 12. 21. 32.]\n", |
| 23 | + "print numpy.multiply(a, b) #[ 5. 12. 21. 32.]\n", |
| 24 | + "\n", |
| 25 | + "print a / b #[ 0.2 0.33333333 0.42857143 0.5 ]\n", |
| 26 | + "print numpy.divide(a, b) #[ 0.2 0.33333333 0.42857143 0.5 ]\n", |
| 27 | + "\n", |
| 28 | + "print a % b #[ 1. 2. 3. 4.]\n", |
| 29 | + "print numpy.mod(a, b) #[ 1. 2. 3. 4.]\n", |
| 30 | + "\n", |
| 31 | + "print a**b #[ 1.00000000e+00 6.40000000e+01 2.18700000e+03 6.55360000e+04]\n", |
| 32 | + "print numpy.power(a, b) #[ 1.00000000e+00 6.40000000e+01 2.18700000e+03 6.55360000e+04]</pre>" |
| 33 | + ] |
| 34 | + }, |
| 35 | + { |
| 36 | + "cell_type": "code", |
| 37 | + "execution_count": 23, |
| 38 | + "metadata": {}, |
| 39 | + "outputs": [ |
| 40 | + { |
| 41 | + "name": "stdin", |
| 42 | + "output_type": "stream", |
| 43 | + "text": [ |
| 44 | + " 1 4\n", |
| 45 | + " 1 2 3 4\n", |
| 46 | + " 5 6 7 8\n" |
| 47 | + ] |
| 48 | + }, |
| 49 | + { |
| 50 | + "name": "stdout", |
| 51 | + "output_type": "stream", |
| 52 | + "text": [ |
| 53 | + "[[ 6 8 10 12]]\n", |
| 54 | + "[[-4 -4 -4 -4]]\n", |
| 55 | + "[[ 5 12 21 32]]\n", |
| 56 | + "[[0 0 0 0]]\n", |
| 57 | + "[[1 2 3 4]]\n", |
| 58 | + "[[ 1 64 2187 65536]]\n", |
| 59 | + "\n" |
| 60 | + ] |
| 61 | + } |
| 62 | + ], |
| 63 | + "source": [ |
| 64 | + "import numpy as np\n", |
| 65 | + "n1,n2=map(int,input().split())\n", |
| 66 | + "a1=np.zeros((n1,n2),int)\n", |
| 67 | + "a2=np.zeros((n1,n2),int)\n", |
| 68 | + "\n", |
| 69 | + "for i in range(n1):\n", |
| 70 | + " a1[i]=np.array(input().split(),int)\n", |
| 71 | + "\n", |
| 72 | + "for i in range(n1):\n", |
| 73 | + " a2[i]=np.array(input().split(),int)\n", |
| 74 | + "\n", |
| 75 | + "\n", |
| 76 | + "np.set_printoptions(legacy='1.13')\n", |
| 77 | + " \n", |
| 78 | + "print(f'{(a1+a2).astype(int)}\\n{(a1-a2).astype(int)}\\n{(a1*a2).astype(int)}\\n{(a1//a2).astype(int)}\\n{(a1%a2).astype(int)}\\n{(a1**a2).astype(int)}\\n')" |
| 79 | + ] |
| 80 | + }, |
| 81 | + { |
| 82 | + "cell_type": "code", |
| 83 | + "execution_count": 31, |
| 84 | + "metadata": {}, |
| 85 | + "outputs": [ |
| 86 | + { |
| 87 | + "data": { |
| 88 | + "text/plain": [ |
| 89 | + "array([0, 0, 0, 0])" |
| 90 | + ] |
| 91 | + }, |
| 92 | + "execution_count": 31, |
| 93 | + "metadata": {}, |
| 94 | + "output_type": "execute_result" |
| 95 | + } |
| 96 | + ], |
| 97 | + "source": [ |
| 98 | + "a1=np.zeros((n1,n2),int)\n", |
| 99 | + "# a1[0]=np.array(input().split(),int)\n", |
| 100 | + "a1[0]" |
| 101 | + ] |
| 102 | + }, |
| 103 | + { |
| 104 | + "cell_type": "code", |
| 105 | + "execution_count": 15, |
| 106 | + "metadata": {}, |
| 107 | + "outputs": [ |
| 108 | + { |
| 109 | + "data": { |
| 110 | + "text/plain": [ |
| 111 | + "array([[0, 0],\n", |
| 112 | + " [0, 0]])" |
| 113 | + ] |
| 114 | + }, |
| 115 | + "execution_count": 15, |
| 116 | + "metadata": {}, |
| 117 | + "output_type": "execute_result" |
| 118 | + } |
| 119 | + ], |
| 120 | + "source": [ |
| 121 | + "np.zeros((2,2),int)" |
| 122 | + ] |
| 123 | + }, |
| 124 | + { |
| 125 | + "cell_type": "markdown", |
| 126 | + "metadata": {}, |
| 127 | + "source": [ |
| 128 | + "# [Floor, Ceil and Rint](https://www.hackerrank.com/challenges/floor-ceil-and-rint/problem?h_r=next-challenge&h_v=zen)\n", |
| 129 | + "\n", |
| 130 | + "<pre>\n", |
| 131 | + "<b>floor</b>\n", |
| 132 | + "The tool floor returns the floor of the input element-wise.\n", |
| 133 | + "The floor of is the largest integer where .\n", |
| 134 | + "\n", |
| 135 | + "import numpy\n", |
| 136 | + "\n", |
| 137 | + "my_array = numpy.array([1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9])\n", |
| 138 | + "print numpy.floor(my_array) #[ 1. 2. 3. 4. 5. 6. 7. 8. 9.]\n", |
| 139 | + "\n", |
| 140 | + "<b>ceil</b>\n", |
| 141 | + "The tool ceil returns the ceiling of the input element-wise.\n", |
| 142 | + "The ceiling of is the smallest integer where .\n", |
| 143 | + "\n", |
| 144 | + "import numpy\n", |
| 145 | + "\n", |
| 146 | + "my_array = numpy.array([1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9])\n", |
| 147 | + "print numpy.ceil(my_array) #[ 2. 3. 4. 5. 6. 7. 8. 9. 10.]\n", |
| 148 | + "\n", |
| 149 | + "<b>rint</b>\n", |
| 150 | + "The rint tool rounds to the nearest integer of input element-wise.\n", |
| 151 | + "\n", |
| 152 | + "import numpy\n", |
| 153 | + "\n", |
| 154 | + "my_array = numpy.array([1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9])\n", |
| 155 | + "print numpy.rint(my_array) #[ 1. 2. 3. 4. 6. 7. 8. 9. 10.]\n", |
| 156 | + "</pre>" |
| 157 | + ] |
| 158 | + }, |
| 159 | + { |
| 160 | + "cell_type": "code", |
| 161 | + "execution_count": 1, |
| 162 | + "metadata": {}, |
| 163 | + "outputs": [ |
| 164 | + { |
| 165 | + "name": "stdin", |
| 166 | + "output_type": "stream", |
| 167 | + "text": [ |
| 168 | + " 1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 9.9\n" |
| 169 | + ] |
| 170 | + }, |
| 171 | + { |
| 172 | + "name": "stdout", |
| 173 | + "output_type": "stream", |
| 174 | + "text": [ |
| 175 | + "[ 1. 2. 3. 4. 5. 6. 7. 8. 9.]\n", |
| 176 | + "[ 2. 3. 4. 5. 6. 7. 8. 9. 10.]\n", |
| 177 | + "[ 1. 2. 3. 4. 6. 7. 8. 9. 10.]\n" |
| 178 | + ] |
| 179 | + } |
| 180 | + ], |
| 181 | + "source": [ |
| 182 | + "import numpy as np\n", |
| 183 | + "a=np.array(input().split(),float)\n", |
| 184 | + "np.set_printoptions(legacy='1.13')\n", |
| 185 | + "print(np.floor(a))\n", |
| 186 | + "print(np.ceil(a))\n", |
| 187 | + "print(np.rint(a))" |
| 188 | + ] |
| 189 | + }, |
| 190 | + { |
| 191 | + "cell_type": "code", |
| 192 | + "execution_count": null, |
| 193 | + "metadata": {}, |
| 194 | + "outputs": [], |
| 195 | + "source": [] |
| 196 | + }, |
3 | 197 | { |
4 | 198 | "cell_type": "markdown", |
5 | 199 | "metadata": {}, |
|
535 | 729 | }, |
536 | 730 | { |
537 | 731 | "cell_type": "code", |
538 | | - "execution_count": 19, |
| 732 | + "execution_count": 1, |
539 | 733 | "metadata": {}, |
540 | 734 | "outputs": [ |
541 | 735 | { |
|
558 | 752 | "source": [ |
559 | 753 | "import numpy as np\n", |
560 | 754 | "n1,n2=map(int,input().split())\n", |
| 755 | + "np.set_printoptions(legacy='1.13')\n", |
561 | 756 | "print(np.eye(n1,n2))" |
562 | 757 | ] |
563 | 758 | }, |
|
0 commit comments