diff --git a/100_Numpy_exercises.ipynb b/100_Numpy_exercises.ipynb index 01b3568..5c00ef6 100644 --- a/100_Numpy_exercises.ipynb +++ b/100_Numpy_exercises.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "markdown", - "id": "52249554", + "id": "86d7d90d", "metadata": {}, "source": [ "# 100 numpy exercises\n", @@ -18,7 +18,7 @@ }, { "cell_type": "markdown", - "id": "9f0e56c3", + "id": "23c3de32", "metadata": {}, "source": [ "File automatically generated. See the documentation to update questions/answers/hints programmatically." @@ -26,7 +26,7 @@ }, { "cell_type": "markdown", - "id": "04b078a1", + "id": "71eb9a4d", "metadata": {}, "source": [ "Run the `initialize.py` module, then for each question you can query the\n", @@ -36,7 +36,7 @@ { "cell_type": "code", "execution_count": null, - "id": "63ebbf77", + "id": "f752b0f7", "metadata": {}, "outputs": [], "source": [ @@ -45,7 +45,7 @@ }, { "cell_type": "markdown", - "id": "e1672806", + "id": "4f870b6b", "metadata": {}, "source": [ "#### 1. Import the numpy package under the name `np` (★☆☆)" @@ -54,14 +54,14 @@ { "cell_type": "code", "execution_count": null, - "id": "c39a5917", + "id": "62928f13", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "1d2a0271", + "id": "edadb91f", "metadata": {}, "source": [ "#### 2. Print the numpy version and the configuration (★☆☆)" @@ -70,14 +70,14 @@ { "cell_type": "code", "execution_count": null, - "id": "d4160594", + "id": "81886061", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "4490d971", + "id": "68fc6d4e", "metadata": {}, "source": [ "#### 3. Create a null vector of size 10 (★☆☆)" @@ -86,14 +86,14 @@ { "cell_type": "code", "execution_count": null, - "id": "9b383fcc", + "id": "49109360", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "4a217c03", + "id": "0b1d0ac2", "metadata": {}, "source": [ "#### 4. How to find the memory size of any array (★☆☆)" @@ -102,14 +102,14 @@ { "cell_type": "code", "execution_count": null, - "id": "7f7c6497", + "id": "73b370a4", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "4a412447", + "id": "56876d69", "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": "9b19b55d", + "id": "95294b19", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "8e890e32", + "id": "ee618a7b", "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": "032ba316", + "id": "ce78fafd", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "36562b7f", + "id": "fd391587", "metadata": {}, "source": [ "#### 7. Create a vector with values ranging from 10 to 49 (★☆☆)" @@ -150,14 +150,14 @@ { "cell_type": "code", "execution_count": null, - "id": "2f1ae242", + "id": "327d872f", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "d7d0e7ff", + "id": "74e245ac", "metadata": {}, "source": [ "#### 8. Reverse a vector (first element becomes last) (★☆☆)" @@ -166,14 +166,14 @@ { "cell_type": "code", "execution_count": null, - "id": "5a07c5c1", + "id": "d1dd2da1", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "1fb509ab", + "id": "330064b1", "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": "b4702eb8", + "id": "ad5a2a6e", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "0d0120c4", + "id": "51a32c95", "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": "ead69303", + "id": "9d820628", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "c001c117", + "id": "bcc28d4e", "metadata": {}, "source": [ "#### 11. Create a 3x3 identity matrix (★☆☆)" @@ -214,14 +214,14 @@ { "cell_type": "code", "execution_count": null, - "id": "e7f9417c", + "id": "eb504454", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "5f75e7a8", + "id": "3adc02f3", "metadata": {}, "source": [ "#### 12. Create a 3x3x3 array with random values (★☆☆)" @@ -230,14 +230,14 @@ { "cell_type": "code", "execution_count": null, - "id": "ec3c59bb", + "id": "8b91d489", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "7f012f49", + "id": "a7cd9e02", "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": "8247cae0", + "id": "a9871150", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "7d976fd0", + "id": "9c007df3", "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": "1d5c77fd", + "id": "2ee43ca7", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "c5266946", + "id": "290aab6f", "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": "140eff80", + "id": "def020fc", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "2dd11b32", + "id": "d5949ccd", "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": "ab1d50a8", + "id": "47aa6e9b", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "a7c39246", + "id": "da6ea764", "metadata": {}, "source": [ "#### 17. What is the result of the following expression? (★☆☆)\n", @@ -318,14 +318,14 @@ { "cell_type": "code", "execution_count": null, - "id": "d5f154f3", + "id": "047bf4c4", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "757aff7a", + "id": "e0b25bf8", "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": "fc9309c9", + "id": "9392a2cc", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "ae259b07", + "id": "ffc7e714", "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": "ceccc277", + "id": "1892d23a", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "9f5d548e", + "id": "42412361", "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": "ebf6fa3c", + "id": "c4060dee", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "e1fe826b", + "id": "fec7cacf", "metadata": {}, "source": [ "#### 21. Create a checkerboard 8x8 matrix using the tile function (★☆☆)" @@ -382,14 +382,14 @@ { "cell_type": "code", "execution_count": null, - "id": "7cbd262a", + "id": "b94338bc", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "3cc99a9e", + "id": "e3bb989c", "metadata": {}, "source": [ "#### 22. Normalize a 5x5 random matrix (★☆☆)" @@ -398,14 +398,14 @@ { "cell_type": "code", "execution_count": null, - "id": "96ec3796", + "id": "95b73cf2", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "3cefb279", + "id": "9bf6610b", "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": "06231cc7", + "id": "0f45e6b5", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "04177bf2", + "id": "0c4834ae", "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": "25c78847", + "id": "7b4bbf2b", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "14a38540", + "id": "c7b74399", "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": "85a57a73", + "id": "30f35110", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "bb586fda", + "id": "da225b07", "metadata": {}, "source": [ "#### 26. What is the output of the following script? (★☆☆)\n", @@ -469,14 +469,14 @@ { "cell_type": "code", "execution_count": null, - "id": "5094a699", + "id": "1c55040f", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "198cdc05", + "id": "60e2d14c", "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": "a4195583", + "id": "cb463c42", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "d83bb54b", + "id": "aa45d8b5", "metadata": {}, "source": [ "#### 28. What are the result of the following expressions? (★☆☆)\n", @@ -514,14 +514,14 @@ { "cell_type": "code", "execution_count": null, - "id": "277bd109", + "id": "ff3500c8", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "415d2d18", + "id": "0b6acccc", "metadata": {}, "source": [ "#### 29. How to round away from zero a float array ? (★☆☆)" @@ -530,14 +530,14 @@ { "cell_type": "code", "execution_count": null, - "id": "63df40b7", + "id": "f0d27652", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "40ddc32c", + "id": "87fb3166", "metadata": {}, "source": [ "#### 30. How to find common values between two arrays? (★☆☆)" @@ -546,14 +546,14 @@ { "cell_type": "code", "execution_count": null, - "id": "e4e221c3", + "id": "e76b28fc", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "96a2b00a", + "id": "7182fca7", "metadata": {}, "source": [ "#### 31. How to ignore all numpy warnings (not recommended)? (★☆☆)" @@ -562,14 +562,14 @@ { "cell_type": "code", "execution_count": null, - "id": "97517a73", + "id": "652def1c", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "5164e541", + "id": "115e0e2a", "metadata": {}, "source": [ "#### 32. Is the following expressions true? (★☆☆)\n", @@ -581,14 +581,14 @@ { "cell_type": "code", "execution_count": null, - "id": "c05aab40", + "id": "d0573d89", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "6ff7fdf8", + "id": "96c9777e", "metadata": {}, "source": [ "#### 33. How to get the dates of yesterday, today and tomorrow? (★☆☆)" @@ -597,14 +597,14 @@ { "cell_type": "code", "execution_count": null, - "id": "6156213d", + "id": "fc0f2ec9", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "97dde2a7", + "id": "85237768", "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": "249806a3", + "id": "baf7883b", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "78f4ad65", + "id": "7873c566", "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": "d9bee25c", + "id": "a454d2c4", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "1a194d91", + "id": "f4f87f16", "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": "46a4ab0a", + "id": "11fd9724", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "23a9130d", + "id": "c8c3ac99", "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": "8a08a042", + "id": "be466890", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "e225eafa", + "id": "f8a91990", "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": "7c2acc82", + "id": "58568088", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "2f61a8f5", + "id": "a4a6c98c", "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": "ef7bc11c", + "id": "4faf8ebb", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "d5ae0694", + "id": "76b7ccb5", "metadata": {}, "source": [ "#### 40. Create a random vector of size 10 and sort it (★★☆)" @@ -709,14 +709,14 @@ { "cell_type": "code", "execution_count": null, - "id": "0b5a407d", + "id": "03f7e0a0", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "1a355962", + "id": "f1d3571d", "metadata": {}, "source": [ "#### 41. How to sum a small array faster than np.sum? (★★☆)" @@ -725,14 +725,14 @@ { "cell_type": "code", "execution_count": null, - "id": "70917f47", + "id": "6a9e7a0f", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "42e65d4e", + "id": "5f2e7f1d", "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": "c4ae0c00", + "id": "fea071d5", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "37c295dd", + "id": "25089569", "metadata": {}, "source": [ "#### 43. Make an array immutable (read-only) (★★☆)" @@ -757,14 +757,14 @@ { "cell_type": "code", "execution_count": null, - "id": "5a2cf158", + "id": "b39c64fd", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "e92096de", + "id": "8e0a92a1", "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": "7d4f2602", + "id": "5dbb04dc", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "a71d88ea", + "id": "be171fc3", "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": "1298225a", + "id": "29050d57", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "261c253c", + "id": "ec8169df", "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": "9150a4a1", + "id": "e1d5811b", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "265deebd", + "id": "6edcee5a", "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": "9fad2a0d", + "id": "72ce5000", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "f9007f53", + "id": "e792c9d8", "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": "2a97c51a", + "id": "57f92e12", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "5130ea82", + "id": "eca7651c", "metadata": {}, "source": [ "#### 49. How to print all the values of an array? (★★☆)" @@ -853,14 +853,14 @@ { "cell_type": "code", "execution_count": null, - "id": "4636aa7a", + "id": "9fea21ff", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "56edae30", + "id": "b2527d57", "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": "b34f3cf0", + "id": "1f94b185", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "a8f3578d", + "id": "42580cd7", "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": "4e9c2e30", + "id": "c280e178", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "cd41c4dc", + "id": "7e23ffda", "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": "53f15c5d", + "id": "9d07ad1d", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "0e2990a0", + "id": "8a2a9157", "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": "8d7e441e", + "id": "4ffd9601", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "9e3fae09", + "id": "10b38393", "metadata": {}, "source": [ "#### 54. How to read the following file? (★★☆)\n", @@ -938,14 +938,14 @@ { "cell_type": "code", "execution_count": null, - "id": "5ce8d677", + "id": "29f152f2", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "dc8e013a", + "id": "72ac7927", "metadata": {}, "source": [ "#### 55. What is the equivalent of enumerate for numpy arrays? (★★☆)" @@ -954,14 +954,14 @@ { "cell_type": "code", "execution_count": null, - "id": "6b46c440", + "id": "3de613a5", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "acdcd8d9", + "id": "db66e31f", "metadata": {}, "source": [ "#### 56. Generate a generic 2D Gaussian-like array (★★☆)" @@ -970,14 +970,14 @@ { "cell_type": "code", "execution_count": null, - "id": "c00f8b49", + "id": "60673f97", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "3f0bf645", + "id": "6997c220", "metadata": {}, "source": [ "#### 57. How to randomly place p elements in a 2D array? (★★☆)" @@ -986,14 +986,14 @@ { "cell_type": "code", "execution_count": null, - "id": "bac7ed71", + "id": "81ac5c03", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "b9ac42fc", + "id": "c168fd87", "metadata": {}, "source": [ "#### 58. Subtract the mean of each row of a matrix (★★☆)" @@ -1002,14 +1002,14 @@ { "cell_type": "code", "execution_count": null, - "id": "0cac0138", + "id": "4158b357", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "f2c2e314", + "id": "f74204b8", "metadata": {}, "source": [ "#### 59. How to sort an array by the nth column? (★★☆)" @@ -1018,14 +1018,14 @@ { "cell_type": "code", "execution_count": null, - "id": "61739197", + "id": "665d7fb2", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "e8311ecb", + "id": "26b46ba2", "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": "a3fd8a79", + "id": "0420e879", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "e9617f44", + "id": "a7c73eaf", "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": "2fb40d45", + "id": "e00ee9c8", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "892813ba", + "id": "016f9f8f", "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": "0ec3c1ee", + "id": "5d73e1b3", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "467ff234", + "id": "5d04eec4", "metadata": {}, "source": [ "#### 63. Create an array class that has a name attribute (★★☆)" @@ -1082,14 +1082,14 @@ { "cell_type": "code", "execution_count": null, - "id": "43d6ba35", + "id": "ddb2a215", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "af15b01a", + "id": "9efcd2a7", "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": "52d9315d", + "id": "4ed538af", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "62d21ba4", + "id": "9215bb59", "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": "56497ebc", + "id": "74f5d129", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "fd7e0104", + "id": "a0ea705d", "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": "1cf3edec", + "id": "6a800287", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "dd70e081", + "id": "39363678", "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": "e36af36e", + "id": "f4102915", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "58333fa3", + "id": "8d0566f6", "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": "acfc823e", + "id": "b698d7c6", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "afbf2ff8", + "id": "c5f95a28", "metadata": {}, "source": [ "#### 69. How to get the diagonal of a dot product? (★★★)" @@ -1178,14 +1178,14 @@ { "cell_type": "code", "execution_count": null, - "id": "8b44d9cb", + "id": "5ff048b9", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "01a1aeea", + "id": "9926d135", "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": "2105434f", + "id": "b3309bfc", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "1da61b2c", + "id": "77faa124", "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": "0f257760", + "id": "f632f202", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "d465a23a", + "id": "56aa0c3d", "metadata": {}, "source": [ "#### 72. How to swap two rows of an array? (★★★)" @@ -1226,14 +1226,14 @@ { "cell_type": "code", "execution_count": null, - "id": "85dbac4a", + "id": "efe16b12", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "64916675", + "id": "1068d7cd", "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": "22cf4ff6", + "id": "e391b049", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "5ac69993", + "id": "bcc34f27", "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": "6e2b4a3b", + "id": "beb8e3e8", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "41ae2500", + "id": "b365cc6c", "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": "460cdb46", + "id": "e0831ba1", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "8a968a39", + "id": "91c854ca", "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": "5d37d9b3", + "id": "1ac36aad", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "d8c056da", + "id": "cbd50313", "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": "3a0ba740", + "id": "7c3b7ab3", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "6a2632c8", + "id": "94a6cb93", "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": "cce12b75", + "id": "705f9fa8", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "7cba6ae9", + "id": "7a7b065f", "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": "576c542d", + "id": "a76a1e2a", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "1402e9db", + "id": "a4a22eac", "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": "8aa89c72", + "id": "3d0a25fa", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "159a6dff", + "id": "3e056709", "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": "ce160b57", + "id": "16e69f8c", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "9479f684", + "id": "df30b27a", "metadata": {}, "source": [ "#### 82. Compute a matrix rank (★★★)" @@ -1386,14 +1386,14 @@ { "cell_type": "code", "execution_count": null, - "id": "9a6e088c", + "id": "a1d0d2cb", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "5b6a81c5", + "id": "f03b141d", "metadata": {}, "source": [ "#### 83. How to find the most frequent value in an array?" @@ -1402,14 +1402,14 @@ { "cell_type": "code", "execution_count": null, - "id": "cf1864ba", + "id": "4ba555c9", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "8142c362", + "id": "eccb13af", "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": "18b475ed", + "id": "c0fa657e", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "6870a477", + "id": "1e955525", "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": "13de2582", + "id": "4b640a43", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "502a6bb6", + "id": "24891de6", "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": "5f575c8e", + "id": "3be310a0", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "b974e01e", + "id": "c250f782", "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": "6e691289", + "id": "090b56b5", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "0b8c10c7", + "id": "48064219", "metadata": {}, "source": [ "#### 88. How to implement the Game of Life using numpy arrays? (★★★)" @@ -1482,14 +1482,14 @@ { "cell_type": "code", "execution_count": null, - "id": "cd02f7f3", + "id": "4c9bd070", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "e3f82541", + "id": "3ad7c2e9", "metadata": {}, "source": [ "#### 89. How to get the n largest values of an array (★★★)" @@ -1498,14 +1498,14 @@ { "cell_type": "code", "execution_count": null, - "id": "4fbd0146", + "id": "8d1dd580", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "a7539391", + "id": "3a64b152", "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": "c6981e30", + "id": "e1bd5177", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "faf28eb3", + "id": "2d4144aa", "metadata": {}, "source": [ "#### 91. How to create a record array from a regular array? (★★★)" @@ -1530,14 +1530,14 @@ { "cell_type": "code", "execution_count": null, - "id": "bf3c36e8", + "id": "d66e2085", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "bd45d52b", + "id": "3712ad1d", "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": "57add736", + "id": "5627fa7c", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "6403bb1e", + "id": "7aadddea", "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": "5e99e376", + "id": "2d2bcb22", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "2b1a4d2d", + "id": "ce2e0ef6", "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": "9d12a8cd", + "id": "94b79a35", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "523fbb20", + "id": "fac5263a", "metadata": {}, "source": [ "#### 95. Convert a vector of ints into a matrix binary representation (★★★)" @@ -1594,14 +1594,14 @@ { "cell_type": "code", "execution_count": null, - "id": "b17c2ce8", + "id": "62291895", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "3eb5ef69", + "id": "85bcb763", "metadata": {}, "source": [ "#### 96. Given a two dimensional array, how to extract unique rows? (★★★)" @@ -1610,14 +1610,14 @@ { "cell_type": "code", "execution_count": null, - "id": "092c2c20", + "id": "c348ae06", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "c0e13fe1", + "id": "5d35d3f8", "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": "b7b697cd", + "id": "7f04a281", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "cc071dbe", + "id": "fd4cd346", "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": "fec9fe86", + "id": "65790a2b", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "972f9dc3", + "id": "5f0572af", "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": "5b284c86", + "id": "fcea30b6", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "afad9d6e", + "id": "0c86dfd0", "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": "f1885c64", + "id": "05e1d7f9", "metadata": {}, "outputs": [], "source": [] diff --git a/100_Numpy_exercises_with_hints_with_solutions.md b/100_Numpy_exercises_with_hints_with_solutions.md index 3d0a490..6e60dd2 100644 --- a/100_Numpy_exercises_with_hints_with_solutions.md +++ b/100_Numpy_exercises_with_hints_with_solutions.md @@ -96,15 +96,6 @@ print(Z) Z = np.random.random((3,3,3)) print(Z) ``` -`hint: np.random.default_rng().random` -```python -# Author: KnightSnape - -rng = np.random.default_rng() -Z = rng.random((3, 3, 3)) -print(Z) -``` - #### 13. Create a 10x10 array with random values and find the minimum and maximum values (★☆☆) `hint: min, max` @@ -956,8 +947,8 @@ Z_start = (np.maximum(Z_start,0)).tolist() R_stop = np.maximum(R_start, (R_stop - np.maximum(Z_stop-Zs,0))).tolist() Z_stop = (np.minimum(Z_stop,Zs)).tolist() -r = [slice(start,stop) for start,stop in zip(R_start,R_stop)] -z = [slice(start,stop) for start,stop in zip(Z_start,Z_stop)] +r = tuple([slice(start,stop) for start,stop in zip(R_start,R_stop)]) +z = tuple([slice(start,stop) for start,stop in zip(Z_start,Z_stop)]) R[r] = Z[z] print(Z) print(R) diff --git a/100_Numpy_exercises_with_solutions.md b/100_Numpy_exercises_with_solutions.md index 0780934..307e658 100644 --- a/100_Numpy_exercises_with_solutions.md +++ b/100_Numpy_exercises_with_solutions.md @@ -947,8 +947,8 @@ Z_start = (np.maximum(Z_start,0)).tolist() R_stop = np.maximum(R_start, (R_stop - np.maximum(Z_stop-Zs,0))).tolist() Z_stop = (np.minimum(Z_stop,Zs)).tolist() -r = [slice(start,stop) for start,stop in zip(R_start,R_stop)] -z = [slice(start,stop) for start,stop in zip(Z_start,Z_stop)] +r = tuple([slice(start,stop) for start,stop in zip(R_start,R_stop)]) +z = tuple([slice(start,stop) for start,stop in zip(Z_start,Z_stop)]) R[r] = Z[z] print(Z) print(R) diff --git a/100_Numpy_random.ipynb b/100_Numpy_random.ipynb index e77c9e5..2766fc5 100644 --- a/100_Numpy_random.ipynb +++ b/100_Numpy_random.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "markdown", - "id": "ea6cbc4b", + "id": "c1a8ea63", "metadata": {}, "source": [ "# 100 numpy exercises\n", @@ -18,7 +18,7 @@ }, { "cell_type": "markdown", - "id": "354a533b", + "id": "8481f467", "metadata": {}, "source": [ "File automatically generated. See the documentation to update questions/answers/hints programmatically." @@ -26,7 +26,7 @@ }, { "cell_type": "markdown", - "id": "9efa41bf", + "id": "7c1a3673", "metadata": {}, "source": [ "Run the `initialize.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": "1a6e8fdb", + "id": "6db52927", "metadata": {}, "outputs": [], "source": [ @@ -46,7 +46,7 @@ { "cell_type": "code", "execution_count": null, - "id": "d1e7d785", + "id": "2c9f898f", "metadata": {}, "outputs": [], "source": [ diff --git a/source/exercises100.ktx b/source/exercises100.ktx index e615e7f..d1457aa 100644 --- a/source/exercises100.ktx +++ b/source/exercises100.ktx @@ -1190,8 +1190,8 @@ Z_start = (np.maximum(Z_start,0)).tolist() R_stop = np.maximum(R_start, (R_stop - np.maximum(Z_stop-Zs,0))).tolist() Z_stop = (np.minimum(Z_stop,Zs)).tolist() -r = [slice(start,stop) for start,stop in zip(R_start,R_stop)] -z = [slice(start,stop) for start,stop in zip(Z_start,Z_stop)] +r = tuple([slice(start,stop) for start,stop in zip(R_start,R_stop)]) +z = tuple([slice(start,stop) for start,stop in zip(Z_start,Z_stop)]) R[r] = Z[z] print(Z) print(R)