diff --git a/100_Numpy_exercises.ipynb b/100_Numpy_exercises.ipynb index d3c304b..3a7edbd 100644 --- a/100_Numpy_exercises.ipynb +++ b/100_Numpy_exercises.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "markdown", - "id": "2e24f5a9", + "id": "44d97bac", "metadata": {}, "source": [ "# 100 numpy exercises\n", @@ -18,7 +18,7 @@ }, { "cell_type": "markdown", - "id": "aeed19bf", + "id": "22920094", "metadata": {}, "source": [ "File automatically generated. See the documentation to update questions/answers/hints programmatically." @@ -26,7 +26,7 @@ }, { "cell_type": "markdown", - "id": "cb8da270", + "id": "bf26bed4", "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": "44566d06", + "id": "1fbf8f40", "metadata": {}, "outputs": [], "source": [ @@ -45,7 +45,7 @@ }, { "cell_type": "markdown", - "id": "f5b8fa07", + "id": "f82fc1fe", "metadata": {}, "source": [ "#### 1. Import the numpy package under the name `np` (★☆☆)" @@ -54,14 +54,14 @@ { "cell_type": "code", "execution_count": null, - "id": "d586938d", + "id": "3ce4a75b", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "f378e996", + "id": "9f52dd8f", "metadata": {}, "source": [ "#### 2. Print the numpy version and the configuration (★☆☆)" @@ -70,14 +70,14 @@ { "cell_type": "code", "execution_count": null, - "id": "f239070d", + "id": "9d650aad", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "c07ef237", + "id": "2e9b3d85", "metadata": {}, "source": [ "#### 3. Create a null vector of size 10 (★☆☆)" @@ -86,14 +86,14 @@ { "cell_type": "code", "execution_count": null, - "id": "ead039ba", + "id": "82efe018", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "fac81ee2", + "id": "e5fcb60f", "metadata": {}, "source": [ "#### 4. How to find the memory size of any array (★☆☆)" @@ -102,14 +102,14 @@ { "cell_type": "code", "execution_count": null, - "id": "95f5e372", + "id": "11b7d769", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "59751f5d", + "id": "beca6a94", "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": "7fd099b0", + "id": "2a71a96e", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "5a5b1090", + "id": "d785b786", "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": "e0658ef5", + "id": "9a23c28e", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "a6b69ea5", + "id": "d338b31b", "metadata": {}, "source": [ "#### 7. Create a vector with values ranging from 10 to 49 (★☆☆)" @@ -150,14 +150,14 @@ { "cell_type": "code", "execution_count": null, - "id": "8f292d6a", + "id": "9d1b76ef", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "544b923a", + "id": "788ff092", "metadata": {}, "source": [ "#### 8. Reverse a vector (first element becomes last) (★☆☆)" @@ -166,14 +166,14 @@ { "cell_type": "code", "execution_count": null, - "id": "e39552ad", + "id": "bb3ff218", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "80dac20e", + "id": "e956caaa", "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": "6eee661c", + "id": "42691982", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "bf2c6bc3", + "id": "3869a3da", "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": "c325f380", + "id": "1688d184", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "f2086e9f", + "id": "504726a6", "metadata": {}, "source": [ "#### 11. Create a 3x3 identity matrix (★☆☆)" @@ -214,14 +214,14 @@ { "cell_type": "code", "execution_count": null, - "id": "e572f7ba", + "id": "ea5d379b", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "33ecfac9", + "id": "1f897d7c", "metadata": {}, "source": [ "#### 12. Create a 3x3x3 array with random values (★☆☆)" @@ -230,14 +230,14 @@ { "cell_type": "code", "execution_count": null, - "id": "0bb38bf9", + "id": "49ad0a78", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "7fb26b12", + "id": "3fce4549", "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": "d09ce056", + "id": "0c36be11", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "fa802167", + "id": "5b74e103", "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": "b39eff27", + "id": "3eacd0ac", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "80d1a395", + "id": "f65ea57e", "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": "c1e98c8f", + "id": "c68c59cd", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "576687ed", + "id": "9be17dc5", "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": "c47afd91", + "id": "c8b4b5dc", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "8f48f7e1", + "id": "d71b5f3e", "metadata": {}, "source": [ "#### 17. What is the result of the following expression? (★☆☆)\n", @@ -318,14 +318,14 @@ { "cell_type": "code", "execution_count": null, - "id": "90b87a10", + "id": "581b79ab", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "60aae59a", + "id": "85782373", "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": "04487663", + "id": "c34181be", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "b9876d56", + "id": "d21591c9", "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": "1adaacb1", + "id": "071eb54b", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "d1fe6fa2", + "id": "88dafde8", "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": "264230b9", + "id": "526fc8ec", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "10368e34", + "id": "cd0fb6fe", "metadata": {}, "source": [ "#### 21. Create a checkerboard 8x8 matrix using the tile function (★☆☆)" @@ -382,14 +382,14 @@ { "cell_type": "code", "execution_count": null, - "id": "d40e42ba", + "id": "da65608e", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "88f1cee7", + "id": "625c8e37", "metadata": {}, "source": [ "#### 22. Normalize a 5x5 random matrix (★☆☆)" @@ -398,14 +398,14 @@ { "cell_type": "code", "execution_count": null, - "id": "2204ded1", + "id": "0ab79180", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "420d6732", + "id": "9eda3d89", "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": "54344d5e", + "id": "5fe4cffb", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "8188397c", + "id": "4f75b3fa", "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": "e7819113", + "id": "bb6c04ca", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "96a2427a", + "id": "19aa6ab3", "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": "278a50ca", + "id": "3a3f9f26", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "5d62d4a6", + "id": "fec154e9", "metadata": {}, "source": [ "#### 26. What is the output of the following script? (★☆☆)\n", @@ -469,14 +469,14 @@ { "cell_type": "code", "execution_count": null, - "id": "3e4c5106", + "id": "2b80d6ba", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "698263af", + "id": "0126ebf5", "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": "d3b4dce4", + "id": "dc3f9012", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "34114266", + "id": "ceb0740c", "metadata": {}, "source": [ "#### 28. What are the result of the following expressions? (★☆☆)\n", @@ -514,14 +514,14 @@ { "cell_type": "code", "execution_count": null, - "id": "c1ee584d", + "id": "b7aa8bc2", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "6ae31252", + "id": "aada0794", "metadata": {}, "source": [ "#### 29. How to round away from zero a float array ? (★☆☆)" @@ -530,14 +530,14 @@ { "cell_type": "code", "execution_count": null, - "id": "bb2ce98d", + "id": "1e49a18e", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "cf7847ab", + "id": "c7083062", "metadata": {}, "source": [ "#### 30. How to find common values between two arrays? (★☆☆)" @@ -546,14 +546,14 @@ { "cell_type": "code", "execution_count": null, - "id": "9106d2ef", + "id": "d71dce19", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "ba807b80", + "id": "d225c1a3", "metadata": {}, "source": [ "#### 31. How to ignore all numpy warnings (not recommended)? (★☆☆)" @@ -562,14 +562,14 @@ { "cell_type": "code", "execution_count": null, - "id": "572f8752", + "id": "3c70f3da", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "a25d16c8", + "id": "98bc8375", "metadata": {}, "source": [ "#### 32. Is the following expressions true? (★☆☆)\n", @@ -581,14 +581,14 @@ { "cell_type": "code", "execution_count": null, - "id": "91e261fc", + "id": "8a401d5c", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "74a83f41", + "id": "678c8b6a", "metadata": {}, "source": [ "#### 33. How to get the dates of yesterday, today and tomorrow? (★☆☆)" @@ -597,14 +597,14 @@ { "cell_type": "code", "execution_count": null, - "id": "e0591dc3", + "id": "9e68b25f", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "4de06358", + "id": "4f2a1924", "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": "4e52bdbd", + "id": "b15490fd", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "a6573321", + "id": "e4a5dc99", "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": "5079b2bc", + "id": "e75c410d", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "f543d80f", + "id": "c237037b", "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": "d7fe6792", + "id": "1b2f84b2", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "58ba275f", + "id": "95314ff8", "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": "74164472", + "id": "a7c1c098", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "8872747f", + "id": "d71ef8a5", "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": "063882ec", + "id": "6c850b78", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "a5ee79a3", + "id": "0649a15f", "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": "e156e66f", + "id": "389d7513", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "d04b2bc7", + "id": "86d3d399", "metadata": {}, "source": [ "#### 40. Create a random vector of size 10 and sort it (★★☆)" @@ -709,14 +709,14 @@ { "cell_type": "code", "execution_count": null, - "id": "cff22bfd", + "id": "da5cf212", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "5586ac55", + "id": "f01c199f", "metadata": {}, "source": [ "#### 41. How to sum a small array faster than np.sum? (★★☆)" @@ -725,14 +725,14 @@ { "cell_type": "code", "execution_count": null, - "id": "73421511", + "id": "26c69575", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "6a532f49", + "id": "d052facf", "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": "4d5ba6a7", + "id": "bf0dd32d", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "d761dfc8", + "id": "771b25c0", "metadata": {}, "source": [ "#### 43. Make an array immutable (read-only) (★★☆)" @@ -757,14 +757,14 @@ { "cell_type": "code", "execution_count": null, - "id": "1530351d", + "id": "b97f513f", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "d94043cc", + "id": "a24ba731", "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": "85a157ac", + "id": "400bb13c", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "25ef290c", + "id": "2b817426", "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": "13af9d9b", + "id": "8d29a67f", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "b3d2f592", + "id": "572f43b4", "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": "df412f13", + "id": "8a6b44bd", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "f0ccd315", + "id": "4292855a", "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": "89332c98", + "id": "8802deb6", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "48834278", + "id": "aa12e597", "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": "276bb5c7", + "id": "7c8da439", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "9e31edaf", + "id": "1e7415bb", "metadata": {}, "source": [ "#### 49. How to print all the values of an array? (★★☆)" @@ -853,14 +853,14 @@ { "cell_type": "code", "execution_count": null, - "id": "9fb2c960", + "id": "a9d29043", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "0ce7b2c5", + "id": "795b2cf9", "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": "63168b52", + "id": "9ced3800", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "d8814ddf", + "id": "5d3f186b", "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": "7dc9b51c", + "id": "5ba34444", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "a1b639cc", + "id": "8adc0c43", "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": "4d5bd1b3", + "id": "158c004c", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "f4241884", + "id": "b570651a", "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": "4cc9a908", + "id": "ed4ee8d2", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "e371981c", + "id": "a18d8171", "metadata": {}, "source": [ "#### 54. How to read the following file? (★★☆)\n", @@ -938,14 +938,14 @@ { "cell_type": "code", "execution_count": null, - "id": "86972b97", + "id": "0de1128a", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "7bd26c11", + "id": "77086362", "metadata": {}, "source": [ "#### 55. What is the equivalent of enumerate for numpy arrays? (★★☆)" @@ -954,14 +954,14 @@ { "cell_type": "code", "execution_count": null, - "id": "3c2a9898", + "id": "2e5a8cd6", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "6188312b", + "id": "9174e885", "metadata": {}, "source": [ "#### 56. Generate a generic 2D Gaussian-like array (★★☆)" @@ -970,14 +970,14 @@ { "cell_type": "code", "execution_count": null, - "id": "12843095", + "id": "fc6351e1", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "9e8e5ff6", + "id": "5d1a350b", "metadata": {}, "source": [ "#### 57. How to randomly place p elements in a 2D array? (★★☆)" @@ -986,14 +986,14 @@ { "cell_type": "code", "execution_count": null, - "id": "d6d38c51", + "id": "02d18a57", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "44b82ae3", + "id": "3f943692", "metadata": {}, "source": [ "#### 58. Subtract the mean of each row of a matrix (★★☆)" @@ -1002,14 +1002,14 @@ { "cell_type": "code", "execution_count": null, - "id": "69a0c96c", + "id": "299cce68", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "e23e5478", + "id": "fbdb38f3", "metadata": {}, "source": [ "#### 59. How to sort an array by the nth column? (★★☆)" @@ -1018,14 +1018,14 @@ { "cell_type": "code", "execution_count": null, - "id": "3e6487e7", + "id": "456ea462", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "23a29dee", + "id": "6ac32f39", "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": "91740fdc", + "id": "ecc0e797", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "784e8175", + "id": "3230d718", "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": "ee4b7989", + "id": "4e701a29", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "cdb6399a", + "id": "f25f621a", "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": "69155208", + "id": "d86aca94", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "15a12d09", + "id": "27dc82b1", "metadata": {}, "source": [ "#### 63. Create an array class that has a name attribute (★★☆)" @@ -1082,14 +1082,14 @@ { "cell_type": "code", "execution_count": null, - "id": "1aed42b2", + "id": "0be1ca98", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "7c1f2c44", + "id": "7dc40e9d", "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": "699018f0", + "id": "dce188cf", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "e05e4158", + "id": "9e3e479f", "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": "4f0df204", + "id": "e12f8de9", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "2ee6e9fb", + "id": "2acecb21", "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": "1f828aeb", + "id": "ea8b64ac", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "685b42c8", + "id": "b22d66df", "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": "19f00ff1", + "id": "f31077a5", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "2cd40204", + "id": "c994f297", "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": "e2bb323f", + "id": "675d5e69", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "f1033a57", + "id": "f4191c54", "metadata": {}, "source": [ "#### 69. How to get the diagonal of a dot product? (★★★)" @@ -1178,14 +1178,14 @@ { "cell_type": "code", "execution_count": null, - "id": "03c551d4", + "id": "2b9c6c8d", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "96d038b8", + "id": "095cad09", "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": "d729100f", + "id": "dfd3073b", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "04a70c47", + "id": "402d99df", "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": "66930de6", + "id": "d6283916", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "2629ed58", + "id": "3ac2bca6", "metadata": {}, "source": [ "#### 72. How to swap two rows of an array? (★★★)" @@ -1226,14 +1226,14 @@ { "cell_type": "code", "execution_count": null, - "id": "1f079625", + "id": "f48b17e8", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "5c3082ae", + "id": "b4a03817", "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": "7df23ad4", + "id": "30904f2e", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "bd0af730", + "id": "18d8341c", "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": "09753516", + "id": "1357ff13", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "9fe326b0", + "id": "0bc12ffe", "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": "48bfe845", + "id": "09627c59", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "686ede4c", + "id": "de8d3343", "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": "e3c9cbdf", + "id": "1c8cf783", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "c2f45eda", + "id": "ee0952b7", "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": "7a2da55a", + "id": "761c8a08", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "4bd0c2a1", + "id": "1a67e5d2", "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": "bb163fe1", + "id": "a457bcff", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "326db9c8", + "id": "76628eff", "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": "b8e4042b", + "id": "26f32598", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "f2d94a4a", + "id": "ed33f16f", "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": "dc8f8cca", + "id": "fedc2807", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "c81f6f9b", + "id": "77f36de3", "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": "7c9737aa", + "id": "1649bd8f", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "35becc31", + "id": "730d107b", "metadata": {}, "source": [ "#### 82. Compute a matrix rank (★★★)" @@ -1386,14 +1386,14 @@ { "cell_type": "code", "execution_count": null, - "id": "83902568", + "id": "83d96d46", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "b866c7bc", + "id": "e1eb53e9", "metadata": {}, "source": [ "#### 83. How to find the most frequent value in an array?" @@ -1402,14 +1402,14 @@ { "cell_type": "code", "execution_count": null, - "id": "f3ab9659", + "id": "f1b6a86b", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "5d8ae7f0", + "id": "afc2a8c3", "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": "a4bab033", + "id": "20f74d99", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "cae4c076", + "id": "de5c4b4e", "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": "a70fa139", + "id": "26fb4bc2", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "3800c0ce", + "id": "2b051e56", "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": "0fc258a1", + "id": "a531cfc1", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "34af0c30", + "id": "2aececf7", "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": "eb089155", + "id": "59c3d79c", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "dc504d48", + "id": "7e7d3389", "metadata": {}, "source": [ "#### 88. How to implement the Game of Life using numpy arrays? (★★★)" @@ -1482,14 +1482,14 @@ { "cell_type": "code", "execution_count": null, - "id": "07d7529a", + "id": "068158c8", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "1d656a40", + "id": "db62deda", "metadata": {}, "source": [ "#### 89. How to get the n largest values of an array (★★★)" @@ -1498,14 +1498,14 @@ { "cell_type": "code", "execution_count": null, - "id": "9e6c0153", + "id": "b21ede2e", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "d85fb529", + "id": "3a708816", "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": "4f2de833", + "id": "6b31b0cd", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "30e08504", + "id": "a8f3bbe0", "metadata": {}, "source": [ "#### 91. How to create a record array from a regular array? (★★★)" @@ -1530,14 +1530,14 @@ { "cell_type": "code", "execution_count": null, - "id": "73cf4fd7", + "id": "2fd5dd75", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "cac23740", + "id": "217349c5", "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": "bb0b1f62", + "id": "05fe0425", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "d883be69", + "id": "efff4c6c", "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": "560070dc", + "id": "5d1c45d3", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "7f8e79e3", + "id": "3f5b046b", "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": "816e321e", + "id": "ef98dcd8", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "8c84b1bb", + "id": "34446b6b", "metadata": {}, "source": [ "#### 95. Convert a vector of ints into a matrix binary representation (★★★)" @@ -1594,14 +1594,14 @@ { "cell_type": "code", "execution_count": null, - "id": "831c8a0d", + "id": "0dc03a0e", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "3338c3c7", + "id": "cb091f58", "metadata": {}, "source": [ "#### 96. Given a two dimensional array, how to extract unique rows? (★★★)" @@ -1610,14 +1610,14 @@ { "cell_type": "code", "execution_count": null, - "id": "8f46b166", + "id": "068310df", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "952f1a97", + "id": "bc6d7f7f", "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": "5b330952", + "id": "db1d298b", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "7c250535", + "id": "fec0aced", "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": "1028c25b", + "id": "cd8582d6", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "22ac5051", + "id": "c085a040", "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": "be6e23f2", + "id": "971b6bf3", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "d376c97c", + "id": "f8dfde8c", "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": "8c9b4471", + "id": "8bf8c280", "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..1e54ecb 100644 --- a/100_Numpy_exercises_with_hints_with_solutions.md +++ b/100_Numpy_exercises_with_hints_with_solutions.md @@ -494,11 +494,11 @@ print(Z[index]) `hint: dtype` ```python -Z = np.zeros(10, [ ('position', [ ('x', float, 1), - ('y', float, 1)]), - ('color', [ ('r', float, 1), - ('g', float, 1), - ('b', float, 1)])]) +Z = np.zeros(10, [ ('position', [ ('x', float), + ('y', float)]), + ('color', [ ('r', float), + ('g', float), + ('b', float)])]) print(Z) ``` #### 52. Consider a random vector with shape (100,2) representing coordinates, find point by point distances (★★☆) diff --git a/100_Numpy_exercises_with_solutions.md b/100_Numpy_exercises_with_solutions.md index 80bd9d8..fea35e2 100644 --- a/100_Numpy_exercises_with_solutions.md +++ b/100_Numpy_exercises_with_solutions.md @@ -494,11 +494,11 @@ print(Z[index]) ```python -Z = np.zeros(10, [ ('position', [ ('x', float, 1), - ('y', float, 1)]), - ('color', [ ('r', float, 1), - ('g', float, 1), - ('b', float, 1)])]) +Z = np.zeros(10, [ ('position', [ ('x', float), + ('y', float)]), + ('color', [ ('r', float), + ('g', float), + ('b', float)])]) print(Z) ``` #### 52. Consider a random vector with shape (100,2) representing coordinates, find point by point distances (★★☆) diff --git a/100_Numpy_random.ipynb b/100_Numpy_random.ipynb index 3dbb0d5..0bf202d 100644 --- a/100_Numpy_random.ipynb +++ b/100_Numpy_random.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "markdown", - "id": "4a87d009", + "id": "261c7cb9", "metadata": {}, "source": [ "# 100 numpy exercises\n", @@ -18,7 +18,7 @@ }, { "cell_type": "markdown", - "id": "16b631f6", + "id": "b0ffec6a", "metadata": {}, "source": [ "File automatically generated. See the documentation to update questions/answers/hints programmatically." @@ -26,7 +26,7 @@ }, { "cell_type": "markdown", - "id": "8c00c264", + "id": "30434163", "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": "c5fa0b45", + "id": "8f632d6c", "metadata": {}, "outputs": [], "source": [ @@ -46,7 +46,7 @@ { "cell_type": "code", "execution_count": null, - "id": "6c4f9753", + "id": "6cfc4341", "metadata": {}, "outputs": [], "source": [