diff --git a/100_Numpy_exercises.ipynb b/100_Numpy_exercises.ipynb index 3722882..db8c742 100644 --- a/100_Numpy_exercises.ipynb +++ b/100_Numpy_exercises.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "markdown", - "id": "3780a226", + "id": "cf1ef34b", "metadata": {}, "source": [ "# 100 numpy exercises\n", @@ -18,7 +18,7 @@ }, { "cell_type": "markdown", - "id": "63ded581", + "id": "6f6230b6", "metadata": {}, "source": [ "File automatically generated. See the documentation to update questions/answers/hints programmatically." @@ -26,17 +26,17 @@ }, { "cell_type": "markdown", - "id": "456258e7", + "id": "699bb7c8", "metadata": {}, "source": [ - "Run the `initialize.py` module, then for each question you can query the\n", + "Run the `initialise.py` module, then for each question you can query the\n", "answer or an hint with `hint(n)` or `answer(n)` for `n` question number." ] }, { "cell_type": "code", "execution_count": null, - "id": "ad8f85b1", + "id": "f8766b32", "metadata": {}, "outputs": [], "source": [ @@ -45,7 +45,7 @@ }, { "cell_type": "markdown", - "id": "40518c2e", + "id": "86bb254b", "metadata": {}, "source": [ "#### 1. Import the numpy package under the name `np` (★☆☆)" @@ -54,14 +54,14 @@ { "cell_type": "code", "execution_count": null, - "id": "08ee02d9", + "id": "518c8183", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "47c48198", + "id": "73bc1295", "metadata": {}, "source": [ "#### 2. Print the numpy version and the configuration (★☆☆)" @@ -70,14 +70,14 @@ { "cell_type": "code", "execution_count": null, - "id": "8a94ff73", + "id": "cc208209", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "4f405544", + "id": "3ec5eeae", "metadata": {}, "source": [ "#### 3. Create a null vector of size 10 (★☆☆)" @@ -86,14 +86,14 @@ { "cell_type": "code", "execution_count": null, - "id": "4a0d86fc", + "id": "11ac0188", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "c8754c7a", + "id": "4833b037", "metadata": {}, "source": [ "#### 4. How to find the memory size of any array (★☆☆)" @@ -102,14 +102,14 @@ { "cell_type": "code", "execution_count": null, - "id": "4fa4c3e1", + "id": "7b37d02c", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "0381891d", + "id": "22406ea1", "metadata": {}, "source": [ "#### 5. How to get the documentation of the numpy add function from the command line? (★☆☆)" @@ -118,14 +118,14 @@ { "cell_type": "code", "execution_count": null, - "id": "79c56ec6", + "id": "17cd71af", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "fb0edf87", + "id": "b3ab3c55", "metadata": {}, "source": [ "#### 6. Create a null vector of size 10 but the fifth value which is 1 (★☆☆)" @@ -134,14 +134,14 @@ { "cell_type": "code", "execution_count": null, - "id": "5b8c3d98", + "id": "0b6c7f75", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "6aedaa04", + "id": "6d8c00c9", "metadata": {}, "source": [ "#### 7. Create a vector with values ranging from 10 to 49 (★☆☆)" @@ -150,14 +150,14 @@ { "cell_type": "code", "execution_count": null, - "id": "399d0f91", + "id": "ba0eea70", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "c05183de", + "id": "eef0d721", "metadata": {}, "source": [ "#### 8. Reverse a vector (first element becomes last) (★☆☆)" @@ -166,14 +166,14 @@ { "cell_type": "code", "execution_count": null, - "id": "beb3ed87", + "id": "e73e011f", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "49ffe24b", + "id": "bf274738", "metadata": {}, "source": [ "#### 9. Create a 3x3 matrix with values ranging from 0 to 8 (★☆☆)" @@ -182,14 +182,14 @@ { "cell_type": "code", "execution_count": null, - "id": "6bcbb69c", + "id": "0013166a", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "d74c5259", + "id": "edc4900b", "metadata": {}, "source": [ "#### 10. Find indices of non-zero elements from [1,2,0,0,4,0] (★☆☆)" @@ -198,14 +198,14 @@ { "cell_type": "code", "execution_count": null, - "id": "5a70a7b4", + "id": "5ec949de", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "8a986270", + "id": "8f68dabf", "metadata": {}, "source": [ "#### 11. Create a 3x3 identity matrix (★☆☆)" @@ -214,14 +214,14 @@ { "cell_type": "code", "execution_count": null, - "id": "c681b2ae", + "id": "65bf9ac5", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "b024a4bf", + "id": "f740ed30", "metadata": {}, "source": [ "#### 12. Create a 3x3x3 array with random values (★☆☆)" @@ -230,14 +230,14 @@ { "cell_type": "code", "execution_count": null, - "id": "e7b54796", + "id": "2e6a02fa", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "629c0620", + "id": "0befba5c", "metadata": {}, "source": [ "#### 13. Create a 10x10 array with random values and find the minimum and maximum values (★☆☆)" @@ -246,14 +246,14 @@ { "cell_type": "code", "execution_count": null, - "id": "0f12525b", + "id": "76c90ded", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "25f9f091", + "id": "98d22f5c", "metadata": {}, "source": [ "#### 14. Create a random vector of size 30 and find the mean value (★☆☆)" @@ -262,14 +262,14 @@ { "cell_type": "code", "execution_count": null, - "id": "03dce2ff", + "id": "462c67ff", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "9c4ac621", + "id": "9aa941ad", "metadata": {}, "source": [ "#### 15. Create a 2d array with 1 on the border and 0 inside (★☆☆)" @@ -278,14 +278,14 @@ { "cell_type": "code", "execution_count": null, - "id": "0a811a3b", + "id": "800e919d", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "c0110caf", + "id": "bb1fedb7", "metadata": {}, "source": [ "#### 16. How to add a border (filled with 0's) around an existing array? (★☆☆)" @@ -294,14 +294,14 @@ { "cell_type": "code", "execution_count": null, - "id": "5e461b43", + "id": "a7db7847", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "c097faee", + "id": "650e107f", "metadata": {}, "source": [ "#### 17. What is the result of the following expression? (★☆☆)\n", @@ -318,14 +318,14 @@ { "cell_type": "code", "execution_count": null, - "id": "de307b6d", + "id": "16163e87", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "7539be84", + "id": "212a2d9e", "metadata": {}, "source": [ "#### 18. Create a 5x5 matrix with values 1,2,3,4 just below the diagonal (★☆☆)" @@ -334,14 +334,14 @@ { "cell_type": "code", "execution_count": null, - "id": "897eeebd", + "id": "8fb12d05", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "69207418", + "id": "38c8a0b8", "metadata": {}, "source": [ "#### 19. Create a 8x8 matrix and fill it with a checkerboard pattern (★☆☆)" @@ -350,14 +350,14 @@ { "cell_type": "code", "execution_count": null, - "id": "da9fc94e", + "id": "73de81c7", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "aeb836d2", + "id": "a298d56c", "metadata": {}, "source": [ "#### 20. Consider a (6,7,8) shape array, what is the index (x,y,z) of the 100th element? (★☆☆)" @@ -366,14 +366,14 @@ { "cell_type": "code", "execution_count": null, - "id": "c026c791", + "id": "06bd3ad1", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "9aef01d6", + "id": "bf247eb3", "metadata": {}, "source": [ "#### 21. Create a checkerboard 8x8 matrix using the tile function (★☆☆)" @@ -382,14 +382,14 @@ { "cell_type": "code", "execution_count": null, - "id": "1cdf747e", + "id": "40f5f51a", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "931830f8", + "id": "4111e5bf", "metadata": {}, "source": [ "#### 22. Normalize a 5x5 random matrix (★☆☆)" @@ -398,14 +398,14 @@ { "cell_type": "code", "execution_count": null, - "id": "1d081cd3", + "id": "845e0a38", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "5f3b5379", + "id": "711631f6", "metadata": {}, "source": [ "#### 23. Create a custom dtype that describes a color as four unsigned bytes (RGBA) (★☆☆)" @@ -414,14 +414,14 @@ { "cell_type": "code", "execution_count": null, - "id": "56847b08", + "id": "05c4f307", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "27294dd6", + "id": "f6b0e3fe", "metadata": {}, "source": [ "#### 24. Multiply a 5x3 matrix by a 3x2 matrix (real matrix product) (★☆☆)" @@ -430,14 +430,14 @@ { "cell_type": "code", "execution_count": null, - "id": "5b219d77", + "id": "f92bc78d", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "57a463f5", + "id": "39a05eb5", "metadata": {}, "source": [ "#### 25. Given a 1D array, negate all elements which are between 3 and 8, in place. (★☆☆)" @@ -446,14 +446,14 @@ { "cell_type": "code", "execution_count": null, - "id": "1137097c", + "id": "1a92f1c1", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "4ec37bca", + "id": "d4605cfb", "metadata": {}, "source": [ "#### 26. What is the output of the following script? (★☆☆)\n", @@ -469,14 +469,14 @@ { "cell_type": "code", "execution_count": null, - "id": "ae0f3d30", + "id": "22f602e0", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "fb875416", + "id": "affc0180", "metadata": {}, "source": [ "#### 27. Consider an integer vector Z, which of these expressions are legal? (★☆☆)\n", @@ -493,14 +493,14 @@ { "cell_type": "code", "execution_count": null, - "id": "0b516d7c", + "id": "e288ada9", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "3322bcc4", + "id": "b18a40e7", "metadata": {}, "source": [ "#### 28. What are the result of the following expressions? (★☆☆)\n", @@ -514,14 +514,14 @@ { "cell_type": "code", "execution_count": null, - "id": "68a40ed3", + "id": "169a8c58", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "e85dae57", + "id": "c2e6fa83", "metadata": {}, "source": [ "#### 29. How to round away from zero a float array ? (★☆☆)" @@ -530,14 +530,14 @@ { "cell_type": "code", "execution_count": null, - "id": "8981d8e2", + "id": "883fd972", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "3d78851a", + "id": "77285ae7", "metadata": {}, "source": [ "#### 30. How to find common values between two arrays? (★☆☆)" @@ -546,14 +546,14 @@ { "cell_type": "code", "execution_count": null, - "id": "ca4b0788", + "id": "37922693", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "535563a3", + "id": "0a0c4dc3", "metadata": {}, "source": [ "#### 31. How to ignore all numpy warnings (not recommended)? (★☆☆)" @@ -562,14 +562,14 @@ { "cell_type": "code", "execution_count": null, - "id": "b1884a9f", + "id": "e9d40845", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "9ff18333", + "id": "4700d906", "metadata": {}, "source": [ "#### 32. Is the following expressions true? (★☆☆)\n", @@ -581,14 +581,14 @@ { "cell_type": "code", "execution_count": null, - "id": "b47e3ad5", + "id": "40a41109", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "94dfbdcb", + "id": "a3ef25bc", "metadata": {}, "source": [ "#### 33. How to get the dates of yesterday, today and tomorrow? (★☆☆)" @@ -597,14 +597,14 @@ { "cell_type": "code", "execution_count": null, - "id": "3f686831", + "id": "ed7abca1", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "eb1816a2", + "id": "4d2a3667", "metadata": {}, "source": [ "#### 34. How to get all the dates corresponding to the month of July 2016? (★★☆)" @@ -613,14 +613,14 @@ { "cell_type": "code", "execution_count": null, - "id": "963239fb", + "id": "ffd619f0", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "9174632a", + "id": "e8df2961", "metadata": {}, "source": [ "#### 35. How to compute ((A+B)*(-A/2)) in place (without copy)? (★★☆)" @@ -629,14 +629,14 @@ { "cell_type": "code", "execution_count": null, - "id": "660f5868", + "id": "0399883a", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "b6b4504d", + "id": "9f117716", "metadata": {}, "source": [ "#### 36. Extract the integer part of a random array of positive numbers using 4 different methods (★★☆)" @@ -645,14 +645,14 @@ { "cell_type": "code", "execution_count": null, - "id": "a125254d", + "id": "eea97bde", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "dcd769b0", + "id": "ce178ee0", "metadata": {}, "source": [ "#### 37. Create a 5x5 matrix with row values ranging from 0 to 4 (★★☆)" @@ -661,14 +661,14 @@ { "cell_type": "code", "execution_count": null, - "id": "6e5f39c8", + "id": "a3d9f792", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "83642768", + "id": "0f4ea1a0", "metadata": {}, "source": [ "#### 38. Consider a generator function that generates 10 integers and use it to build an array (★☆☆)" @@ -677,14 +677,14 @@ { "cell_type": "code", "execution_count": null, - "id": "a24bdb56", + "id": "8da1da7a", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "8ab76708", + "id": "7f47a1a7", "metadata": {}, "source": [ "#### 39. Create a vector of size 10 with values ranging from 0 to 1, both excluded (★★☆)" @@ -693,14 +693,14 @@ { "cell_type": "code", "execution_count": null, - "id": "2b66dc48", + "id": "bc941b22", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "a77ffeff", + "id": "cda5555a", "metadata": {}, "source": [ "#### 40. Create a random vector of size 10 and sort it (★★☆)" @@ -709,14 +709,14 @@ { "cell_type": "code", "execution_count": null, - "id": "4bd4613d", + "id": "0b131978", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "0f131622", + "id": "fcb4f465", "metadata": {}, "source": [ "#### 41. How to sum a small array faster than np.sum? (★★☆)" @@ -725,14 +725,14 @@ { "cell_type": "code", "execution_count": null, - "id": "50006b4f", + "id": "060223c0", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "0205ef5d", + "id": "3c3d7234", "metadata": {}, "source": [ "#### 42. Consider two random arrays A and B, check if they are equal (★★☆)" @@ -741,14 +741,14 @@ { "cell_type": "code", "execution_count": null, - "id": "33df1c36", + "id": "f499fcc0", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "be053e5d", + "id": "d99f6e03", "metadata": {}, "source": [ "#### 43. Make an array immutable (read-only) (★★☆)" @@ -757,14 +757,14 @@ { "cell_type": "code", "execution_count": null, - "id": "060a0518", + "id": "69eccc3a", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "92732bec", + "id": "def920df", "metadata": {}, "source": [ "#### 44. Consider a random 10x2 matrix representing cartesian coordinates, convert them to polar coordinates (★★☆)" @@ -773,14 +773,14 @@ { "cell_type": "code", "execution_count": null, - "id": "d4f46c96", + "id": "515f464d", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "1f75774a", + "id": "055ebe42", "metadata": {}, "source": [ "#### 45. Create random vector of size 10 and replace the maximum value by 0 (★★☆)" @@ -789,14 +789,14 @@ { "cell_type": "code", "execution_count": null, - "id": "812354bf", + "id": "bb1f0772", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "ed8431ad", + "id": "620bc824", "metadata": {}, "source": [ "#### 46. Create a structured array with `x` and `y` coordinates covering the [0,1]x[0,1] area (★★☆)" @@ -805,14 +805,14 @@ { "cell_type": "code", "execution_count": null, - "id": "f0966336", + "id": "e5e11349", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "34250c0f", + "id": "94ea37a2", "metadata": {}, "source": [ "#### 47. Given two arrays, X and Y, construct the Cauchy matrix C (Cij =1/(xi - yj)) (★★☆)" @@ -821,14 +821,14 @@ { "cell_type": "code", "execution_count": null, - "id": "e8a7ac1a", + "id": "2f17ac46", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "5418176b", + "id": "00b3d6c7", "metadata": {}, "source": [ "#### 48. Print the minimum and maximum representable values for each numpy scalar type (★★☆)" @@ -837,14 +837,14 @@ { "cell_type": "code", "execution_count": null, - "id": "ad633154", + "id": "b3fe7fae", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "1d868ac4", + "id": "222a56bf", "metadata": {}, "source": [ "#### 49. How to print all the values of an array? (★★☆)" @@ -853,14 +853,14 @@ { "cell_type": "code", "execution_count": null, - "id": "d12c9111", + "id": "ef470264", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "c5f1c082", + "id": "df13c8c7", "metadata": {}, "source": [ "#### 50. How to find the closest value (to a given scalar) in a vector? (★★☆)" @@ -869,14 +869,14 @@ { "cell_type": "code", "execution_count": null, - "id": "4374f950", + "id": "cdc407d4", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "91d0c841", + "id": "37d46c4c", "metadata": {}, "source": [ "#### 51. Create a structured array representing a position (x,y) and a color (r,g,b) (★★☆)" @@ -885,14 +885,14 @@ { "cell_type": "code", "execution_count": null, - "id": "11b8fc88", + "id": "8aa81f1c", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "076d36d7", + "id": "b628672c", "metadata": {}, "source": [ "#### 52. Consider a random vector with shape (100,2) representing coordinates, find point by point distances (★★☆)" @@ -901,14 +901,14 @@ { "cell_type": "code", "execution_count": null, - "id": "14c1ff07", + "id": "72bc8a0c", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "3581ca30", + "id": "4087a52b", "metadata": {}, "source": [ "#### 53. How to convert a float (32 bits) array into an integer (32 bits) array in place?" @@ -917,14 +917,14 @@ { "cell_type": "code", "execution_count": null, - "id": "72732a66", + "id": "d6420a26", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "3f440ff2", + "id": "07ee5bc1", "metadata": {}, "source": [ "#### 54. How to read the following file? (★★☆)\n", @@ -938,14 +938,14 @@ { "cell_type": "code", "execution_count": null, - "id": "9d03d55d", + "id": "0d0a20a3", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "8ef1aff5", + "id": "3c0faef9", "metadata": {}, "source": [ "#### 55. What is the equivalent of enumerate for numpy arrays? (★★☆)" @@ -954,14 +954,14 @@ { "cell_type": "code", "execution_count": null, - "id": "bc1cfb8e", + "id": "646e9f61", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "b8737694", + "id": "99cbb1dd", "metadata": {}, "source": [ "#### 56. Generate a generic 2D Gaussian-like array (★★☆)" @@ -970,14 +970,14 @@ { "cell_type": "code", "execution_count": null, - "id": "e38c976d", + "id": "3b127842", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "1f7a093e", + "id": "976e8fed", "metadata": {}, "source": [ "#### 57. How to randomly place p elements in a 2D array? (★★☆)" @@ -986,14 +986,14 @@ { "cell_type": "code", "execution_count": null, - "id": "aa14eab2", + "id": "aced8bc6", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "4b1f4144", + "id": "e263430b", "metadata": {}, "source": [ "#### 58. Subtract the mean of each row of a matrix (★★☆)" @@ -1002,14 +1002,14 @@ { "cell_type": "code", "execution_count": null, - "id": "39be0ed4", + "id": "b8422f36", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "36c47776", + "id": "65245363", "metadata": {}, "source": [ "#### 59. How to sort an array by the nth column? (★★☆)" @@ -1018,14 +1018,14 @@ { "cell_type": "code", "execution_count": null, - "id": "43867c6a", + "id": "4738c884", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "d798a57f", + "id": "0ebe0c25", "metadata": {}, "source": [ "#### 60. How to tell if a given 2D array has null columns? (★★☆)" @@ -1034,14 +1034,14 @@ { "cell_type": "code", "execution_count": null, - "id": "d2059b5c", + "id": "852bbd9c", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "eee145c4", + "id": "7edfc14e", "metadata": {}, "source": [ "#### 61. Find the nearest value from a given value in an array (★★☆)" @@ -1050,14 +1050,14 @@ { "cell_type": "code", "execution_count": null, - "id": "1b1b5829", + "id": "15c7dd5f", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "745bdd9a", + "id": "e91490a4", "metadata": {}, "source": [ "#### 62. Considering two arrays with shape (1,3) and (3,1), how to compute their sum using an iterator? (★★☆)" @@ -1066,14 +1066,14 @@ { "cell_type": "code", "execution_count": null, - "id": "3a9859f9", + "id": "77a5759d", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "652b80f0", + "id": "0521e979", "metadata": {}, "source": [ "#### 63. Create an array class that has a name attribute (★★☆)" @@ -1082,14 +1082,14 @@ { "cell_type": "code", "execution_count": null, - "id": "cfe2e2a7", + "id": "2d13bf65", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "58349a3a", + "id": "621a1a83", "metadata": {}, "source": [ "#### 64. Consider a given vector, how to add 1 to each element indexed by a second vector (be careful with repeated indices)? (★★★)" @@ -1098,14 +1098,14 @@ { "cell_type": "code", "execution_count": null, - "id": "7d5f94a6", + "id": "2b68677b", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "13247c07", + "id": "a14a48d7", "metadata": {}, "source": [ "#### 65. How to accumulate elements of a vector (X) to an array (F) based on an index list (I)? (★★★)" @@ -1114,14 +1114,14 @@ { "cell_type": "code", "execution_count": null, - "id": "074b5598", + "id": "e2f51a24", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "8996b4be", + "id": "39519e19", "metadata": {}, "source": [ "#### 66. Considering a (w,h,3) image of (dtype=ubyte), compute the number of unique colors (★★☆)" @@ -1130,14 +1130,14 @@ { "cell_type": "code", "execution_count": null, - "id": "4541bada", + "id": "24e2c766", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "1de1a251", + "id": "71fb8c01", "metadata": {}, "source": [ "#### 67. Considering a four dimensions array, how to get sum over the last two axis at once? (★★★)" @@ -1146,14 +1146,14 @@ { "cell_type": "code", "execution_count": null, - "id": "855d4734", + "id": "df855378", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "2c3a49fa", + "id": "f21b484a", "metadata": {}, "source": [ "#### 68. Considering a one-dimensional vector D, how to compute means of subsets of D using a vector S of same size describing subset indices? (★★★)" @@ -1162,14 +1162,14 @@ { "cell_type": "code", "execution_count": null, - "id": "9785a70e", + "id": "19ddb0ea", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "d97e5ce6", + "id": "2190b0f6", "metadata": {}, "source": [ "#### 69. How to get the diagonal of a dot product? (★★★)" @@ -1178,14 +1178,14 @@ { "cell_type": "code", "execution_count": null, - "id": "b8adf4c7", + "id": "7ee08c89", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "59f04026", + "id": "59ad47ed", "metadata": {}, "source": [ "#### 70. Consider the vector [1, 2, 3, 4, 5], how to build a new vector with 3 consecutive zeros interleaved between each value? (★★★)" @@ -1194,14 +1194,14 @@ { "cell_type": "code", "execution_count": null, - "id": "9bf72549", + "id": "adc755f1", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "5915be32", + "id": "5c637715", "metadata": {}, "source": [ "#### 71. Consider an array of dimension (5,5,3), how to multiply it by an array with dimensions (5,5)? (★★★)" @@ -1210,14 +1210,14 @@ { "cell_type": "code", "execution_count": null, - "id": "615685a1", + "id": "650cc21f", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "88afd68d", + "id": "d586e715", "metadata": {}, "source": [ "#### 72. How to swap two rows of an array? (★★★)" @@ -1226,14 +1226,14 @@ { "cell_type": "code", "execution_count": null, - "id": "ab382690", + "id": "c7b20b67", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "103b64e3", + "id": "6f411566", "metadata": {}, "source": [ "#### 73. Consider a set of 10 triplets describing 10 triangles (with shared vertices), find the set of unique line segments composing all the triangles (★★★)" @@ -1242,14 +1242,14 @@ { "cell_type": "code", "execution_count": null, - "id": "ea64e9b8", + "id": "609122b8", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "69e0c2c7", + "id": "59807176", "metadata": {}, "source": [ "#### 74. Given a sorted array C that corresponds to a bincount, how to produce an array A such that np.bincount(A) == C? (★★★)" @@ -1258,14 +1258,14 @@ { "cell_type": "code", "execution_count": null, - "id": "7935210d", + "id": "81663875", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "a217bfc5", + "id": "5146bc0c", "metadata": {}, "source": [ "#### 75. How to compute averages using a sliding window over an array? (★★★)" @@ -1274,14 +1274,14 @@ { "cell_type": "code", "execution_count": null, - "id": "16cd9bde", + "id": "d5d875cd", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "1da33793", + "id": "58e73426", "metadata": {}, "source": [ "#### 76. Consider a one-dimensional array Z, build a two-dimensional array whose first row is (Z[0],Z[1],Z[2]) and each subsequent row is shifted by 1 (last row should be (Z[-3],Z[-2],Z[-1]) (★★★)" @@ -1290,14 +1290,14 @@ { "cell_type": "code", "execution_count": null, - "id": "8059a53b", + "id": "fa414aab", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "f6c798af", + "id": "1ed1562b", "metadata": {}, "source": [ "#### 77. How to negate a boolean, or to change the sign of a float inplace? (★★★)" @@ -1306,14 +1306,14 @@ { "cell_type": "code", "execution_count": null, - "id": "95d52799", + "id": "5635bb46", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "ed6b13c8", + "id": "0bdf5738", "metadata": {}, "source": [ "#### 78. Consider 2 sets of points P0,P1 describing lines (2d) and a point p, how to compute distance from p to each line i (P0[i],P1[i])? (★★★)" @@ -1322,14 +1322,14 @@ { "cell_type": "code", "execution_count": null, - "id": "2870e6b4", + "id": "1b40ceda", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "7f9a94ca", + "id": "d6b2a570", "metadata": {}, "source": [ "#### 79. Consider 2 sets of points P0,P1 describing lines (2d) and a set of points P, how to compute distance from each point j (P[j]) to each line i (P0[i],P1[i])? (★★★)" @@ -1338,14 +1338,14 @@ { "cell_type": "code", "execution_count": null, - "id": "de27d0c4", + "id": "e74d9556", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "61709616", + "id": "10a26db0", "metadata": {}, "source": [ "#### 80. Consider an arbitrary array, write a function that extracts a subpart with a fixed shape and centered on a given element (pad with a `fill` value when necessary) (★★★)" @@ -1354,14 +1354,14 @@ { "cell_type": "code", "execution_count": null, - "id": "952b826a", + "id": "51453c36", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "585a0296", + "id": "0b16fd78", "metadata": {}, "source": [ "#### 81. Consider an array Z = [1,2,3,4,5,6,7,8,9,10,11,12,13,14], how to generate an array R = [[1,2,3,4], [2,3,4,5], [3,4,5,6], ..., [11,12,13,14]]? (★★★)" @@ -1370,14 +1370,14 @@ { "cell_type": "code", "execution_count": null, - "id": "66729fa0", + "id": "22744f52", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "f6ea03b3", + "id": "32d8f28c", "metadata": {}, "source": [ "#### 82. Compute a matrix rank (★★★)" @@ -1386,14 +1386,14 @@ { "cell_type": "code", "execution_count": null, - "id": "80b6d0f4", + "id": "3ee2a152", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "1ef1692e", + "id": "cb129ead", "metadata": {}, "source": [ "#### 83. How to find the most frequent value in an array?" @@ -1402,14 +1402,14 @@ { "cell_type": "code", "execution_count": null, - "id": "de8f66cc", + "id": "c6f253a6", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "310aa04e", + "id": "3035e42a", "metadata": {}, "source": [ "#### 84. Extract all the contiguous 3x3 blocks from a random 10x10 matrix (★★★)" @@ -1418,14 +1418,14 @@ { "cell_type": "code", "execution_count": null, - "id": "f2639e17", + "id": "778e4127", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "cdf46958", + "id": "692fb35b", "metadata": {}, "source": [ "#### 85. Create a 2D array subclass such that Z[i,j] == Z[j,i] (★★★)" @@ -1434,14 +1434,14 @@ { "cell_type": "code", "execution_count": null, - "id": "7f3cb2c9", + "id": "36ae4368", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "9ab6b1ca", + "id": "5412edce", "metadata": {}, "source": [ "#### 86. Consider a set of p matrices with shape (n,n) and a set of p vectors with shape (n,1). How to compute the sum of of the p matrix products at once? (result has shape (n,1)) (★★★)" @@ -1450,14 +1450,14 @@ { "cell_type": "code", "execution_count": null, - "id": "a6f93a7b", + "id": "cbb9912f", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "2c8cd60e", + "id": "d6953705", "metadata": {}, "source": [ "#### 87. Consider a 16x16 array, how to get the block-sum (block size is 4x4)? (★★★)" @@ -1466,14 +1466,14 @@ { "cell_type": "code", "execution_count": null, - "id": "1f8d7f5f", + "id": "8e4fa063", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "cbd5321e", + "id": "e31ffc63", "metadata": {}, "source": [ "#### 88. How to implement the Game of Life using numpy arrays? (★★★)" @@ -1482,14 +1482,14 @@ { "cell_type": "code", "execution_count": null, - "id": "a8bd84fd", + "id": "bcd9e5ed", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "f9c4b416", + "id": "364c7e98", "metadata": {}, "source": [ "#### 89. How to get the n largest values of an array (★★★)" @@ -1498,14 +1498,14 @@ { "cell_type": "code", "execution_count": null, - "id": "c4123ace", + "id": "b5e5a5d9", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "495897e7", + "id": "98eb9cb6", "metadata": {}, "source": [ "#### 90. Given an arbitrary number of vectors, build the cartesian product (every combination of every item) (★★★)" @@ -1514,14 +1514,14 @@ { "cell_type": "code", "execution_count": null, - "id": "9a9ed4a7", + "id": "fee613f4", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "c26bde0b", + "id": "6d934720", "metadata": {}, "source": [ "#### 91. How to create a record array from a regular array? (★★★)" @@ -1530,14 +1530,14 @@ { "cell_type": "code", "execution_count": null, - "id": "4958f3a6", + "id": "b5d5f153", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "d6c5435c", + "id": "db2fc6ec", "metadata": {}, "source": [ "#### 92. Consider a large vector Z, compute Z to the power of 3 using 3 different methods (★★★)" @@ -1546,14 +1546,14 @@ { "cell_type": "code", "execution_count": null, - "id": "5269d1ec", + "id": "df435ed9", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "9f7efc22", + "id": "112fddd0", "metadata": {}, "source": [ "#### 93. Consider two arrays A and B of shape (8,3) and (2,2). How to find rows of A that contain elements of each row of B regardless of the order of the elements in B? (★★★)" @@ -1562,14 +1562,14 @@ { "cell_type": "code", "execution_count": null, - "id": "1d9166aa", + "id": "911ccb7e", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "2bfe9e3c", + "id": "b0e62e04", "metadata": {}, "source": [ "#### 94. Considering a 10x3 matrix, extract rows with unequal values (e.g. [2,2,3]) (★★★)" @@ -1578,14 +1578,14 @@ { "cell_type": "code", "execution_count": null, - "id": "c00f8f84", + "id": "2359edad", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "1bebee22", + "id": "79b7f023", "metadata": {}, "source": [ "#### 95. Convert a vector of ints into a matrix binary representation (★★★)" @@ -1594,14 +1594,14 @@ { "cell_type": "code", "execution_count": null, - "id": "4bf1b73f", + "id": "7fd3d80e", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "37acca61", + "id": "801babdf", "metadata": {}, "source": [ "#### 96. Given a two dimensional array, how to extract unique rows? (★★★)" @@ -1610,14 +1610,14 @@ { "cell_type": "code", "execution_count": null, - "id": "7051b1d7", + "id": "e3f8e896", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "6c8a313c", + "id": "5e447828", "metadata": {}, "source": [ "#### 97. Considering 2 vectors A & B, write the einsum equivalent of inner, outer, sum, and mul function (★★★)" @@ -1626,14 +1626,14 @@ { "cell_type": "code", "execution_count": null, - "id": "7bb32dd1", + "id": "3c6e497c", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "dffed116", + "id": "9e5a85e0", "metadata": {}, "source": [ "#### 98. Considering a path described by two vectors (X,Y), how to sample it using equidistant samples (★★★)?" @@ -1642,14 +1642,14 @@ { "cell_type": "code", "execution_count": null, - "id": "c8a0a05f", + "id": "9643a618", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "e2208f4e", + "id": "8d305a59", "metadata": {}, "source": [ "#### 99. Given an integer n and a 2D array X, select from X the rows which can be interpreted as draws from a multinomial distribution with n degrees, i.e., the rows which only contain integers and which sum to n. (★★★)" @@ -1658,14 +1658,14 @@ { "cell_type": "code", "execution_count": null, - "id": "5a240dba", + "id": "eb6b9159", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "21157036", + "id": "6e2b83fc", "metadata": {}, "source": [ "#### 100. Compute bootstrapped 95% confidence intervals for the mean of a 1D array X (i.e., resample the elements of an array with replacement N times, compute the mean of each sample, and then compute percentiles over the means). (★★★)" @@ -1674,7 +1674,7 @@ { "cell_type": "code", "execution_count": null, - "id": "66c8a5db", + "id": "76d6be8f", "metadata": {}, "outputs": [], "source": [] diff --git a/100_Numpy_exercises_with_hints_with_solutions.md b/100_Numpy_exercises_with_hints_with_solutions.md index 409c00f..633ef1f 100644 --- a/100_Numpy_exercises_with_hints_with_solutions.md +++ b/100_Numpy_exercises_with_hints_with_solutions.md @@ -993,7 +993,8 @@ print(sliding_window_view(Z, window_shape=4)) Z = np.random.uniform(0,1,(10,10)) U, S, V = np.linalg.svd(Z) # Singular Value Decomposition -rank = np.sum(S > 1e-10) +threshold = len(S) * S.max() * np.finfo(S.dtype).eps +rank = np.sum(S > threshold) print(rank) # alternative solution: diff --git a/100_Numpy_exercises_with_solutions.md b/100_Numpy_exercises_with_solutions.md index c7285d2..352c272 100644 --- a/100_Numpy_exercises_with_solutions.md +++ b/100_Numpy_exercises_with_solutions.md @@ -993,7 +993,8 @@ print(sliding_window_view(Z, window_shape=4)) Z = np.random.uniform(0,1,(10,10)) U, S, V = np.linalg.svd(Z) # Singular Value Decomposition -rank = np.sum(S > 1e-10) +threshold = len(S) * S.max() * np.finfo(S.dtype).eps +rank = np.sum(S > threshold) print(rank) # alternative solution: diff --git a/100_Numpy_random.ipynb b/100_Numpy_random.ipynb index 880138a..bf3c433 100644 --- a/100_Numpy_random.ipynb +++ b/100_Numpy_random.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "markdown", - "id": "738eba3f", + "id": "c2a7dc72", "metadata": {}, "source": [ "# 100 numpy exercises\n", @@ -18,7 +18,7 @@ }, { "cell_type": "markdown", - "id": "f65f901e", + "id": "0f9c3a25", "metadata": {}, "source": [ "File automatically generated. See the documentation to update questions/answers/hints programmatically." @@ -26,17 +26,17 @@ }, { "cell_type": "markdown", - "id": "15045647", + "id": "7334326e", "metadata": {}, "source": [ - "Run the `initialize.py` module, then call a random question with `pick()` an hint towards its solution with\n", + "Run the `initialise.py` module, then call a random question with `pick()` an hint towards its solution with\n", "`hint(n)` and the answer with `answer(n)`, where n is the number of the picked question." ] }, { "cell_type": "code", "execution_count": null, - "id": "0d23aa5b", + "id": "abd0d1b2", "metadata": {}, "outputs": [], "source": [ @@ -46,7 +46,7 @@ { "cell_type": "code", "execution_count": null, - "id": "4a6a613b", + "id": "ecfa16ec", "metadata": {}, "outputs": [], "source": [