diff --git a/100_Numpy_exercises.ipynb b/100_Numpy_exercises.ipynb index 9572fb2..a1590c3 100644 --- a/100_Numpy_exercises.ipynb +++ b/100_Numpy_exercises.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "markdown", - "id": "cb151141", + "id": "9a9aa121", "metadata": {}, "source": [ "# 100 numpy exercises\n", @@ -18,7 +18,7 @@ }, { "cell_type": "markdown", - "id": "784fd964", + "id": "8befc7ce", "metadata": {}, "source": [ "File automatically generated. See the documentation to update questions/answers/hints programmatically." @@ -26,7 +26,7 @@ }, { "cell_type": "markdown", - "id": "08520984", + "id": "6f825f1c", "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": "868c08f7", + "id": "703184e0", "metadata": {}, "outputs": [], "source": [ @@ -45,7 +45,7 @@ }, { "cell_type": "markdown", - "id": "0ec7bede", + "id": "d1f04fa5", "metadata": {}, "source": [ "#### 1. Import the numpy package under the name `np` (★☆☆)" @@ -54,14 +54,14 @@ { "cell_type": "code", "execution_count": null, - "id": "a4a9c0c8", + "id": "bc171159", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "26dcb1d4", + "id": "4fb231dc", "metadata": {}, "source": [ "#### 2. Print the numpy version and the configuration (★☆☆)" @@ -70,14 +70,14 @@ { "cell_type": "code", "execution_count": null, - "id": "58ab7192", + "id": "b2ed5c7c", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "30b8499d", + "id": "04caf5c7", "metadata": {}, "source": [ "#### 3. Create a null vector of size 10 (★☆☆)" @@ -86,14 +86,14 @@ { "cell_type": "code", "execution_count": null, - "id": "1585f8ca", + "id": "42e74a30", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "58b8ecf0", + "id": "751cf42e", "metadata": {}, "source": [ "#### 4. How to find the memory size of any array (★☆☆)" @@ -102,14 +102,14 @@ { "cell_type": "code", "execution_count": null, - "id": "4ca0dc06", + "id": "97c5fbab", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "70aae14e", + "id": "88a97f0c", "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": "c1be42e4", + "id": "92abb854", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "cf744af1", + "id": "a7671961", "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": "de0d9872", + "id": "6c38f791", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "2beffa2c", + "id": "8e5fdd72", "metadata": {}, "source": [ "#### 7. Create a vector with values ranging from 10 to 49 (★☆☆)" @@ -150,14 +150,14 @@ { "cell_type": "code", "execution_count": null, - "id": "5c6b8a2c", + "id": "0c911e4b", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "353f9c65", + "id": "de9853d4", "metadata": {}, "source": [ "#### 8. Reverse a vector (first element becomes last) (★☆☆)" @@ -166,14 +166,14 @@ { "cell_type": "code", "execution_count": null, - "id": "687f0be9", + "id": "63ec0dea", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "659c76a6", + "id": "a35c1207", "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": "a6233637", + "id": "bde1723f", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "5dfc54fd", + "id": "9ad0f11f", "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": "fdda7dae", + "id": "959823d4", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "2bc094f3", + "id": "d3c8eac1", "metadata": {}, "source": [ "#### 11. Create a 3x3 identity matrix (★☆☆)" @@ -214,14 +214,14 @@ { "cell_type": "code", "execution_count": null, - "id": "27bf3e3b", + "id": "306a48be", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "91422af9", + "id": "a905079b", "metadata": {}, "source": [ "#### 12. Create a 3x3x3 array with random values (★☆☆)" @@ -230,14 +230,14 @@ { "cell_type": "code", "execution_count": null, - "id": "ac21ab0c", + "id": "3bd26f5d", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "74b39ed8", + "id": "cbf5cf4a", "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": "6d3bfc93", + "id": "a0686c44", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "3529bc8b", + "id": "b322aab7", "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": "e9556f88", + "id": "97dce1ea", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "b29f1fe7", + "id": "101e9f4f", "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": "e09342a0", + "id": "55b6af0c", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "b6c7312a", + "id": "b75a05e9", "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": "aa8b1b7f", + "id": "771b07f2", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "7f8f6271", + "id": "d773a68e", "metadata": {}, "source": [ "#### 17. What is the result of the following expression? (★☆☆)\n", @@ -318,14 +318,14 @@ { "cell_type": "code", "execution_count": null, - "id": "36b105d4", + "id": "5283493c", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "f9357ed6", + "id": "569089c1", "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": "2bafb657", + "id": "acdd4875", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "e5649fea", + "id": "c757729e", "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": "4a551f9a", + "id": "a96e0ca1", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "c08fd1b7", + "id": "63d93126", "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": "c8c5613c", + "id": "22800ac9", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "9e0758ae", + "id": "237998f6", "metadata": {}, "source": [ "#### 21. Create a checkerboard 8x8 matrix using the tile function (★☆☆)" @@ -382,14 +382,14 @@ { "cell_type": "code", "execution_count": null, - "id": "0b9f0957", + "id": "22a86261", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "36d82b39", + "id": "578e1eca", "metadata": {}, "source": [ "#### 22. Normalize a 5x5 random matrix (★☆☆)" @@ -398,14 +398,14 @@ { "cell_type": "code", "execution_count": null, - "id": "77c692a6", + "id": "b07707b1", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "087f5baa", + "id": "57d1fff2", "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": "45165b06", + "id": "1ea1a438", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "d99fdcde", + "id": "08189760", "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": "2c1f45c4", + "id": "2b120295", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "84706b21", + "id": "3fa7d315", "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": "f03c98b1", + "id": "52e10a95", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "1b6fd430", + "id": "6100c656", "metadata": {}, "source": [ "#### 26. What is the output of the following script? (★☆☆)\n", @@ -469,14 +469,14 @@ { "cell_type": "code", "execution_count": null, - "id": "bd5af7e0", + "id": "099d3b5f", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "96abcf8b", + "id": "21cadfdb", "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": "8e54ba57", + "id": "eefdc61f", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "f34bbd85", + "id": "edb05d8c", "metadata": {}, "source": [ "#### 28. What are the result of the following expressions? (★☆☆)\n", @@ -514,14 +514,14 @@ { "cell_type": "code", "execution_count": null, - "id": "23208ae3", + "id": "98cf2ccd", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "74217b07", + "id": "e125e143", "metadata": {}, "source": [ "#### 29. How to round away from zero a float array ? (★☆☆)" @@ -530,14 +530,14 @@ { "cell_type": "code", "execution_count": null, - "id": "1e42f860", + "id": "ac635a3c", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "18d5e602", + "id": "6fbcfde4", "metadata": {}, "source": [ "#### 30. How to find common values between two arrays? (★☆☆)" @@ -546,14 +546,14 @@ { "cell_type": "code", "execution_count": null, - "id": "c1878e1b", + "id": "cef72c3d", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "6fcc2efc", + "id": "448290f7", "metadata": {}, "source": [ "#### 31. How to ignore all numpy warnings (not recommended)? (★☆☆)" @@ -562,14 +562,14 @@ { "cell_type": "code", "execution_count": null, - "id": "7498f5cb", + "id": "570ffcdb", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "5e2eb440", + "id": "a9634713", "metadata": {}, "source": [ "#### 32. Is the following expressions true? (★☆☆)\n", @@ -581,14 +581,14 @@ { "cell_type": "code", "execution_count": null, - "id": "71fe5a09", + "id": "ea730787", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "7bcf1731", + "id": "ca2c7bd0", "metadata": {}, "source": [ "#### 33. How to get the dates of yesterday, today and tomorrow? (★☆☆)" @@ -597,14 +597,14 @@ { "cell_type": "code", "execution_count": null, - "id": "3b681a04", + "id": "e771971c", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "d0d9e4de", + "id": "2ad51e8b", "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": "46a1d176", + "id": "265753fd", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "1efa5c31", + "id": "a58b5c61", "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": "005891a0", + "id": "2a29953f", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "a670be8d", + "id": "76f22ae3", "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": "c738825d", + "id": "0990421d", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "651618f5", + "id": "994a8b81", "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": "72915897", + "id": "3cbaa01b", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "2a26e9f8", + "id": "c7d368b0", "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": "ec282148", + "id": "92c49778", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "30581fe0", + "id": "6adeb79d", "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": "fad42a72", + "id": "21e51b0f", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "bfd3c2d7", + "id": "1cdc3d67", "metadata": {}, "source": [ "#### 40. Create a random vector of size 10 and sort it (★★☆)" @@ -709,14 +709,14 @@ { "cell_type": "code", "execution_count": null, - "id": "f7bd8b7b", + "id": "24d950b4", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "eee0da97", + "id": "a2da25e2", "metadata": {}, "source": [ "#### 41. How to sum a small array faster than np.sum? (★★☆)" @@ -725,14 +725,14 @@ { "cell_type": "code", "execution_count": null, - "id": "7bea9c9e", + "id": "d7ca546d", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "b88921f5", + "id": "19cc3ede", "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": "0624b41e", + "id": "5c055834", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "5fb42a01", + "id": "7df700bc", "metadata": {}, "source": [ "#### 43. Make an array immutable (read-only) (★★☆)" @@ -757,14 +757,14 @@ { "cell_type": "code", "execution_count": null, - "id": "0312f046", + "id": "a5f059a0", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "da433293", + "id": "982e2f7e", "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": "c84b9f28", + "id": "d9423986", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "0ceb0e76", + "id": "b5863c52", "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": "698bdc4a", + "id": "3b65f66f", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "bf680915", + "id": "2a12f2ae", "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": "ee22242f", + "id": "65763e44", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "13bc0fb9", + "id": "9be0eb72", "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": "06096b76", + "id": "d999c9c6", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "9515c015", + "id": "864fda69", "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": "4df42aec", + "id": "03e5dc9e", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "b0daa004", + "id": "e6437089", "metadata": {}, "source": [ "#### 49. How to print all the values of an array? (★★☆)" @@ -853,14 +853,14 @@ { "cell_type": "code", "execution_count": null, - "id": "3f6846f0", + "id": "d0b8972b", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "41b31221", + "id": "59b3f402", "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": "962edb6f", + "id": "9c3702bb", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "41c4ea4e", + "id": "5161a0e8", "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": "f01d3d21", + "id": "fee3d794", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "65463e57", + "id": "f0c0a11e", "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": "f3b1f86c", + "id": "6fdb2124", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "76783da2", + "id": "13007015", "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": "c3d91959", + "id": "6b691b3b", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "d63898c3", + "id": "3acba415", "metadata": {}, "source": [ "#### 54. How to read the following file? (★★☆)\n", @@ -938,14 +938,14 @@ { "cell_type": "code", "execution_count": null, - "id": "e5179e31", + "id": "8505835e", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "5658f323", + "id": "0c46d792", "metadata": {}, "source": [ "#### 55. What is the equivalent of enumerate for numpy arrays? (★★☆)" @@ -954,14 +954,14 @@ { "cell_type": "code", "execution_count": null, - "id": "3e1a0084", + "id": "85fdeb4a", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "958747e3", + "id": "9e276331", "metadata": {}, "source": [ "#### 56. Generate a generic 2D Gaussian-like array (★★☆)" @@ -970,14 +970,14 @@ { "cell_type": "code", "execution_count": null, - "id": "754c9a82", + "id": "5f1d784a", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "1605b330", + "id": "da594a47", "metadata": {}, "source": [ "#### 57. How to randomly place p elements in a 2D array? (★★☆)" @@ -986,14 +986,14 @@ { "cell_type": "code", "execution_count": null, - "id": "1cb5cb80", + "id": "b4f5aedd", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "662b72e6", + "id": "5a8e20eb", "metadata": {}, "source": [ "#### 58. Subtract the mean of each row of a matrix (★★☆)" @@ -1002,14 +1002,14 @@ { "cell_type": "code", "execution_count": null, - "id": "5bf1bb4a", + "id": "0850bec6", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "c4459342", + "id": "529d3c17", "metadata": {}, "source": [ "#### 59. How to sort an array by the nth column? (★★☆)" @@ -1018,14 +1018,14 @@ { "cell_type": "code", "execution_count": null, - "id": "e71a61dc", + "id": "0d401082", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "738adb27", + "id": "0f6680ae", "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": "1d098fa1", + "id": "fe60a4d0", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "cd77a8ec", + "id": "f33c3a84", "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": "2bead823", + "id": "d45b9371", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "ba3c7fb5", + "id": "c2f889ed", "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": "91309123", + "id": "57ea3125", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "09fb6f82", + "id": "dcb09dcd", "metadata": {}, "source": [ "#### 63. Create an array class that has a name attribute (★★☆)" @@ -1082,14 +1082,14 @@ { "cell_type": "code", "execution_count": null, - "id": "c603ae4c", + "id": "d7bd3904", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "dd7b5612", + "id": "b0e91553", "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": "5b0cc2b0", + "id": "3affc760", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "ba79212e", + "id": "e0385252", "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": "fbdf4dd2", + "id": "c9e111a0", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "83d3b69c", + "id": "30ceeeb5", "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": "0792905d", + "id": "a07bac48", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "8de63a9f", + "id": "f2c4f7fe", "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": "34160d8e", + "id": "377e2aa2", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "2b347e90", + "id": "0748b433", "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": "ec0fafa4", + "id": "458ead5a", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "78d1f336", + "id": "fca120bf", "metadata": {}, "source": [ "#### 69. How to get the diagonal of a dot product? (★★★)" @@ -1178,14 +1178,14 @@ { "cell_type": "code", "execution_count": null, - "id": "2c20632c", + "id": "aabe682a", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "e9047d74", + "id": "a53781d5", "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": "08effab1", + "id": "f596a9f0", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "d15f1b2a", + "id": "8d4c4bcc", "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": "f2512d71", + "id": "7eb126d1", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "357515b9", + "id": "870c6b3e", "metadata": {}, "source": [ "#### 72. How to swap two rows of an array? (★★★)" @@ -1226,14 +1226,14 @@ { "cell_type": "code", "execution_count": null, - "id": "4725e0e7", + "id": "018a1fce", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "6efa29de", + "id": "f807b95a", "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": "a0715f3e", + "id": "182d7ab8", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "d65caefc", + "id": "127d6162", "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": "6ac1c530", + "id": "58bfa175", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "aac707b9", + "id": "bea3a91d", "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": "e78bf2ba", + "id": "3da93cb6", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "9721e468", + "id": "71481034", "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": "f6207c2f", + "id": "b1419349", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "63cda36b", + "id": "29041930", "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": "f9cfc9b5", + "id": "0f8c2129", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "a01442f6", + "id": "75fd8ebf", "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": "f65638f6", + "id": "e08558dc", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "6920d21b", + "id": "1248ffc8", "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": "2b3995f5", + "id": "53f1708e", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "72e3d131", + "id": "76875c5a", "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": "2b3fbbc0", + "id": "165687fa", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "8236d9e5", + "id": "cdaf9927", "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": "0d01bac8", + "id": "f2312a8b", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "69aef8a2", + "id": "f60ed7e8", "metadata": {}, "source": [ "#### 82. Compute a matrix rank (★★★)" @@ -1386,14 +1386,14 @@ { "cell_type": "code", "execution_count": null, - "id": "d52171f3", + "id": "53d7964b", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "ffea816d", + "id": "9df16ebf", "metadata": {}, "source": [ "#### 83. How to find the most frequent value in an array?" @@ -1402,14 +1402,14 @@ { "cell_type": "code", "execution_count": null, - "id": "b6755599", + "id": "b4ee330e", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "28b9ebd5", + "id": "730664dc", "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": "dc4d87a9", + "id": "a828d6f9", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "c9dd1f89", + "id": "8734d0ef", "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": "f9a61154", + "id": "35f465a1", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "1fef0eaf", + "id": "bb4493e6", "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": "8ce59597", + "id": "5f3d2af1", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "b432745a", + "id": "c4da8597", "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": "7e4bc3ae", + "id": "f6ea8bcd", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "3c619372", + "id": "c0fab898", "metadata": {}, "source": [ "#### 88. How to implement the Game of Life using numpy arrays? (★★★)" @@ -1482,14 +1482,14 @@ { "cell_type": "code", "execution_count": null, - "id": "311afced", + "id": "7265ae17", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "72de3599", + "id": "376e5084", "metadata": {}, "source": [ "#### 89. How to get the n largest values of an array (★★★)" @@ -1498,14 +1498,14 @@ { "cell_type": "code", "execution_count": null, - "id": "3580a003", + "id": "4141f5ab", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "f7d1f478", + "id": "391538a7", "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": "adf3192a", + "id": "2d969dbe", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "5891cf3d", + "id": "53187ef3", "metadata": {}, "source": [ "#### 91. How to create a record array from a regular array? (★★★)" @@ -1530,14 +1530,14 @@ { "cell_type": "code", "execution_count": null, - "id": "c8b908d2", + "id": "44ae3756", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "69d4f84d", + "id": "6702714f", "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": "53d7a73a", + "id": "5fd2ce21", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "409ee0f6", + "id": "4a093a17", "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": "dce81ba7", + "id": "c51bb071", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "a7c15528", + "id": "78e0b17f", "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": "86138a0d", + "id": "aa3b5e56", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "47d8c777", + "id": "531cf623", "metadata": {}, "source": [ "#### 95. Convert a vector of ints into a matrix binary representation (★★★)" @@ -1594,14 +1594,14 @@ { "cell_type": "code", "execution_count": null, - "id": "133a8992", + "id": "140abfed", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "08cd42bf", + "id": "c7e0f77f", "metadata": {}, "source": [ "#### 96. Given a two dimensional array, how to extract unique rows? (★★★)" @@ -1610,14 +1610,14 @@ { "cell_type": "code", "execution_count": null, - "id": "7ab043a3", + "id": "b3add5bb", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "503d5c03", + "id": "f85e3d17", "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": "2a469c8b", + "id": "5d8603d7", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "e6839e1b", + "id": "f1bae5a9", "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": "218a2ce0", + "id": "5e40ac5c", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "112e5f75", + "id": "ef87eba2", "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": "bd70252e", + "id": "e8de02d6", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "ef5a1a4b", + "id": "e2b693f7", "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": "459973ba", + "id": "cd36404a", "metadata": {}, "outputs": [], "source": [] diff --git a/100_Numpy_exercises_with_hints_with_solutions.md b/100_Numpy_exercises_with_hints_with_solutions.md index 08e2fd9..15c40cb 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 -!python -c "import numpy; numpy.info(numpy.add)" +%run `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 80bd9d8..1032820 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 -!python -c "import numpy; numpy.info(numpy.add)" +%run `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 7628390..f80bcf4 100644 --- a/100_Numpy_random.ipynb +++ b/100_Numpy_random.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "markdown", - "id": "a0f6fafe", + "id": "94b10be0", "metadata": {}, "source": [ "# 100 numpy exercises\n", @@ -18,7 +18,7 @@ }, { "cell_type": "markdown", - "id": "b9776715", + "id": "1d0b9900", "metadata": {}, "source": [ "File automatically generated. See the documentation to update questions/answers/hints programmatically." @@ -26,7 +26,7 @@ }, { "cell_type": "markdown", - "id": "886a415d", + "id": "21313ae9", "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": "0e099906", + "id": "c4ad3a14", "metadata": {}, "outputs": [], "source": [ @@ -46,7 +46,7 @@ { "cell_type": "code", "execution_count": null, - "id": "3d14e103", + "id": "13bdf6d0", "metadata": {}, "outputs": [], "source": [