diff --git a/100_Numpy_exercises.ipynb b/100_Numpy_exercises.ipynb index cb8ecc1..a40acc7 100644 --- a/100_Numpy_exercises.ipynb +++ b/100_Numpy_exercises.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "markdown", - "id": "5a28b949", + "id": "efad8fc9", "metadata": {}, "source": [ "# 100 numpy exercises\n", @@ -18,7 +18,7 @@ }, { "cell_type": "markdown", - "id": "3f7a87ec", + "id": "c5e74195", "metadata": {}, "source": [ "File automatically generated. See the documentation to update questions/answers/hints programmatically." @@ -26,7 +26,7 @@ }, { "cell_type": "markdown", - "id": "854ecf1b", + "id": "7cded7d4", "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": "c53ea877", + "id": "6ed45646", "metadata": {}, "outputs": [], "source": [ @@ -45,7 +45,7 @@ }, { "cell_type": "markdown", - "id": "c00df986", + "id": "96879753", "metadata": {}, "source": [ "#### 1. Import the numpy package under the name `np` (★☆☆)" @@ -54,14 +54,14 @@ { "cell_type": "code", "execution_count": null, - "id": "35bde95c", + "id": "4e7f6961", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "b555a89f", + "id": "bccd4adb", "metadata": {}, "source": [ "#### 2. Print the numpy version and the configuration (★☆☆)" @@ -70,14 +70,14 @@ { "cell_type": "code", "execution_count": null, - "id": "2c9d363d", + "id": "4b3ea76d", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "e2713407", + "id": "f111cfb0", "metadata": {}, "source": [ "#### 3. Create a null vector of size 10 (★☆☆)" @@ -86,14 +86,14 @@ { "cell_type": "code", "execution_count": null, - "id": "6df2f6db", + "id": "5530af37", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "5a09554f", + "id": "e68ba5eb", "metadata": {}, "source": [ "#### 4. How to find the memory size of any array (★☆☆)" @@ -102,14 +102,14 @@ { "cell_type": "code", "execution_count": null, - "id": "c55a2a00", + "id": "cb5dd6ee", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "4720d438", + "id": "6b05b5a0", "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": "d9e2277e", + "id": "b4a926db", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "b812d322", + "id": "ef2afb66", "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": "9f95cedf", + "id": "c369724e", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "a348f2a8", + "id": "d1520136", "metadata": {}, "source": [ "#### 7. Create a vector with values ranging from 10 to 49 (★☆☆)" @@ -150,14 +150,14 @@ { "cell_type": "code", "execution_count": null, - "id": "5a68fd5b", + "id": "7991d24d", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "2f4ca4c0", + "id": "f1d4d44e", "metadata": {}, "source": [ "#### 8. Reverse a vector (first element becomes last) (★☆☆)" @@ -166,14 +166,14 @@ { "cell_type": "code", "execution_count": null, - "id": "1892e775", + "id": "d34135c8", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "31381832", + "id": "b236e16e", "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": "0154f251", + "id": "e648a47e", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "8c353e84", + "id": "b438d174", "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": "da3495c3", + "id": "96ee3ada", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "a26ac749", + "id": "1dc40ee1", "metadata": {}, "source": [ "#### 11. Create a 3x3 identity matrix (★☆☆)" @@ -214,14 +214,14 @@ { "cell_type": "code", "execution_count": null, - "id": "3e33feda", + "id": "09083520", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "3a293379", + "id": "3d131679", "metadata": {}, "source": [ "#### 12. Create a 3x3x3 array with random values (★☆☆)" @@ -230,14 +230,14 @@ { "cell_type": "code", "execution_count": null, - "id": "2da08152", + "id": "73c7a765", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "e3faa924", + "id": "9e8a79a2", "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": "e9fb3793", + "id": "145c1955", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "317cb8d1", + "id": "507c9f24", "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": "4ea995d6", + "id": "57c13962", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "b8dee0f2", + "id": "fac3a281", "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": "7054464b", + "id": "30d6b8b9", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "1476f45f", + "id": "044bc36a", "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": "4f0212af", + "id": "361f0b65", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "52844dc7", + "id": "95efa980", "metadata": {}, "source": [ "#### 17. What is the result of the following expression? (★☆☆)\n", @@ -318,14 +318,14 @@ { "cell_type": "code", "execution_count": null, - "id": "cfdb7352", + "id": "ac8bf3f0", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "9801c961", + "id": "14f7de41", "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": "4e97651a", + "id": "326b60af", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "3dc05e9f", + "id": "97c38c34", "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": "0b100b85", + "id": "26c4959f", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "d8e941b6", + "id": "2ca2a1dd", "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": "b3bc1fcb", + "id": "f16f87e3", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "bf3b8ab3", + "id": "11b3f803", "metadata": {}, "source": [ "#### 21. Create a checkerboard 8x8 matrix using the tile function (★☆☆)" @@ -382,14 +382,14 @@ { "cell_type": "code", "execution_count": null, - "id": "eb15c681", + "id": "3214a545", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "71af9d19", + "id": "276ee0a3", "metadata": {}, "source": [ "#### 22. Normalize a 5x5 random matrix (★☆☆)" @@ -398,14 +398,14 @@ { "cell_type": "code", "execution_count": null, - "id": "df20b788", + "id": "ad98885c", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "01cd9893", + "id": "1173798a", "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": "4acddc2f", + "id": "5bfc6872", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "0ef5f38b", + "id": "2d1ebe6d", "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": "3d36d4a0", + "id": "4e93ed48", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "a46db425", + "id": "575aa67a", "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": "86e3d6a8", + "id": "6ac349e9", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "89fa6007", + "id": "bf1698f3", "metadata": {}, "source": [ "#### 26. What is the output of the following script? (★☆☆)\n", @@ -469,14 +469,14 @@ { "cell_type": "code", "execution_count": null, - "id": "59f2c6dc", + "id": "b813e950", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "38de4589", + "id": "4485dc46", "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": "a9dc9520", + "id": "bf090be3", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "77c8b8c7", + "id": "5dc9e518", "metadata": {}, "source": [ "#### 28. What are the result of the following expressions? (★☆☆)\n", @@ -514,14 +514,14 @@ { "cell_type": "code", "execution_count": null, - "id": "3b66fe44", + "id": "0fb055be", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "1f3f0892", + "id": "1c808a3d", "metadata": {}, "source": [ "#### 29. How to round away from zero a float array ? (★☆☆)" @@ -530,14 +530,14 @@ { "cell_type": "code", "execution_count": null, - "id": "8fce1bd7", + "id": "44fc39b0", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "53f47438", + "id": "b7b0a425", "metadata": {}, "source": [ "#### 30. How to find common values between two arrays? (★☆☆)" @@ -546,14 +546,14 @@ { "cell_type": "code", "execution_count": null, - "id": "981db6ec", + "id": "15719227", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "51cf9109", + "id": "a736f4cd", "metadata": {}, "source": [ "#### 31. How to ignore all numpy warnings (not recommended)? (★☆☆)" @@ -562,14 +562,14 @@ { "cell_type": "code", "execution_count": null, - "id": "09ba7521", + "id": "736488dd", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "948a691b", + "id": "a99366cf", "metadata": {}, "source": [ "#### 32. Is the following expressions true? (★☆☆)\n", @@ -581,14 +581,14 @@ { "cell_type": "code", "execution_count": null, - "id": "6d8f745b", + "id": "8dcc60f6", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "d1a8992e", + "id": "5720a912", "metadata": {}, "source": [ "#### 33. How to get the dates of yesterday, today and tomorrow? (★☆☆)" @@ -597,14 +597,14 @@ { "cell_type": "code", "execution_count": null, - "id": "31a1ade2", + "id": "6130a533", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "01378794", + "id": "2bb63fc8", "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": "928c55c6", + "id": "23d6ffc5", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "e3f8fe94", + "id": "9fad1b29", "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": "14e478d5", + "id": "2e3d0f40", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "5c4d58c9", + "id": "bea5ed2f", "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": "586beded", + "id": "13d4c3d1", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "62628653", + "id": "c7c23f39", "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": "e57763b3", + "id": "7dad57a3", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "d3d46d64", + "id": "ed29c1d6", "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": "24590d8c", + "id": "9c1c8539", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "5e1e51e4", + "id": "1d43313d", "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": "def73884", + "id": "1ddf85ea", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "9e19964c", + "id": "b310b5ca", "metadata": {}, "source": [ "#### 40. Create a random vector of size 10 and sort it (★★☆)" @@ -709,14 +709,14 @@ { "cell_type": "code", "execution_count": null, - "id": "ec302eb1", + "id": "9990abe6", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "13a034e2", + "id": "b790c8e7", "metadata": {}, "source": [ "#### 41. How to sum a small array faster than np.sum? (★★☆)" @@ -725,14 +725,14 @@ { "cell_type": "code", "execution_count": null, - "id": "b09a3551", + "id": "a1027f4c", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "32d10473", + "id": "b6e25d4e", "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": "5ebb1103", + "id": "5a1c8886", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "a50474b0", + "id": "d1ea53c0", "metadata": {}, "source": [ "#### 43. Make an array immutable (read-only) (★★☆)" @@ -757,14 +757,14 @@ { "cell_type": "code", "execution_count": null, - "id": "db6fe176", + "id": "f6b684bb", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "b7a6093e", + "id": "2ae768e5", "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": "4479c48d", + "id": "553e9aee", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "934f889c", + "id": "69318337", "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": "4b576814", + "id": "604f6139", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "d4f6e2e1", + "id": "ba594cdf", "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": "f90f969b", + "id": "f459d3a3", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "bc9c88d3", + "id": "32ba7806", "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": "e90ad595", + "id": "7689f771", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "a840a52f", + "id": "bf02c92c", "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": "d2eb3b4a", + "id": "cd62eb79", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "0a0819d5", + "id": "a0de5c11", "metadata": {}, "source": [ "#### 49. How to print all the values of an array? (★★☆)" @@ -853,14 +853,14 @@ { "cell_type": "code", "execution_count": null, - "id": "d76dbf55", + "id": "9b2ca45e", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "50c62214", + "id": "cef38159", "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": "24b45bb5", + "id": "64f16ae9", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "4c5a5116", + "id": "8d8de5a2", "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": "275806b2", + "id": "91df6e12", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "1d3399ca", + "id": "ae4a3038", "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": "67eae5bf", + "id": "2c9c8a81", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "003ff920", + "id": "340c56c8", "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": "76245e6d", + "id": "fbeff718", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "0c99eac4", + "id": "524d1c6b", "metadata": {}, "source": [ "#### 54. How to read the following file? (★★☆)\n", @@ -938,14 +938,14 @@ { "cell_type": "code", "execution_count": null, - "id": "55c6bf8c", + "id": "4f7e4bfd", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "8cd30360", + "id": "6924b96d", "metadata": {}, "source": [ "#### 55. What is the equivalent of enumerate for numpy arrays? (★★☆)" @@ -954,14 +954,14 @@ { "cell_type": "code", "execution_count": null, - "id": "608315ba", + "id": "2048f576", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "4823a294", + "id": "871a7d29", "metadata": {}, "source": [ "#### 56. Generate a generic 2D Gaussian-like array (★★☆)" @@ -970,14 +970,14 @@ { "cell_type": "code", "execution_count": null, - "id": "9b3b682d", + "id": "6b0897a4", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "2dd8891d", + "id": "95246a56", "metadata": {}, "source": [ "#### 57. How to randomly place p elements in a 2D array? (★★☆)" @@ -986,14 +986,14 @@ { "cell_type": "code", "execution_count": null, - "id": "a55c52dc", + "id": "fd6ce38b", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "270b6834", + "id": "712e08ad", "metadata": {}, "source": [ "#### 58. Subtract the mean of each row of a matrix (★★☆)" @@ -1002,14 +1002,14 @@ { "cell_type": "code", "execution_count": null, - "id": "814080c9", + "id": "69173061", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "52c92916", + "id": "1d6ce114", "metadata": {}, "source": [ "#### 59. How to sort an array by the nth column? (★★☆)" @@ -1018,14 +1018,14 @@ { "cell_type": "code", "execution_count": null, - "id": "c6ebac6d", + "id": "8b978737", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "05cef60b", + "id": "e67babfb", "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": "7979b467", + "id": "b7367c9a", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "4891fdf5", + "id": "8fe7d958", "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": "b38950fd", + "id": "4fd72a02", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "b34bfbc7", + "id": "d8f3c279", "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": "aa7eb006", + "id": "a3cb4d0a", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "77a27b83", + "id": "8ea81762", "metadata": {}, "source": [ "#### 63. Create an array class that has a name attribute (★★☆)" @@ -1082,14 +1082,14 @@ { "cell_type": "code", "execution_count": null, - "id": "a2a83936", + "id": "79f5f341", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "f7f5d229", + "id": "ce6a4e47", "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": "9b8f8f48", + "id": "f11d48e7", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "b067fca8", + "id": "15d0f1c9", "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": "f983f8ba", + "id": "d4652d5d", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "765e2b8e", + "id": "ad9871d7", "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": "7572d90d", + "id": "8581be23", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "02743317", + "id": "7e182f59", "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": "980b5c45", + "id": "5f6fe735", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "fe8b8107", + "id": "8a311e05", "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": "9c17c8c9", + "id": "a2d618a0", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "7efa1173", + "id": "df2a4420", "metadata": {}, "source": [ "#### 69. How to get the diagonal of a dot product? (★★★)" @@ -1178,14 +1178,14 @@ { "cell_type": "code", "execution_count": null, - "id": "59fc6a51", + "id": "9d82a2bf", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "b72adc30", + "id": "e26f6fbc", "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": "94b582b4", + "id": "753f0402", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "fafa05b8", + "id": "2b56c24c", "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": "0dbb5bd4", + "id": "ae5ea33b", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "35a2bae5", + "id": "fd6e754d", "metadata": {}, "source": [ "#### 72. How to swap two rows of an array? (★★★)" @@ -1226,14 +1226,14 @@ { "cell_type": "code", "execution_count": null, - "id": "d63b30c3", + "id": "6b88370c", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "b760fc57", + "id": "39809f9e", "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": "01738774", + "id": "6c2c5391", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "2d28cdaa", + "id": "f0e0aaee", "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": "8423859b", + "id": "efd89ef3", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "5f4cafbd", + "id": "b22e5117", "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": "62d6d514", + "id": "81127417", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "575d8bb4", + "id": "5251b5fe", "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": "2a41d341", + "id": "2f4a0d30", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "80dc0b2a", + "id": "5a57285b", "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": "6fc68014", + "id": "6f9edf16", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "7288634d", + "id": "de63f29d", "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": "4602caa6", + "id": "5ddc4b17", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "e1880e79", + "id": "60b56ef4", "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": "a14e151b", + "id": "edcc16dc", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "b8a2a287", + "id": "6bab5778", "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": "86af1186", + "id": "ded87e89", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "caa64150", + "id": "cab456d6", "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": "4e7cc696", + "id": "a917491b", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "9444c83b", + "id": "cc3a2d92", "metadata": {}, "source": [ "#### 82. Compute a matrix rank (★★★)" @@ -1386,14 +1386,14 @@ { "cell_type": "code", "execution_count": null, - "id": "9e5c1d64", + "id": "ac8f7874", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "6eef9f33", + "id": "52fb5b21", "metadata": {}, "source": [ "#### 83. How to find the most frequent value in an array?" @@ -1402,14 +1402,14 @@ { "cell_type": "code", "execution_count": null, - "id": "9fb60e52", + "id": "6873f923", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "65f370f9", + "id": "7916c3ee", "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": "65ade1a6", + "id": "c32062e7", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "845ad7f8", + "id": "76feb13d", "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": "30b1ba71", + "id": "0de21a14", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "adc48f02", + "id": "b4fec8ef", "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": "ec798e90", + "id": "da035355", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "9efb34b2", + "id": "cf2aa631", "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": "ea79f40b", + "id": "091cc87e", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "a9368578", + "id": "70492780", "metadata": {}, "source": [ "#### 88. How to implement the Game of Life using numpy arrays? (★★★)" @@ -1482,14 +1482,14 @@ { "cell_type": "code", "execution_count": null, - "id": "315e9f11", + "id": "1c6bed05", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "8c598af3", + "id": "942fefca", "metadata": {}, "source": [ "#### 89. How to get the n largest values of an array (★★★)" @@ -1498,14 +1498,14 @@ { "cell_type": "code", "execution_count": null, - "id": "cba8d433", + "id": "1f9ed7f3", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "036a3bb0", + "id": "3645d7f2", "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": "d712972d", + "id": "bb08a04a", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "36487aab", + "id": "d78539cc", "metadata": {}, "source": [ "#### 91. How to create a record array from a regular array? (★★★)" @@ -1530,14 +1530,14 @@ { "cell_type": "code", "execution_count": null, - "id": "9779dbac", + "id": "1bcf00e8", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "9f178562", + "id": "2840401f", "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": "b08000b8", + "id": "2dff785d", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "86acaf60", + "id": "52aec3df", "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": "793b26d4", + "id": "6a49c12a", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "c71b9df2", + "id": "5bb45c8f", "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": "494069f0", + "id": "608bff2d", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "1a8ae7c1", + "id": "5b7bc7f9", "metadata": {}, "source": [ "#### 95. Convert a vector of ints into a matrix binary representation (★★★)" @@ -1594,14 +1594,14 @@ { "cell_type": "code", "execution_count": null, - "id": "d5e967b8", + "id": "6bd15a76", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "edef1198", + "id": "14e71dfc", "metadata": {}, "source": [ "#### 96. Given a two dimensional array, how to extract unique rows? (★★★)" @@ -1610,14 +1610,14 @@ { "cell_type": "code", "execution_count": null, - "id": "fa14799b", + "id": "ec8f1c8a", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "575598a1", + "id": "5bb57087", "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": "9da72f6e", + "id": "83a1722b", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "88e2796a", + "id": "0514b3d8", "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": "c17314f8", + "id": "05e45957", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "8b25b7bd", + "id": "ed816266", "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": "f0dc48cd", + "id": "023c961d", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "b7068c76", + "id": "a145d662", "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": "3c234201", + "id": "ca32b19b", "metadata": {}, "outputs": [], "source": [] diff --git a/100_Numpy_exercises_with_hints_with_solutions.md b/100_Numpy_exercises_with_hints_with_solutions.md index 093f0f2..3c6d409 100644 --- a/100_Numpy_exercises_with_hints_with_solutions.md +++ b/100_Numpy_exercises_with_hints_with_solutions.md @@ -548,7 +548,7 @@ s = StringIO('''1, 2, 3, 4, 5 , , 9,10,11 ''') -Z = np.genfromtxt(s, delimiter=",", dtype=np.int) +Z = np.genfromtxt(s, delimiter=",", dtype = int, filling_values = 0) print(Z) ``` #### 55. What is the equivalent of enumerate for numpy arrays? (★★☆) diff --git a/100_Numpy_exercises_with_solutions.md b/100_Numpy_exercises_with_solutions.md index 2b8f668..3ab3c91 100644 --- a/100_Numpy_exercises_with_solutions.md +++ b/100_Numpy_exercises_with_solutions.md @@ -548,7 +548,7 @@ s = StringIO('''1, 2, 3, 4, 5 , , 9,10,11 ''') -Z = np.genfromtxt(s, delimiter=",", dtype=np.int) +Z = np.genfromtxt(s, delimiter=",", dtype = int, filling_values = 0) print(Z) ``` #### 55. What is the equivalent of enumerate for numpy arrays? (★★☆) diff --git a/100_Numpy_random.ipynb b/100_Numpy_random.ipynb index 9880b63..1c011fd 100644 --- a/100_Numpy_random.ipynb +++ b/100_Numpy_random.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "markdown", - "id": "af9e3994", + "id": "21aee2db", "metadata": {}, "source": [ "# 100 numpy exercises\n", @@ -18,7 +18,7 @@ }, { "cell_type": "markdown", - "id": "76e99d49", + "id": "9ba95040", "metadata": {}, "source": [ "File automatically generated. See the documentation to update questions/answers/hints programmatically." @@ -26,7 +26,7 @@ }, { "cell_type": "markdown", - "id": "8a54b21a", + "id": "d2c26b77", "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": "670bb646", + "id": "2fbe49f5", "metadata": {}, "outputs": [], "source": [ @@ -46,7 +46,7 @@ { "cell_type": "code", "execution_count": null, - "id": "4ff49967", + "id": "8a120f51", "metadata": {}, "outputs": [], "source": [