diff --git a/100_Numpy_exercises.ipynb b/100_Numpy_exercises.ipynb index a1590c3..d3c304b 100644 --- a/100_Numpy_exercises.ipynb +++ b/100_Numpy_exercises.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "markdown", - "id": "9a9aa121", + "id": "2e24f5a9", "metadata": {}, "source": [ "# 100 numpy exercises\n", @@ -18,7 +18,7 @@ }, { "cell_type": "markdown", - "id": "8befc7ce", + "id": "aeed19bf", "metadata": {}, "source": [ "File automatically generated. See the documentation to update questions/answers/hints programmatically." @@ -26,7 +26,7 @@ }, { "cell_type": "markdown", - "id": "6f825f1c", + "id": "cb8da270", "metadata": {}, "source": [ "Run the `initialise.py` module, then for each question you can query the\n", @@ -36,7 +36,7 @@ { "cell_type": "code", "execution_count": null, - "id": "703184e0", + "id": "44566d06", "metadata": {}, "outputs": [], "source": [ @@ -45,7 +45,7 @@ }, { "cell_type": "markdown", - "id": "d1f04fa5", + "id": "f5b8fa07", "metadata": {}, "source": [ "#### 1. Import the numpy package under the name `np` (★☆☆)" @@ -54,14 +54,14 @@ { "cell_type": "code", "execution_count": null, - "id": "bc171159", + "id": "d586938d", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "4fb231dc", + "id": "f378e996", "metadata": {}, "source": [ "#### 2. Print the numpy version and the configuration (★☆☆)" @@ -70,14 +70,14 @@ { "cell_type": "code", "execution_count": null, - "id": "b2ed5c7c", + "id": "f239070d", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "04caf5c7", + "id": "c07ef237", "metadata": {}, "source": [ "#### 3. Create a null vector of size 10 (★☆☆)" @@ -86,14 +86,14 @@ { "cell_type": "code", "execution_count": null, - "id": "42e74a30", + "id": "ead039ba", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "751cf42e", + "id": "fac81ee2", "metadata": {}, "source": [ "#### 4. How to find the memory size of any array (★☆☆)" @@ -102,14 +102,14 @@ { "cell_type": "code", "execution_count": null, - "id": "97c5fbab", + "id": "95f5e372", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "88a97f0c", + "id": "59751f5d", "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": "92abb854", + "id": "7fd099b0", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "a7671961", + "id": "5a5b1090", "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": "6c38f791", + "id": "e0658ef5", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "8e5fdd72", + "id": "a6b69ea5", "metadata": {}, "source": [ "#### 7. Create a vector with values ranging from 10 to 49 (★☆☆)" @@ -150,14 +150,14 @@ { "cell_type": "code", "execution_count": null, - "id": "0c911e4b", + "id": "8f292d6a", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "de9853d4", + "id": "544b923a", "metadata": {}, "source": [ "#### 8. Reverse a vector (first element becomes last) (★☆☆)" @@ -166,14 +166,14 @@ { "cell_type": "code", "execution_count": null, - "id": "63ec0dea", + "id": "e39552ad", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "a35c1207", + "id": "80dac20e", "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": "bde1723f", + "id": "6eee661c", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "9ad0f11f", + "id": "bf2c6bc3", "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": "959823d4", + "id": "c325f380", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "d3c8eac1", + "id": "f2086e9f", "metadata": {}, "source": [ "#### 11. Create a 3x3 identity matrix (★☆☆)" @@ -214,14 +214,14 @@ { "cell_type": "code", "execution_count": null, - "id": "306a48be", + "id": "e572f7ba", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "a905079b", + "id": "33ecfac9", "metadata": {}, "source": [ "#### 12. Create a 3x3x3 array with random values (★☆☆)" @@ -230,14 +230,14 @@ { "cell_type": "code", "execution_count": null, - "id": "3bd26f5d", + "id": "0bb38bf9", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "cbf5cf4a", + "id": "7fb26b12", "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": "a0686c44", + "id": "d09ce056", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "b322aab7", + "id": "fa802167", "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": "97dce1ea", + "id": "b39eff27", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "101e9f4f", + "id": "80d1a395", "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": "55b6af0c", + "id": "c1e98c8f", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "b75a05e9", + "id": "576687ed", "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": "771b07f2", + "id": "c47afd91", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "d773a68e", + "id": "8f48f7e1", "metadata": {}, "source": [ "#### 17. What is the result of the following expression? (★☆☆)\n", @@ -318,14 +318,14 @@ { "cell_type": "code", "execution_count": null, - "id": "5283493c", + "id": "90b87a10", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "569089c1", + "id": "60aae59a", "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": "acdd4875", + "id": "04487663", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "c757729e", + "id": "b9876d56", "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": "a96e0ca1", + "id": "1adaacb1", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "63d93126", + "id": "d1fe6fa2", "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": "22800ac9", + "id": "264230b9", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "237998f6", + "id": "10368e34", "metadata": {}, "source": [ "#### 21. Create a checkerboard 8x8 matrix using the tile function (★☆☆)" @@ -382,14 +382,14 @@ { "cell_type": "code", "execution_count": null, - "id": "22a86261", + "id": "d40e42ba", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "578e1eca", + "id": "88f1cee7", "metadata": {}, "source": [ "#### 22. Normalize a 5x5 random matrix (★☆☆)" @@ -398,14 +398,14 @@ { "cell_type": "code", "execution_count": null, - "id": "b07707b1", + "id": "2204ded1", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "57d1fff2", + "id": "420d6732", "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": "1ea1a438", + "id": "54344d5e", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "08189760", + "id": "8188397c", "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": "2b120295", + "id": "e7819113", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "3fa7d315", + "id": "96a2427a", "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": "52e10a95", + "id": "278a50ca", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "6100c656", + "id": "5d62d4a6", "metadata": {}, "source": [ "#### 26. What is the output of the following script? (★☆☆)\n", @@ -469,14 +469,14 @@ { "cell_type": "code", "execution_count": null, - "id": "099d3b5f", + "id": "3e4c5106", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "21cadfdb", + "id": "698263af", "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": "eefdc61f", + "id": "d3b4dce4", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "edb05d8c", + "id": "34114266", "metadata": {}, "source": [ "#### 28. What are the result of the following expressions? (★☆☆)\n", @@ -514,14 +514,14 @@ { "cell_type": "code", "execution_count": null, - "id": "98cf2ccd", + "id": "c1ee584d", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "e125e143", + "id": "6ae31252", "metadata": {}, "source": [ "#### 29. How to round away from zero a float array ? (★☆☆)" @@ -530,14 +530,14 @@ { "cell_type": "code", "execution_count": null, - "id": "ac635a3c", + "id": "bb2ce98d", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "6fbcfde4", + "id": "cf7847ab", "metadata": {}, "source": [ "#### 30. How to find common values between two arrays? (★☆☆)" @@ -546,14 +546,14 @@ { "cell_type": "code", "execution_count": null, - "id": "cef72c3d", + "id": "9106d2ef", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "448290f7", + "id": "ba807b80", "metadata": {}, "source": [ "#### 31. How to ignore all numpy warnings (not recommended)? (★☆☆)" @@ -562,14 +562,14 @@ { "cell_type": "code", "execution_count": null, - "id": "570ffcdb", + "id": "572f8752", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "a9634713", + "id": "a25d16c8", "metadata": {}, "source": [ "#### 32. Is the following expressions true? (★☆☆)\n", @@ -581,14 +581,14 @@ { "cell_type": "code", "execution_count": null, - "id": "ea730787", + "id": "91e261fc", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "ca2c7bd0", + "id": "74a83f41", "metadata": {}, "source": [ "#### 33. How to get the dates of yesterday, today and tomorrow? (★☆☆)" @@ -597,14 +597,14 @@ { "cell_type": "code", "execution_count": null, - "id": "e771971c", + "id": "e0591dc3", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "2ad51e8b", + "id": "4de06358", "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": "265753fd", + "id": "4e52bdbd", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "a58b5c61", + "id": "a6573321", "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": "2a29953f", + "id": "5079b2bc", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "76f22ae3", + "id": "f543d80f", "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": "0990421d", + "id": "d7fe6792", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "994a8b81", + "id": "58ba275f", "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": "3cbaa01b", + "id": "74164472", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "c7d368b0", + "id": "8872747f", "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": "92c49778", + "id": "063882ec", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "6adeb79d", + "id": "a5ee79a3", "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": "21e51b0f", + "id": "e156e66f", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "1cdc3d67", + "id": "d04b2bc7", "metadata": {}, "source": [ "#### 40. Create a random vector of size 10 and sort it (★★☆)" @@ -709,14 +709,14 @@ { "cell_type": "code", "execution_count": null, - "id": "24d950b4", + "id": "cff22bfd", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "a2da25e2", + "id": "5586ac55", "metadata": {}, "source": [ "#### 41. How to sum a small array faster than np.sum? (★★☆)" @@ -725,14 +725,14 @@ { "cell_type": "code", "execution_count": null, - "id": "d7ca546d", + "id": "73421511", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "19cc3ede", + "id": "6a532f49", "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": "5c055834", + "id": "4d5ba6a7", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "7df700bc", + "id": "d761dfc8", "metadata": {}, "source": [ "#### 43. Make an array immutable (read-only) (★★☆)" @@ -757,14 +757,14 @@ { "cell_type": "code", "execution_count": null, - "id": "a5f059a0", + "id": "1530351d", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "982e2f7e", + "id": "d94043cc", "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": "d9423986", + "id": "85a157ac", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "b5863c52", + "id": "25ef290c", "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": "3b65f66f", + "id": "13af9d9b", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "2a12f2ae", + "id": "b3d2f592", "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": "65763e44", + "id": "df412f13", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "9be0eb72", + "id": "f0ccd315", "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": "d999c9c6", + "id": "89332c98", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "864fda69", + "id": "48834278", "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": "03e5dc9e", + "id": "276bb5c7", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "e6437089", + "id": "9e31edaf", "metadata": {}, "source": [ "#### 49. How to print all the values of an array? (★★☆)" @@ -853,14 +853,14 @@ { "cell_type": "code", "execution_count": null, - "id": "d0b8972b", + "id": "9fb2c960", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "59b3f402", + "id": "0ce7b2c5", "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": "9c3702bb", + "id": "63168b52", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "5161a0e8", + "id": "d8814ddf", "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": "fee3d794", + "id": "7dc9b51c", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "f0c0a11e", + "id": "a1b639cc", "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": "6fdb2124", + "id": "4d5bd1b3", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "13007015", + "id": "f4241884", "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": "6b691b3b", + "id": "4cc9a908", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "3acba415", + "id": "e371981c", "metadata": {}, "source": [ "#### 54. How to read the following file? (★★☆)\n", @@ -938,14 +938,14 @@ { "cell_type": "code", "execution_count": null, - "id": "8505835e", + "id": "86972b97", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "0c46d792", + "id": "7bd26c11", "metadata": {}, "source": [ "#### 55. What is the equivalent of enumerate for numpy arrays? (★★☆)" @@ -954,14 +954,14 @@ { "cell_type": "code", "execution_count": null, - "id": "85fdeb4a", + "id": "3c2a9898", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "9e276331", + "id": "6188312b", "metadata": {}, "source": [ "#### 56. Generate a generic 2D Gaussian-like array (★★☆)" @@ -970,14 +970,14 @@ { "cell_type": "code", "execution_count": null, - "id": "5f1d784a", + "id": "12843095", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "da594a47", + "id": "9e8e5ff6", "metadata": {}, "source": [ "#### 57. How to randomly place p elements in a 2D array? (★★☆)" @@ -986,14 +986,14 @@ { "cell_type": "code", "execution_count": null, - "id": "b4f5aedd", + "id": "d6d38c51", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "5a8e20eb", + "id": "44b82ae3", "metadata": {}, "source": [ "#### 58. Subtract the mean of each row of a matrix (★★☆)" @@ -1002,14 +1002,14 @@ { "cell_type": "code", "execution_count": null, - "id": "0850bec6", + "id": "69a0c96c", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "529d3c17", + "id": "e23e5478", "metadata": {}, "source": [ "#### 59. How to sort an array by the nth column? (★★☆)" @@ -1018,14 +1018,14 @@ { "cell_type": "code", "execution_count": null, - "id": "0d401082", + "id": "3e6487e7", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "0f6680ae", + "id": "23a29dee", "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": "fe60a4d0", + "id": "91740fdc", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "f33c3a84", + "id": "784e8175", "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": "d45b9371", + "id": "ee4b7989", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "c2f889ed", + "id": "cdb6399a", "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": "57ea3125", + "id": "69155208", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "dcb09dcd", + "id": "15a12d09", "metadata": {}, "source": [ "#### 63. Create an array class that has a name attribute (★★☆)" @@ -1082,14 +1082,14 @@ { "cell_type": "code", "execution_count": null, - "id": "d7bd3904", + "id": "1aed42b2", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "b0e91553", + "id": "7c1f2c44", "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": "3affc760", + "id": "699018f0", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "e0385252", + "id": "e05e4158", "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": "c9e111a0", + "id": "4f0df204", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "30ceeeb5", + "id": "2ee6e9fb", "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": "a07bac48", + "id": "1f828aeb", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "f2c4f7fe", + "id": "685b42c8", "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": "377e2aa2", + "id": "19f00ff1", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "0748b433", + "id": "2cd40204", "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": "458ead5a", + "id": "e2bb323f", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "fca120bf", + "id": "f1033a57", "metadata": {}, "source": [ "#### 69. How to get the diagonal of a dot product? (★★★)" @@ -1178,14 +1178,14 @@ { "cell_type": "code", "execution_count": null, - "id": "aabe682a", + "id": "03c551d4", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "a53781d5", + "id": "96d038b8", "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": "f596a9f0", + "id": "d729100f", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "8d4c4bcc", + "id": "04a70c47", "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": "7eb126d1", + "id": "66930de6", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "870c6b3e", + "id": "2629ed58", "metadata": {}, "source": [ "#### 72. How to swap two rows of an array? (★★★)" @@ -1226,14 +1226,14 @@ { "cell_type": "code", "execution_count": null, - "id": "018a1fce", + "id": "1f079625", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "f807b95a", + "id": "5c3082ae", "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": "182d7ab8", + "id": "7df23ad4", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "127d6162", + "id": "bd0af730", "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": "58bfa175", + "id": "09753516", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "bea3a91d", + "id": "9fe326b0", "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": "3da93cb6", + "id": "48bfe845", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "71481034", + "id": "686ede4c", "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": "b1419349", + "id": "e3c9cbdf", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "29041930", + "id": "c2f45eda", "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": "0f8c2129", + "id": "7a2da55a", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "75fd8ebf", + "id": "4bd0c2a1", "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": "e08558dc", + "id": "bb163fe1", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "1248ffc8", + "id": "326db9c8", "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": "53f1708e", + "id": "b8e4042b", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "76875c5a", + "id": "f2d94a4a", "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": "165687fa", + "id": "dc8f8cca", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "cdaf9927", + "id": "c81f6f9b", "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": "f2312a8b", + "id": "7c9737aa", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "f60ed7e8", + "id": "35becc31", "metadata": {}, "source": [ "#### 82. Compute a matrix rank (★★★)" @@ -1386,14 +1386,14 @@ { "cell_type": "code", "execution_count": null, - "id": "53d7964b", + "id": "83902568", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "9df16ebf", + "id": "b866c7bc", "metadata": {}, "source": [ "#### 83. How to find the most frequent value in an array?" @@ -1402,14 +1402,14 @@ { "cell_type": "code", "execution_count": null, - "id": "b4ee330e", + "id": "f3ab9659", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "730664dc", + "id": "5d8ae7f0", "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": "a828d6f9", + "id": "a4bab033", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "8734d0ef", + "id": "cae4c076", "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": "35f465a1", + "id": "a70fa139", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "bb4493e6", + "id": "3800c0ce", "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": "5f3d2af1", + "id": "0fc258a1", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "c4da8597", + "id": "34af0c30", "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": "f6ea8bcd", + "id": "eb089155", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "c0fab898", + "id": "dc504d48", "metadata": {}, "source": [ "#### 88. How to implement the Game of Life using numpy arrays? (★★★)" @@ -1482,14 +1482,14 @@ { "cell_type": "code", "execution_count": null, - "id": "7265ae17", + "id": "07d7529a", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "376e5084", + "id": "1d656a40", "metadata": {}, "source": [ "#### 89. How to get the n largest values of an array (★★★)" @@ -1498,14 +1498,14 @@ { "cell_type": "code", "execution_count": null, - "id": "4141f5ab", + "id": "9e6c0153", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "391538a7", + "id": "d85fb529", "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": "2d969dbe", + "id": "4f2de833", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "53187ef3", + "id": "30e08504", "metadata": {}, "source": [ "#### 91. How to create a record array from a regular array? (★★★)" @@ -1530,14 +1530,14 @@ { "cell_type": "code", "execution_count": null, - "id": "44ae3756", + "id": "73cf4fd7", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "6702714f", + "id": "cac23740", "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": "5fd2ce21", + "id": "bb0b1f62", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "4a093a17", + "id": "d883be69", "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": "c51bb071", + "id": "560070dc", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "78e0b17f", + "id": "7f8e79e3", "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": "aa3b5e56", + "id": "816e321e", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "531cf623", + "id": "8c84b1bb", "metadata": {}, "source": [ "#### 95. Convert a vector of ints into a matrix binary representation (★★★)" @@ -1594,14 +1594,14 @@ { "cell_type": "code", "execution_count": null, - "id": "140abfed", + "id": "831c8a0d", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "c7e0f77f", + "id": "3338c3c7", "metadata": {}, "source": [ "#### 96. Given a two dimensional array, how to extract unique rows? (★★★)" @@ -1610,14 +1610,14 @@ { "cell_type": "code", "execution_count": null, - "id": "b3add5bb", + "id": "8f46b166", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "f85e3d17", + "id": "952f1a97", "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": "5d8603d7", + "id": "5b330952", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "f1bae5a9", + "id": "7c250535", "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": "5e40ac5c", + "id": "1028c25b", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "ef87eba2", + "id": "22ac5051", "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": "e8de02d6", + "id": "be6e23f2", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "e2b693f7", + "id": "d376c97c", "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": "cd36404a", + "id": "8c9b4471", "metadata": {}, "outputs": [], "source": [] diff --git a/100_Numpy_exercises_with_hints_with_solutions.md b/100_Numpy_exercises_with_hints_with_solutions.md index 15c40cb..08e2fd9 100644 --- a/100_Numpy_exercises_with_hints_with_solutions.md +++ b/100_Numpy_exercises_with_hints_with_solutions.md @@ -46,7 +46,7 @@ print("%d bytes" % Z.nbytes) `hint: np.info` ```python -%run `python -c "import numpy; numpy.info(numpy.add)"` +!python -c "import numpy; numpy.info(numpy.add)" ``` #### 6. Create a null vector of size 10 but the fifth value which is 1 (★☆☆) `hint: array[4]` diff --git a/100_Numpy_exercises_with_solutions.md b/100_Numpy_exercises_with_solutions.md index 1032820..80bd9d8 100644 --- a/100_Numpy_exercises_with_solutions.md +++ b/100_Numpy_exercises_with_solutions.md @@ -46,7 +46,7 @@ print("%d bytes" % Z.nbytes) ```python -%run `python -c "import numpy; numpy.info(numpy.add)"` +!python -c "import numpy; numpy.info(numpy.add)" ``` #### 6. Create a null vector of size 10 but the fifth value which is 1 (★☆☆) diff --git a/100_Numpy_random.ipynb b/100_Numpy_random.ipynb index f80bcf4..3dbb0d5 100644 --- a/100_Numpy_random.ipynb +++ b/100_Numpy_random.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "markdown", - "id": "94b10be0", + "id": "4a87d009", "metadata": {}, "source": [ "# 100 numpy exercises\n", @@ -18,7 +18,7 @@ }, { "cell_type": "markdown", - "id": "1d0b9900", + "id": "16b631f6", "metadata": {}, "source": [ "File automatically generated. See the documentation to update questions/answers/hints programmatically." @@ -26,7 +26,7 @@ }, { "cell_type": "markdown", - "id": "21313ae9", + "id": "8c00c264", "metadata": {}, "source": [ "Run the `initialise.py` module, then call a random question with `pick()` an hint towards its solution with\n", @@ -36,7 +36,7 @@ { "cell_type": "code", "execution_count": null, - "id": "c4ad3a14", + "id": "c5fa0b45", "metadata": {}, "outputs": [], "source": [ @@ -46,7 +46,7 @@ { "cell_type": "code", "execution_count": null, - "id": "13bdf6d0", + "id": "6c4f9753", "metadata": {}, "outputs": [], "source": [