|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Problem: Counting the number of negative numbers.\n", |
| 8 | + "\n", |
| 9 | + "You are given a 2-dimensional array with integers.\n", |
| 10 | + "\n", |
| 11 | + "Example Input: \n", |
| 12 | + "[[-4, -3, -1, 1], \n", |
| 13 | + " [-2, -2, 1, 2], \n", |
| 14 | + " [-1, 1, 2, 3], \n", |
| 15 | + " [ 1, 2, 4, 5]]\n", |
| 16 | + "\n", |
| 17 | + "Write a function, count_negatives(input), which finds and returns the number of negative integers in th array." |
| 18 | + ] |
| 19 | + }, |
| 20 | + { |
| 21 | + "cell_type": "markdown", |
| 22 | + "metadata": {}, |
| 23 | + "source": [ |
| 24 | + "### NOTE: Make sure to run your program once you write it :)" |
| 25 | + ] |
| 26 | + }, |
| 27 | + { |
| 28 | + "cell_type": "markdown", |
| 29 | + "metadata": {}, |
| 30 | + "source": [ |
| 31 | + "# Implement your function below." |
| 32 | + ] |
| 33 | + }, |
| 34 | + { |
| 35 | + "cell_type": "code", |
| 36 | + "execution_count": 1, |
| 37 | + "metadata": {}, |
| 38 | + "outputs": [], |
| 39 | + "source": [ |
| 40 | + "# Input: A 2-dimensional array with integers. Example below.\n", |
| 41 | + "#[[-4, -3, -1, 1],\n", |
| 42 | + "# [-2, -2, 1, 2],\n", |
| 43 | + "# [-1, 1, 2, 3],\n", |
| 44 | + "# [ 1, 2, 4, 5]]\n", |
| 45 | + "# Returns:\n", |
| 46 | + "# The number of negative numbers in the array.\n", |
| 47 | + "# In the above case, this function should return 6\n", |
| 48 | + "# since there are 6 negative numbers in the array.\n", |
| 49 | + "def count_negatives(given_array):\n", |
| 50 | + " count = 0\n", |
| 51 | + " row_i = 0\n", |
| 52 | + " col_i = len(given_array) - 1 # start from the right most last columns and move to left\n", |
| 53 | + " \n", |
| 54 | + " while col_i >= 0 and row_i < len(given_array):\n", |
| 55 | + " if given_array[row_i][col_i] < 0:\n", |
| 56 | + " count += (col_i + 1)\n", |
| 57 | + " row_i += 1\n", |
| 58 | + " else:\n", |
| 59 | + " col_i -= 1 # move to left side\n", |
| 60 | + "\n", |
| 61 | + " return count" |
| 62 | + ] |
| 63 | + }, |
| 64 | + { |
| 65 | + "cell_type": "markdown", |
| 66 | + "metadata": {}, |
| 67 | + "source": [ |
| 68 | + "# Use the code below to test your function." |
| 69 | + ] |
| 70 | + }, |
| 71 | + { |
| 72 | + "cell_type": "code", |
| 73 | + "execution_count": 2, |
| 74 | + "metadata": {}, |
| 75 | + "outputs": [ |
| 76 | + { |
| 77 | + "name": "stdout", |
| 78 | + "output_type": "stream", |
| 79 | + "text": [ |
| 80 | + "\n", |
| 81 | + "How many negative numbers are there in the following array?\n", |
| 82 | + "(There are 6.)\n", |
| 83 | + "\n", |
| 84 | + "[[-4, -3, -1, 1],\n", |
| 85 | + " [-2, -2, 1, 2],\n", |
| 86 | + " [-1, 1, 2, 3],\n", |
| 87 | + " [ 1, 2, 4, 5]]\n", |
| 88 | + "\n" |
| 89 | + ] |
| 90 | + }, |
| 91 | + { |
| 92 | + "data": { |
| 93 | + "text/plain": [ |
| 94 | + "6" |
| 95 | + ] |
| 96 | + }, |
| 97 | + "execution_count": 2, |
| 98 | + "metadata": {}, |
| 99 | + "output_type": "execute_result" |
| 100 | + } |
| 101 | + ], |
| 102 | + "source": [ |
| 103 | + "print(\"\"\"\n", |
| 104 | + "How many negative numbers are there in the following array?\n", |
| 105 | + "(There are 6.)\n", |
| 106 | + "\n", |
| 107 | + "[[-4, -3, -1, 1],\n", |
| 108 | + " [-2, -2, 1, 2],\n", |
| 109 | + " [-1, 1, 2, 3],\n", |
| 110 | + " [ 1, 2, 4, 5]]\n", |
| 111 | + "\"\"\")\n", |
| 112 | + "\n", |
| 113 | + "count_negatives(\n", |
| 114 | + "[[-4, -3, -1, 1],\n", |
| 115 | + " [-2, -2, 1, 2],\n", |
| 116 | + " [-1, 1, 2, 3],\n", |
| 117 | + " [ 1, 2, 4, 5]])" |
| 118 | + ] |
| 119 | + }, |
| 120 | + { |
| 121 | + "cell_type": "code", |
| 122 | + "execution_count": 3, |
| 123 | + "metadata": {}, |
| 124 | + "outputs": [ |
| 125 | + { |
| 126 | + "name": "stdout", |
| 127 | + "output_type": "stream", |
| 128 | + "text": [ |
| 129 | + "\n", |
| 130 | + "How many negative numbers are there in the following array?\n", |
| 131 | + "(There is 1.)\n", |
| 132 | + "\n", |
| 133 | + "[[-1, 0],\n", |
| 134 | + " [0, 0]]\n", |
| 135 | + "\n" |
| 136 | + ] |
| 137 | + }, |
| 138 | + { |
| 139 | + "data": { |
| 140 | + "text/plain": [ |
| 141 | + "1" |
| 142 | + ] |
| 143 | + }, |
| 144 | + "execution_count": 3, |
| 145 | + "metadata": {}, |
| 146 | + "output_type": "execute_result" |
| 147 | + } |
| 148 | + ], |
| 149 | + "source": [ |
| 150 | + "print(\"\"\"\n", |
| 151 | + "How many negative numbers are there in the following array?\n", |
| 152 | + "(There is 1.)\n", |
| 153 | + "\n", |
| 154 | + "[[-1, 0],\n", |
| 155 | + " [0, 0]]\n", |
| 156 | + "\"\"\")\n", |
| 157 | + "\n", |
| 158 | + "count_negatives(\n", |
| 159 | + "[[-1, 0],\n", |
| 160 | + " [0, 0]])" |
| 161 | + ] |
| 162 | + }, |
| 163 | + { |
| 164 | + "cell_type": "code", |
| 165 | + "execution_count": 4, |
| 166 | + "metadata": {}, |
| 167 | + "outputs": [ |
| 168 | + { |
| 169 | + "name": "stdout", |
| 170 | + "output_type": "stream", |
| 171 | + "text": [ |
| 172 | + "\n", |
| 173 | + "How many negative numbers are there in the following array?\n", |
| 174 | + "(There are 2.)\n", |
| 175 | + "\n", |
| 176 | + "[[-2, 0],\n", |
| 177 | + " [-1, 0]]\n", |
| 178 | + "\n" |
| 179 | + ] |
| 180 | + }, |
| 181 | + { |
| 182 | + "data": { |
| 183 | + "text/plain": [ |
| 184 | + "2" |
| 185 | + ] |
| 186 | + }, |
| 187 | + "execution_count": 4, |
| 188 | + "metadata": {}, |
| 189 | + "output_type": "execute_result" |
| 190 | + } |
| 191 | + ], |
| 192 | + "source": [ |
| 193 | + "print(\"\"\"\n", |
| 194 | + "How many negative numbers are there in the following array?\n", |
| 195 | + "(There are 2.)\n", |
| 196 | + "\n", |
| 197 | + "[[-2, 0],\n", |
| 198 | + " [-1, 0]]\n", |
| 199 | + "\"\"\")\n", |
| 200 | + "\n", |
| 201 | + "count_negatives(\n", |
| 202 | + "[[-2, 0],\n", |
| 203 | + " [-1, 0]])" |
| 204 | + ] |
| 205 | + }, |
| 206 | + { |
| 207 | + "cell_type": "code", |
| 208 | + "execution_count": 5, |
| 209 | + "metadata": {}, |
| 210 | + "outputs": [ |
| 211 | + { |
| 212 | + "name": "stdout", |
| 213 | + "output_type": "stream", |
| 214 | + "text": [ |
| 215 | + "\n", |
| 216 | + "How many negative numbers are there in the following array?\n", |
| 217 | + "(There are none.)\n", |
| 218 | + "\n", |
| 219 | + "[[0]]\n", |
| 220 | + "\n" |
| 221 | + ] |
| 222 | + }, |
| 223 | + { |
| 224 | + "data": { |
| 225 | + "text/plain": [ |
| 226 | + "0" |
| 227 | + ] |
| 228 | + }, |
| 229 | + "execution_count": 5, |
| 230 | + "metadata": {}, |
| 231 | + "output_type": "execute_result" |
| 232 | + } |
| 233 | + ], |
| 234 | + "source": [ |
| 235 | + "print(\"\"\"\n", |
| 236 | + "How many negative numbers are there in the following array?\n", |
| 237 | + "(There are none.)\n", |
| 238 | + "\n", |
| 239 | + "[[0]]\n", |
| 240 | + "\"\"\")\n", |
| 241 | + "\n", |
| 242 | + "count_negatives([[0]])" |
| 243 | + ] |
| 244 | + } |
| 245 | + ], |
| 246 | + "metadata": { |
| 247 | + "anaconda-cloud": {}, |
| 248 | + "kernelspec": { |
| 249 | + "display_name": "Python 3", |
| 250 | + "language": "python", |
| 251 | + "name": "python3" |
| 252 | + }, |
| 253 | + "language_info": { |
| 254 | + "codemirror_mode": { |
| 255 | + "name": "ipython", |
| 256 | + "version": 3 |
| 257 | + }, |
| 258 | + "file_extension": ".py", |
| 259 | + "mimetype": "text/x-python", |
| 260 | + "name": "python", |
| 261 | + "nbconvert_exporter": "python", |
| 262 | + "pygments_lexer": "ipython3", |
| 263 | + "version": "3.7.6" |
| 264 | + } |
| 265 | + }, |
| 266 | + "nbformat": 4, |
| 267 | + "nbformat_minor": 1 |
| 268 | +} |
0 commit comments