From 8ca28aaefca69a32ad3ccbbd01c7c9a785c2d5c1 Mon Sep 17 00:00:00 2001 From: rougier <327203+rougier@users.noreply.github.com> Date: Wed, 26 Aug 2026 10:29:27 +0000 Subject: [PATCH] solutions update from 92b0e36 --- 100_Numpy_exercises.ipynb | 410 +++++++++--------- 100_Numpy_exercises.md | 2 +- 100_Numpy_exercises_with_hints.md | 2 +- ...mpy_exercises_with_hints_with_solutions.md | 2 +- 100_Numpy_exercises_with_solutions.md | 2 +- 100_Numpy_random.ipynb | 10 +- 6 files changed, 214 insertions(+), 214 deletions(-) diff --git a/100_Numpy_exercises.ipynb b/100_Numpy_exercises.ipynb index 3a7edbd..934f661 100644 --- a/100_Numpy_exercises.ipynb +++ b/100_Numpy_exercises.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "markdown", - "id": "44d97bac", + "id": "9f1c4bdc", "metadata": {}, "source": [ "# 100 numpy exercises\n", @@ -18,7 +18,7 @@ }, { "cell_type": "markdown", - "id": "22920094", + "id": "1c38c76d", "metadata": {}, "source": [ "File automatically generated. See the documentation to update questions/answers/hints programmatically." @@ -26,7 +26,7 @@ }, { "cell_type": "markdown", - "id": "bf26bed4", + "id": "d247d301", "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": "1fbf8f40", + "id": "17c05c66", "metadata": {}, "outputs": [], "source": [ @@ -45,7 +45,7 @@ }, { "cell_type": "markdown", - "id": "f82fc1fe", + "id": "9cba10d4", "metadata": {}, "source": [ "#### 1. Import the numpy package under the name `np` (★☆☆)" @@ -54,14 +54,14 @@ { "cell_type": "code", "execution_count": null, - "id": "3ce4a75b", + "id": "39326a32", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "9f52dd8f", + "id": "5fcfcf89", "metadata": {}, "source": [ "#### 2. Print the numpy version and the configuration (★☆☆)" @@ -70,14 +70,14 @@ { "cell_type": "code", "execution_count": null, - "id": "9d650aad", + "id": "d2af6d2b", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "2e9b3d85", + "id": "a0039ef2", "metadata": {}, "source": [ "#### 3. Create a null vector of size 10 (★☆☆)" @@ -86,14 +86,14 @@ { "cell_type": "code", "execution_count": null, - "id": "82efe018", + "id": "0548970a", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "e5fcb60f", + "id": "b5232d74", "metadata": {}, "source": [ "#### 4. How to find the memory size of any array (★☆☆)" @@ -102,14 +102,14 @@ { "cell_type": "code", "execution_count": null, - "id": "11b7d769", + "id": "1faa16a0", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "beca6a94", + "id": "565150b3", "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": "2a71a96e", + "id": "9d4057e1", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "d785b786", + "id": "50ee4f8b", "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": "9a23c28e", + "id": "c03cab65", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "d338b31b", + "id": "f3e3db6f", "metadata": {}, "source": [ "#### 7. Create a vector with values ranging from 10 to 49 (★☆☆)" @@ -150,14 +150,14 @@ { "cell_type": "code", "execution_count": null, - "id": "9d1b76ef", + "id": "05dda42d", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "788ff092", + "id": "07ac0e10", "metadata": {}, "source": [ "#### 8. Reverse a vector (first element becomes last) (★☆☆)" @@ -166,14 +166,14 @@ { "cell_type": "code", "execution_count": null, - "id": "bb3ff218", + "id": "5b2a6edb", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "e956caaa", + "id": "9495e174", "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": "42691982", + "id": "d38595d1", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "3869a3da", + "id": "588c569a", "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": "1688d184", + "id": "404ef4b8", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "504726a6", + "id": "e35c46f5", "metadata": {}, "source": [ "#### 11. Create a 3x3 identity matrix (★☆☆)" @@ -214,14 +214,14 @@ { "cell_type": "code", "execution_count": null, - "id": "ea5d379b", + "id": "6cfe670e", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "1f897d7c", + "id": "f0286fec", "metadata": {}, "source": [ "#### 12. Create a 3x3x3 array with random values (★☆☆)" @@ -230,14 +230,14 @@ { "cell_type": "code", "execution_count": null, - "id": "49ad0a78", + "id": "f8c79d4f", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "3fce4549", + "id": "4dba699d", "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": "0c36be11", + "id": "66776dae", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "5b74e103", + "id": "e02b0a0e", "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": "3eacd0ac", + "id": "d1acb35b", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "f65ea57e", + "id": "516e1a16", "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": "c68c59cd", + "id": "b9e2bfc0", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "9be17dc5", + "id": "24ec162f", "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": "c8b4b5dc", + "id": "f9527fdd", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "d71b5f3e", + "id": "1fe219cb", "metadata": {}, "source": [ "#### 17. What is the result of the following expression? (★☆☆)\n", @@ -318,14 +318,14 @@ { "cell_type": "code", "execution_count": null, - "id": "581b79ab", + "id": "f05457e3", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "85782373", + "id": "ac14a261", "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": "c34181be", + "id": "c56efed4", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "d21591c9", + "id": "fc6a1218", "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": "071eb54b", + "id": "8e61d2af", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "88dafde8", + "id": "2ca95506", "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": "526fc8ec", + "id": "b1f547b3", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "cd0fb6fe", + "id": "66f6c729", "metadata": {}, "source": [ "#### 21. Create a checkerboard 8x8 matrix using the tile function (★☆☆)" @@ -382,14 +382,14 @@ { "cell_type": "code", "execution_count": null, - "id": "da65608e", + "id": "8bd82a39", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "625c8e37", + "id": "e6d4ea6b", "metadata": {}, "source": [ "#### 22. Normalize a 5x5 random matrix (★☆☆)" @@ -398,14 +398,14 @@ { "cell_type": "code", "execution_count": null, - "id": "0ab79180", + "id": "396ff342", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "9eda3d89", + "id": "561fc316", "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": "5fe4cffb", + "id": "077d8347", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "4f75b3fa", + "id": "e97601fd", "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": "bb6c04ca", + "id": "07fb40aa", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "19aa6ab3", + "id": "f6ebca72", "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": "3a3f9f26", + "id": "fc6e07b0", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "fec154e9", + "id": "5c3d5339", "metadata": {}, "source": [ "#### 26. What is the output of the following script? (★☆☆)\n", @@ -469,14 +469,14 @@ { "cell_type": "code", "execution_count": null, - "id": "2b80d6ba", + "id": "48a47b26", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "0126ebf5", + "id": "4a398257", "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": "dc3f9012", + "id": "8ea008f5", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "ceb0740c", + "id": "34c90909", "metadata": {}, "source": [ "#### 28. What are the result of the following expressions? (★☆☆)\n", @@ -514,14 +514,14 @@ { "cell_type": "code", "execution_count": null, - "id": "b7aa8bc2", + "id": "b40c2a19", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "aada0794", + "id": "d6222736", "metadata": {}, "source": [ "#### 29. How to round away from zero a float array ? (★☆☆)" @@ -530,14 +530,14 @@ { "cell_type": "code", "execution_count": null, - "id": "1e49a18e", + "id": "9092d8a0", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "c7083062", + "id": "2f203b98", "metadata": {}, "source": [ "#### 30. How to find common values between two arrays? (★☆☆)" @@ -546,14 +546,14 @@ { "cell_type": "code", "execution_count": null, - "id": "d71dce19", + "id": "08ad43e4", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "d225c1a3", + "id": "18d7f8f9", "metadata": {}, "source": [ "#### 31. How to ignore all numpy warnings (not recommended)? (★☆☆)" @@ -562,14 +562,14 @@ { "cell_type": "code", "execution_count": null, - "id": "3c70f3da", + "id": "0e5cb159", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "98bc8375", + "id": "6a7cb1d5", "metadata": {}, "source": [ "#### 32. Is the following expressions true? (★☆☆)\n", @@ -581,14 +581,14 @@ { "cell_type": "code", "execution_count": null, - "id": "8a401d5c", + "id": "95b08ebf", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "678c8b6a", + "id": "ed3bcb31", "metadata": {}, "source": [ "#### 33. How to get the dates of yesterday, today and tomorrow? (★☆☆)" @@ -597,14 +597,14 @@ { "cell_type": "code", "execution_count": null, - "id": "9e68b25f", + "id": "ef8290f0", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "4f2a1924", + "id": "609e4756", "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": "b15490fd", + "id": "b3daa210", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "e4a5dc99", + "id": "4fc7e445", "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": "e75c410d", + "id": "fd0852fe", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "c237037b", + "id": "f26534b8", "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": "1b2f84b2", + "id": "d3e4038e", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "95314ff8", + "id": "08a242f5", "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": "a7c1c098", + "id": "d9bff9c8", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "d71ef8a5", + "id": "3a2f5f74", "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": "6c850b78", + "id": "ea69400e", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "0649a15f", + "id": "32a6d375", "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": "389d7513", + "id": "abc7b2e0", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "86d3d399", + "id": "45a93227", "metadata": {}, "source": [ "#### 40. Create a random vector of size 10 and sort it (★★☆)" @@ -709,14 +709,14 @@ { "cell_type": "code", "execution_count": null, - "id": "da5cf212", + "id": "a87fcc86", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "f01c199f", + "id": "f1ceb002", "metadata": {}, "source": [ "#### 41. How to sum a small array faster than np.sum? (★★☆)" @@ -725,14 +725,14 @@ { "cell_type": "code", "execution_count": null, - "id": "26c69575", + "id": "0a3d9eb2", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "d052facf", + "id": "cdf989a6", "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": "bf0dd32d", + "id": "9f8383b7", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "771b25c0", + "id": "79fa8d3c", "metadata": {}, "source": [ "#### 43. Make an array immutable (read-only) (★★☆)" @@ -757,14 +757,14 @@ { "cell_type": "code", "execution_count": null, - "id": "b97f513f", + "id": "28261b00", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "a24ba731", + "id": "e36940a3", "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": "400bb13c", + "id": "fd414a62", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "2b817426", + "id": "9b61efe6", "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": "8d29a67f", + "id": "9763a8d6", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "572f43b4", + "id": "3f349cdf", "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": "8a6b44bd", + "id": "077f517d", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "4292855a", + "id": "82185505", "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": "8802deb6", + "id": "5a5ad75f", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "aa12e597", + "id": "db51b5ae", "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": "7c8da439", + "id": "a0985f7a", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "1e7415bb", + "id": "6f450903", "metadata": {}, "source": [ "#### 49. How to print all the values of an array? (★★☆)" @@ -853,14 +853,14 @@ { "cell_type": "code", "execution_count": null, - "id": "a9d29043", + "id": "128241d1", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "795b2cf9", + "id": "db7f2c75", "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": "9ced3800", + "id": "48e3bc1f", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "5d3f186b", + "id": "858b6cc9", "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": "5ba34444", + "id": "87cc8ff2", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "8adc0c43", + "id": "51e6c5ab", "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": "158c004c", + "id": "2292da9a", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "b570651a", + "id": "c7f19a31", "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": "ed4ee8d2", + "id": "b865fbca", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "a18d8171", + "id": "97a0a87e", "metadata": {}, "source": [ "#### 54. How to read the following file? (★★☆)\n", @@ -938,14 +938,14 @@ { "cell_type": "code", "execution_count": null, - "id": "0de1128a", + "id": "fbc23786", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "77086362", + "id": "00d2b897", "metadata": {}, "source": [ "#### 55. What is the equivalent of enumerate for numpy arrays? (★★☆)" @@ -954,14 +954,14 @@ { "cell_type": "code", "execution_count": null, - "id": "2e5a8cd6", + "id": "f19b27ee", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "9174e885", + "id": "c6aa0c57", "metadata": {}, "source": [ "#### 56. Generate a generic 2D Gaussian-like array (★★☆)" @@ -970,14 +970,14 @@ { "cell_type": "code", "execution_count": null, - "id": "fc6351e1", + "id": "49d9b6de", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "5d1a350b", + "id": "f9338371", "metadata": {}, "source": [ "#### 57. How to randomly place p elements in a 2D array? (★★☆)" @@ -986,14 +986,14 @@ { "cell_type": "code", "execution_count": null, - "id": "02d18a57", + "id": "d605d1fe", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "3f943692", + "id": "7a82438d", "metadata": {}, "source": [ "#### 58. Subtract the mean of each row of a matrix (★★☆)" @@ -1002,14 +1002,14 @@ { "cell_type": "code", "execution_count": null, - "id": "299cce68", + "id": "fb02ac6d", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "fbdb38f3", + "id": "0261917b", "metadata": {}, "source": [ "#### 59. How to sort an array by the nth column? (★★☆)" @@ -1018,14 +1018,14 @@ { "cell_type": "code", "execution_count": null, - "id": "456ea462", + "id": "67b22a7c", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "6ac32f39", + "id": "69751c84", "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": "ecc0e797", + "id": "f940a7dd", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "3230d718", + "id": "93ee7ad2", "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": "4e701a29", + "id": "1f74e69c", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "f25f621a", + "id": "0381fe41", "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": "d86aca94", + "id": "a0330a8e", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "27dc82b1", + "id": "90e99043", "metadata": {}, "source": [ "#### 63. Create an array class that has a name attribute (★★☆)" @@ -1082,14 +1082,14 @@ { "cell_type": "code", "execution_count": null, - "id": "0be1ca98", + "id": "b4a88110", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "7dc40e9d", + "id": "d65a55cd", "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": "dce188cf", + "id": "6f676244", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "9e3e479f", + "id": "832af575", "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": "e12f8de9", + "id": "d7ba6e54", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "2acecb21", + "id": "d446a9fb", "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": "ea8b64ac", + "id": "3497cfef", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "b22d66df", + "id": "945eed56", "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": "f31077a5", + "id": "29c0acf8", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "c994f297", + "id": "4e3c817e", "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": "675d5e69", + "id": "2350501f", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "f4191c54", + "id": "c6aa883c", "metadata": {}, "source": [ "#### 69. How to get the diagonal of a dot product? (★★★)" @@ -1178,14 +1178,14 @@ { "cell_type": "code", "execution_count": null, - "id": "2b9c6c8d", + "id": "4ed11eee", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "095cad09", + "id": "85422ba5", "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": "dfd3073b", + "id": "c56a8fd1", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "402d99df", + "id": "2b4ad9c3", "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": "d6283916", + "id": "c46eaf08", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "3ac2bca6", + "id": "60663487", "metadata": {}, "source": [ "#### 72. How to swap two rows of an array? (★★★)" @@ -1226,14 +1226,14 @@ { "cell_type": "code", "execution_count": null, - "id": "f48b17e8", + "id": "ce123bf0", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "b4a03817", + "id": "47c3ccf3", "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": "30904f2e", + "id": "756c1b27", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "18d8341c", + "id": "fb7c8691", "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": "1357ff13", + "id": "7d3b529f", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "0bc12ffe", + "id": "49691c25", "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": "09627c59", + "id": "03e534cf", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "de8d3343", + "id": "26c862ef", "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": "1c8cf783", + "id": "be44258f", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "ee0952b7", + "id": "e873e40e", "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": "761c8a08", + "id": "1caba306", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "1a67e5d2", + "id": "a2962b5e", "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": "a457bcff", + "id": "a9b0686d", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "76628eff", + "id": "0528f198", "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": "26f32598", + "id": "6e7605f3", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "ed33f16f", + "id": "c8cd212a", "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": "fedc2807", + "id": "881a5d91", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "77f36de3", + "id": "dd2fe955", "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": "1649bd8f", + "id": "0a26781a", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "730d107b", + "id": "25f6c024", "metadata": {}, "source": [ "#### 82. Compute a matrix rank (★★★)" @@ -1386,14 +1386,14 @@ { "cell_type": "code", "execution_count": null, - "id": "83d96d46", + "id": "2239ec39", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "e1eb53e9", + "id": "ab340078", "metadata": {}, "source": [ "#### 83. How to find the most frequent value in an array?" @@ -1402,14 +1402,14 @@ { "cell_type": "code", "execution_count": null, - "id": "f1b6a86b", + "id": "9e4e0907", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "afc2a8c3", + "id": "8ca6efa2", "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": "20f74d99", + "id": "91c3a75f", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "de5c4b4e", + "id": "7c464e31", "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": "26fb4bc2", + "id": "c1cbe28b", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "2b051e56", + "id": "1a1f0ec3", "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": "a531cfc1", + "id": "1ef0c2c2", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "2aececf7", + "id": "cd65d050", "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": "59c3d79c", + "id": "2fab1343", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "7e7d3389", + "id": "d42049b1", "metadata": {}, "source": [ "#### 88. How to implement the Game of Life using numpy arrays? (★★★)" @@ -1482,14 +1482,14 @@ { "cell_type": "code", "execution_count": null, - "id": "068158c8", + "id": "f52ce705", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "db62deda", + "id": "bdb31b75", "metadata": {}, "source": [ "#### 89. How to get the n largest values of an array (★★★)" @@ -1498,14 +1498,14 @@ { "cell_type": "code", "execution_count": null, - "id": "b21ede2e", + "id": "61544df8", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "3a708816", + "id": "90a0055e", "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": "6b31b0cd", + "id": "44a35080", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "a8f3bbe0", + "id": "8e49aec8", "metadata": {}, "source": [ "#### 91. How to create a record array from a regular array? (★★★)" @@ -1530,14 +1530,14 @@ { "cell_type": "code", "execution_count": null, - "id": "2fd5dd75", + "id": "f6b0c8d7", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "217349c5", + "id": "ad266d27", "metadata": {}, "source": [ "#### 92. Consider a large vector Z, compute Z to the power of 3 using 3 different methods (★★★)" @@ -1546,30 +1546,30 @@ { "cell_type": "code", "execution_count": null, - "id": "05fe0425", + "id": "889dca38", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "efff4c6c", + "id": "698f6f8d", "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? (★★★)" + "#### 93. Consider two arrays A and B of shape (8,3) and (2,2). How to find rows of A that contain at least one element of each row of B regardless of the order of the elements in B?" ] }, { "cell_type": "code", "execution_count": null, - "id": "5d1c45d3", + "id": "7edf343e", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "3f5b046b", + "id": "f241c0a4", "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": "ef98dcd8", + "id": "a5ddfbb3", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "34446b6b", + "id": "d94a00f3", "metadata": {}, "source": [ "#### 95. Convert a vector of ints into a matrix binary representation (★★★)" @@ -1594,14 +1594,14 @@ { "cell_type": "code", "execution_count": null, - "id": "0dc03a0e", + "id": "6c8fed99", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "cb091f58", + "id": "acfeb574", "metadata": {}, "source": [ "#### 96. Given a two dimensional array, how to extract unique rows? (★★★)" @@ -1610,14 +1610,14 @@ { "cell_type": "code", "execution_count": null, - "id": "068310df", + "id": "fce027f6", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "bc6d7f7f", + "id": "1630076d", "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": "db1d298b", + "id": "46e77e02", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "fec0aced", + "id": "77a252e9", "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": "cd8582d6", + "id": "8f06e8c9", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "c085a040", + "id": "c1b1b575", "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": "971b6bf3", + "id": "611dff66", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "f8dfde8c", + "id": "e3542a74", "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": "8bf8c280", + "id": "e3893074", "metadata": {}, "outputs": [], "source": [] diff --git a/100_Numpy_exercises.md b/100_Numpy_exercises.md index 54e1c5c..c8ea459 100644 --- a/100_Numpy_exercises.md +++ b/100_Numpy_exercises.md @@ -232,7 +232,7 @@ np.sqrt(-1) == np.emath.sqrt(-1) #### 92. Consider a large vector Z, compute Z to the power of 3 using 3 different methods (★★★) -#### 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? (★★★) +#### 93. Consider two arrays A and B of shape (8,3) and (2,2). How to find rows of A that contain at least one element of each row of B regardless of the order of the elements in B? #### 94. Considering a 10x3 matrix, extract rows with unequal values (e.g. [2,2,3]) (★★★) diff --git a/100_Numpy_exercises_with_hints.md b/100_Numpy_exercises_with_hints.md index 513fa3f..0dbc261 100644 --- a/100_Numpy_exercises_with_hints.md +++ b/100_Numpy_exercises_with_hints.md @@ -232,7 +232,7 @@ np.sqrt(-1) == np.emath.sqrt(-1) `hint: np.core.records.fromarrays` #### 92. Consider a large vector Z, compute Z to the power of 3 using 3 different methods (★★★) `hint: np.power, *, np.einsum` -#### 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? (★★★) +#### 93. Consider two arrays A and B of shape (8,3) and (2,2). How to find rows of A that contain at least one element of each row of B regardless of the order of the elements in B? `hint: np.where` #### 94. Considering a 10x3 matrix, extract rows with unequal values (e.g. [2,2,3]) (★★★) `No hints provided...` diff --git a/100_Numpy_exercises_with_hints_with_solutions.md b/100_Numpy_exercises_with_hints_with_solutions.md index 1e54ecb..b4b3388 100644 --- a/100_Numpy_exercises_with_hints_with_solutions.md +++ b/100_Numpy_exercises_with_hints_with_solutions.md @@ -1181,7 +1181,7 @@ x = np.random.rand(int(5e7)) %timeit x*x*x %timeit np.einsum('i,i,i->i',x,x,x) ``` -#### 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? (★★★) +#### 93. Consider two arrays A and B of shape (8,3) and (2,2). How to find rows of A that contain at least one element of each row of B regardless of the order of the elements in B? `hint: np.where` ```python diff --git a/100_Numpy_exercises_with_solutions.md b/100_Numpy_exercises_with_solutions.md index fea35e2..23beca8 100644 --- a/100_Numpy_exercises_with_solutions.md +++ b/100_Numpy_exercises_with_solutions.md @@ -1181,7 +1181,7 @@ x = np.random.rand(int(5e7)) %timeit x*x*x %timeit np.einsum('i,i,i->i',x,x,x) ``` -#### 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? (★★★) +#### 93. Consider two arrays A and B of shape (8,3) and (2,2). How to find rows of A that contain at least one element of each row of B regardless of the order of the elements in B? ```python diff --git a/100_Numpy_random.ipynb b/100_Numpy_random.ipynb index 0bf202d..ac0daa0 100644 --- a/100_Numpy_random.ipynb +++ b/100_Numpy_random.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "markdown", - "id": "261c7cb9", + "id": "0853fe0e", "metadata": {}, "source": [ "# 100 numpy exercises\n", @@ -18,7 +18,7 @@ }, { "cell_type": "markdown", - "id": "b0ffec6a", + "id": "042c5a4b", "metadata": {}, "source": [ "File automatically generated. See the documentation to update questions/answers/hints programmatically." @@ -26,7 +26,7 @@ }, { "cell_type": "markdown", - "id": "30434163", + "id": "021cd2a9", "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": "8f632d6c", + "id": "762ccffd", "metadata": {}, "outputs": [], "source": [ @@ -46,7 +46,7 @@ { "cell_type": "code", "execution_count": null, - "id": "6cfc4341", + "id": "61737701", "metadata": {}, "outputs": [], "source": [