|
131 | 131 | "cell_type": "markdown", |
132 | 132 | "metadata": {}, |
133 | 133 | "source": [ |
134 | | - "## Array Sum" |
| 134 | + "## Length of an array" |
135 | 135 | ] |
136 | 136 | }, |
137 | 137 | { |
138 | 138 | "cell_type": "code", |
139 | 139 | "execution_count": 6, |
| 140 | + "metadata": {}, |
| 141 | + "outputs": [], |
| 142 | + "source": [ |
| 143 | + "def array_len(x):\n", |
| 144 | + " if x == []:\n", |
| 145 | + " return 0\n", |
| 146 | + " else:\n", |
| 147 | + " return 1 + array_len(x[1:])" |
| 148 | + ] |
| 149 | + }, |
| 150 | + { |
| 151 | + "cell_type": "code", |
| 152 | + "execution_count": 7, |
| 153 | + "metadata": {}, |
| 154 | + "outputs": [ |
| 155 | + { |
| 156 | + "data": { |
| 157 | + "text/plain": [ |
| 158 | + "0" |
| 159 | + ] |
| 160 | + }, |
| 161 | + "execution_count": 7, |
| 162 | + "metadata": {}, |
| 163 | + "output_type": "execute_result" |
| 164 | + } |
| 165 | + ], |
| 166 | + "source": [ |
| 167 | + "array_len([])" |
| 168 | + ] |
| 169 | + }, |
| 170 | + { |
| 171 | + "cell_type": "code", |
| 172 | + "execution_count": 8, |
| 173 | + "metadata": {}, |
| 174 | + "outputs": [ |
| 175 | + { |
| 176 | + "data": { |
| 177 | + "text/plain": [ |
| 178 | + "3" |
| 179 | + ] |
| 180 | + }, |
| 181 | + "execution_count": 8, |
| 182 | + "metadata": {}, |
| 183 | + "output_type": "execute_result" |
| 184 | + } |
| 185 | + ], |
| 186 | + "source": [ |
| 187 | + "array_len([1, 2, 3])" |
| 188 | + ] |
| 189 | + }, |
| 190 | + { |
| 191 | + "cell_type": "markdown", |
| 192 | + "metadata": {}, |
| 193 | + "source": [ |
| 194 | + "## Sum of the elements in an array" |
| 195 | + ] |
| 196 | + }, |
| 197 | + { |
| 198 | + "cell_type": "code", |
| 199 | + "execution_count": 9, |
140 | 200 | "metadata": { |
141 | 201 | "collapsed": true |
142 | 202 | }, |
143 | 203 | "outputs": [], |
144 | 204 | "source": [ |
145 | 205 | "def array_sum(x):\n", |
146 | | - " if not len(x):\n", |
| 206 | + " if x == []:\n", |
147 | 207 | " return 0\n", |
148 | 208 | " else:\n", |
149 | 209 | " return x[0] + array_sum(x[1:])" |
150 | 210 | ] |
151 | 211 | }, |
152 | 212 | { |
153 | 213 | "cell_type": "code", |
154 | | - "execution_count": 7, |
| 214 | + "execution_count": 10, |
155 | 215 | "metadata": {}, |
156 | 216 | "outputs": [ |
157 | 217 | { |
|
160 | 220 | "0" |
161 | 221 | ] |
162 | 222 | }, |
163 | | - "execution_count": 7, |
| 223 | + "execution_count": 10, |
164 | 224 | "metadata": {}, |
165 | 225 | "output_type": "execute_result" |
166 | 226 | } |
|
171 | 231 | }, |
172 | 232 | { |
173 | 233 | "cell_type": "code", |
174 | | - "execution_count": 8, |
| 234 | + "execution_count": 11, |
175 | 235 | "metadata": {}, |
176 | 236 | "outputs": [ |
177 | 237 | { |
|
180 | 240 | "5" |
181 | 241 | ] |
182 | 242 | }, |
183 | | - "execution_count": 8, |
| 243 | + "execution_count": 11, |
184 | 244 | "metadata": {}, |
185 | 245 | "output_type": "execute_result" |
186 | 246 | } |
|
191 | 251 | }, |
192 | 252 | { |
193 | 253 | "cell_type": "code", |
194 | | - "execution_count": 9, |
| 254 | + "execution_count": 12, |
195 | 255 | "metadata": {}, |
196 | 256 | "outputs": [ |
197 | 257 | { |
|
200 | 260 | "15" |
201 | 261 | ] |
202 | 262 | }, |
203 | | - "execution_count": 9, |
| 263 | + "execution_count": 12, |
204 | 264 | "metadata": {}, |
205 | 265 | "output_type": "execute_result" |
206 | 266 | } |
207 | 267 | ], |
208 | 268 | "source": [ |
209 | 269 | "array_sum([1, 2, 3, 4, 5])" |
210 | 270 | ] |
| 271 | + }, |
| 272 | + { |
| 273 | + "cell_type": "markdown", |
| 274 | + "metadata": {}, |
| 275 | + "source": [ |
| 276 | + "## Binary search using recursion" |
| 277 | + ] |
| 278 | + }, |
| 279 | + { |
| 280 | + "cell_type": "code", |
| 281 | + "execution_count": 13, |
| 282 | + "metadata": { |
| 283 | + "collapsed": true |
| 284 | + }, |
| 285 | + "outputs": [], |
| 286 | + "source": [ |
| 287 | + "def binary_search(array, value, min_idx=0, max_idx=None):\n", |
| 288 | + " \n", |
| 289 | + " if min_idx == max_idx:\n", |
| 290 | + " return None\n", |
| 291 | + " elif max_idx is None:\n", |
| 292 | + " max_idx = len(array)\n", |
| 293 | + " else:\n", |
| 294 | + " pass\n", |
| 295 | + "\n", |
| 296 | + " middle_idx = min_idx + (max_idx - min_idx) // 2\n", |
| 297 | + "\n", |
| 298 | + " if array[middle_idx] == value:\n", |
| 299 | + " return middle_idx\n", |
| 300 | + " elif array[middle_idx] < value:\n", |
| 301 | + " min_idx = middle_idx + 1\n", |
| 302 | + " else:\n", |
| 303 | + " max_idx = middle_idx\n", |
| 304 | + " \n", |
| 305 | + " return binary_search(array, value, min_idx, max_idx)" |
| 306 | + ] |
| 307 | + }, |
| 308 | + { |
| 309 | + "cell_type": "code", |
| 310 | + "execution_count": 14, |
| 311 | + "metadata": {}, |
| 312 | + "outputs": [ |
| 313 | + { |
| 314 | + "data": { |
| 315 | + "text/plain": [ |
| 316 | + "0" |
| 317 | + ] |
| 318 | + }, |
| 319 | + "execution_count": 14, |
| 320 | + "metadata": {}, |
| 321 | + "output_type": "execute_result" |
| 322 | + } |
| 323 | + ], |
| 324 | + "source": [ |
| 325 | + "binary_search(array=[1, 2, 4, 7, 8, 10, 11],\n", |
| 326 | + " value=1)" |
| 327 | + ] |
| 328 | + }, |
| 329 | + { |
| 330 | + "cell_type": "code", |
| 331 | + "execution_count": 15, |
| 332 | + "metadata": {}, |
| 333 | + "outputs": [ |
| 334 | + { |
| 335 | + "data": { |
| 336 | + "text/plain": [ |
| 337 | + "1" |
| 338 | + ] |
| 339 | + }, |
| 340 | + "execution_count": 15, |
| 341 | + "metadata": {}, |
| 342 | + "output_type": "execute_result" |
| 343 | + } |
| 344 | + ], |
| 345 | + "source": [ |
| 346 | + "binary_search(array=[1, 2, 4, 7, 8, 10, 11],\n", |
| 347 | + " value=2)" |
| 348 | + ] |
| 349 | + }, |
| 350 | + { |
| 351 | + "cell_type": "code", |
| 352 | + "execution_count": 16, |
| 353 | + "metadata": {}, |
| 354 | + "outputs": [ |
| 355 | + { |
| 356 | + "data": { |
| 357 | + "text/plain": [ |
| 358 | + "2" |
| 359 | + ] |
| 360 | + }, |
| 361 | + "execution_count": 16, |
| 362 | + "metadata": {}, |
| 363 | + "output_type": "execute_result" |
| 364 | + } |
| 365 | + ], |
| 366 | + "source": [ |
| 367 | + "binary_search(array=[1, 2, 4, 7, 8, 10, 11],\n", |
| 368 | + " value=4)" |
| 369 | + ] |
| 370 | + }, |
| 371 | + { |
| 372 | + "cell_type": "code", |
| 373 | + "execution_count": 17, |
| 374 | + "metadata": {}, |
| 375 | + "outputs": [ |
| 376 | + { |
| 377 | + "data": { |
| 378 | + "text/plain": [ |
| 379 | + "6" |
| 380 | + ] |
| 381 | + }, |
| 382 | + "execution_count": 17, |
| 383 | + "metadata": {}, |
| 384 | + "output_type": "execute_result" |
| 385 | + } |
| 386 | + ], |
| 387 | + "source": [ |
| 388 | + "binary_search(array=[1, 2, 4, 7, 8, 10, 11],\n", |
| 389 | + " value=11)" |
| 390 | + ] |
| 391 | + }, |
| 392 | + { |
| 393 | + "cell_type": "code", |
| 394 | + "execution_count": 18, |
| 395 | + "metadata": {}, |
| 396 | + "outputs": [], |
| 397 | + "source": [ |
| 398 | + "binary_search(array=[1, 2, 4, 7, 8, 10, 11],\n", |
| 399 | + " value=99)" |
| 400 | + ] |
211 | 401 | } |
212 | 402 | ], |
213 | 403 | "metadata": { |
|
0 commit comments