From 025845ff5c80ea4581d22c1e860c23c16db11704 Mon Sep 17 00:00:00 2001 From: Francesco Terenzi Date: Fri, 9 Apr 2021 10:50:11 +0200 Subject: [PATCH] Updated question 37, keep previous solution for reference --- 100_Numpy_exercises.ipynb | 408 +++++++++--------- ...mpy_exercises_with_hints_with_solutions.md | 5 + 100_Numpy_exercises_with_solutions.md | 5 + 100_Numpy_random.ipynb | 10 +- source/exercises100.ktx | 5 + 5 files changed, 224 insertions(+), 209 deletions(-) diff --git a/100_Numpy_exercises.ipynb b/100_Numpy_exercises.ipynb index 5ef0925..ae49130 100644 --- a/100_Numpy_exercises.ipynb +++ b/100_Numpy_exercises.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "markdown", - "id": "8d5f9376", + "id": "c742d9cc", "metadata": {}, "source": [ "# 100 numpy exercises\n", @@ -18,7 +18,7 @@ }, { "cell_type": "markdown", - "id": "f3991601", + "id": "4292e21c", "metadata": {}, "source": [ "File automatically generated. See the documentation to update questions/answers/hints programmatically." @@ -26,7 +26,7 @@ }, { "cell_type": "markdown", - "id": "00eb0a43", + "id": "e1eba8a7", "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": "71de66b9", + "id": "1ca6f730", "metadata": {}, "outputs": [], "source": [ @@ -45,7 +45,7 @@ }, { "cell_type": "markdown", - "id": "7bfd9bff", + "id": "c6ed9243", "metadata": {}, "source": [ "#### 1. Import the numpy package under the name `np` (★☆☆)" @@ -54,14 +54,14 @@ { "cell_type": "code", "execution_count": null, - "id": "87399bef", + "id": "4dc18925", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "cb191df0", + "id": "b4882d94", "metadata": {}, "source": [ "#### 2. Print the numpy version and the configuration (★☆☆)" @@ -70,14 +70,14 @@ { "cell_type": "code", "execution_count": null, - "id": "6bddfd8d", + "id": "9a41fb2b", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "8d29e132", + "id": "084258bf", "metadata": {}, "source": [ "#### 3. Create a null vector of size 10 (★☆☆)" @@ -86,14 +86,14 @@ { "cell_type": "code", "execution_count": null, - "id": "98d87f02", + "id": "660c6b91", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "8ccac176", + "id": "0b7ea863", "metadata": {}, "source": [ "#### 4. How to find the memory size of any array (★☆☆)" @@ -102,14 +102,14 @@ { "cell_type": "code", "execution_count": null, - "id": "2a350784", + "id": "ac12eeb3", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "cfdb15e5", + "id": "399ac53c", "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": "14c16b65", + "id": "8706e03d", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "aa575121", + "id": "94fb58be", "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": "ba8cdb73", + "id": "618f340f", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "182022f5", + "id": "d577e66c", "metadata": {}, "source": [ "#### 7. Create a vector with values ranging from 10 to 49 (★☆☆)" @@ -150,14 +150,14 @@ { "cell_type": "code", "execution_count": null, - "id": "9aeebf5e", + "id": "091a60f4", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "cb876890", + "id": "bc9c60e2", "metadata": {}, "source": [ "#### 8. Reverse a vector (first element becomes last) (★☆☆)" @@ -166,14 +166,14 @@ { "cell_type": "code", "execution_count": null, - "id": "0f76940e", + "id": "23978628", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "eb4808bf", + "id": "7454fb7b", "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": "1b315e4c", + "id": "28f9a38f", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "6b963906", + "id": "caf21358", "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": "fb8e5d22", + "id": "d390f4e1", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "32162d7a", + "id": "1dd829ac", "metadata": {}, "source": [ "#### 11. Create a 3x3 identity matrix (★☆☆)" @@ -214,14 +214,14 @@ { "cell_type": "code", "execution_count": null, - "id": "2a1f76ee", + "id": "1dde894f", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "40e9c864", + "id": "322aa74e", "metadata": {}, "source": [ "#### 12. Create a 3x3x3 array with random values (★☆☆)" @@ -230,14 +230,14 @@ { "cell_type": "code", "execution_count": null, - "id": "348d9b8b", + "id": "1f5c8d38", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "ccddf92a", + "id": "82e8c932", "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": "5cb70d41", + "id": "85c6a59d", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "1e721bf5", + "id": "c78b3d5e", "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": "820bf543", + "id": "37149237", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "bd1601a4", + "id": "c07320af", "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": "55b2da2b", + "id": "19897ca9", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "97a1cbff", + "id": "c6d094c4", "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": "40bb1714", + "id": "9c05295e", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "bab40feb", + "id": "6e00ebde", "metadata": {}, "source": [ "#### 17. What is the result of the following expression? (★☆☆)\n", @@ -318,14 +318,14 @@ { "cell_type": "code", "execution_count": null, - "id": "c4e3b9f0", + "id": "5015d2a2", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "73e147e5", + "id": "ffee7be0", "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": "748b0220", + "id": "2db510c1", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "c76e859b", + "id": "0c687bd7", "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": "f6ebaefc", + "id": "c7a51f78", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "d2ae73d8", + "id": "46c55b45", "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": "7bf0d74b", + "id": "d95d1406", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "1028425d", + "id": "4898dddb", "metadata": {}, "source": [ "#### 21. Create a checkerboard 8x8 matrix using the tile function (★☆☆)" @@ -382,14 +382,14 @@ { "cell_type": "code", "execution_count": null, - "id": "79512e34", + "id": "de7dc9d8", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "b2fbd122", + "id": "ef2963f0", "metadata": {}, "source": [ "#### 22. Normalize a 5x5 random matrix (★☆☆)" @@ -398,14 +398,14 @@ { "cell_type": "code", "execution_count": null, - "id": "87641a2c", + "id": "f92f76ce", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "2509c57c", + "id": "09390c44", "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": "52f8acea", + "id": "86bea398", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "ded9a899", + "id": "e8ff25fc", "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": "3c298bde", + "id": "bcb2fa2e", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "fa460ccc", + "id": "ce43c805", "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": "9f2f8d69", + "id": "2b915f12", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "4dd2ddbb", + "id": "20696fea", "metadata": {}, "source": [ "#### 26. What is the output of the following script? (★☆☆)\n", @@ -469,14 +469,14 @@ { "cell_type": "code", "execution_count": null, - "id": "c430369b", + "id": "89d43108", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "b9f9c658", + "id": "9f3e3396", "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": "d143bbc3", + "id": "fa6a4ad1", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "9a17682c", + "id": "99a826bb", "metadata": {}, "source": [ "#### 28. What are the result of the following expressions?\n", @@ -514,14 +514,14 @@ { "cell_type": "code", "execution_count": null, - "id": "ab7599cf", + "id": "f5178b8b", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "fa411d12", + "id": "04270e58", "metadata": {}, "source": [ "#### 29. How to round away from zero a float array ? (★☆☆)" @@ -530,14 +530,14 @@ { "cell_type": "code", "execution_count": null, - "id": "9f12134e", + "id": "7dcb8271", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "7b52f26a", + "id": "186af0bd", "metadata": {}, "source": [ "#### 30. How to find common values between two arrays? (★☆☆)" @@ -546,14 +546,14 @@ { "cell_type": "code", "execution_count": null, - "id": "7fe39358", + "id": "751ce3d6", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "a6749c05", + "id": "8dc246e6", "metadata": {}, "source": [ "#### 31. How to ignore all numpy warnings (not recommended)? (★☆☆)" @@ -562,14 +562,14 @@ { "cell_type": "code", "execution_count": null, - "id": "ccdab4e3", + "id": "882d5831", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "1f25789b", + "id": "46286b98", "metadata": {}, "source": [ "#### 32. Is the following expressions true? (★☆☆)\n", @@ -581,14 +581,14 @@ { "cell_type": "code", "execution_count": null, - "id": "52400a9b", + "id": "35b2ee6a", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "3eecd35d", + "id": "c19f893b", "metadata": {}, "source": [ "#### 33. How to get the dates of yesterday, today and tomorrow? (★☆☆)" @@ -597,14 +597,14 @@ { "cell_type": "code", "execution_count": null, - "id": "0cf79900", + "id": "39f552e8", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "2b1ecb5e", + "id": "7507fb36", "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": "0ec271db", + "id": "23c538eb", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "f7104e3c", + "id": "d62fba94", "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": "d6bfbf1a", + "id": "b517ffa9", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "b2a99af4", + "id": "0593c304", "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": "2f4c5c3b", + "id": "9c7ad92e", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "edef1564", + "id": "e90b042b", "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": "5aa93285", + "id": "7892b4b0", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "34fd06e6", + "id": "a2d7df8e", "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": "30fcb1e1", + "id": "5f323469", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "914fee46", + "id": "064c68c0", "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": "1d3cdc81", + "id": "a179d69e", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "bfc54e3b", + "id": "5682b5b3", "metadata": {}, "source": [ "#### 40. Create a random vector of size 10 and sort it (★★☆)" @@ -709,14 +709,14 @@ { "cell_type": "code", "execution_count": null, - "id": "e530dc04", + "id": "f5e5b511", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "ef014831", + "id": "c8831686", "metadata": {}, "source": [ "#### 41. How to sum a small array faster than np.sum? (★★☆)" @@ -725,14 +725,14 @@ { "cell_type": "code", "execution_count": null, - "id": "1156b032", + "id": "31c6ec4d", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "3313ce07", + "id": "c39eed1e", "metadata": {}, "source": [ "#### 42. Consider two random array A and B, check if they are equal (★★☆)" @@ -741,14 +741,14 @@ { "cell_type": "code", "execution_count": null, - "id": "1317970d", + "id": "c4bf0ba3", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "fc9a5834", + "id": "ffafa4aa", "metadata": {}, "source": [ "#### 43. Make an array immutable (read-only) (★★☆)" @@ -757,14 +757,14 @@ { "cell_type": "code", "execution_count": null, - "id": "bf473a1d", + "id": "c237c0d2", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "9e90f64c", + "id": "86886e5e", "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": "9037151a", + "id": "5146f04c", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "d55519be", + "id": "50740a46", "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": "203c13e6", + "id": "2c11ffdd", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "ab364b3f", + "id": "5979e3ee", "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": "c3b70ace", + "id": "0747c0d6", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "a5ec75bc", + "id": "cd53abc3", "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": "c1c8772e", + "id": "2d14d9b7", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "ae730f76", + "id": "37875f0d", "metadata": {}, "source": [ "#### 48. Print the minimum and maximum representable value for each numpy scalar type (★★☆)" @@ -837,14 +837,14 @@ { "cell_type": "code", "execution_count": null, - "id": "c31c676d", + "id": "1578e3fa", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "71e1d392", + "id": "aa4ce1fd", "metadata": {}, "source": [ "#### 49. How to print all the values of an array? (★★☆)" @@ -853,14 +853,14 @@ { "cell_type": "code", "execution_count": null, - "id": "6d49ec36", + "id": "4f1e9847", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "f5b9005b", + "id": "ae637202", "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": "3b1263ef", + "id": "c4546e5c", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "8ed1cfac", + "id": "93157464", "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": "ac621bf9", + "id": "69c4b979", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "2ef9e21c", + "id": "07ebafa2", "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": "4984f42a", + "id": "2db3c3e5", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "caaf1c14", + "id": "fb93d503", "metadata": {}, "source": [ "#### 53. How to convert a float (32 bits) array into an integer (32 bits) in place?" @@ -917,14 +917,14 @@ { "cell_type": "code", "execution_count": null, - "id": "10e1a512", + "id": "67231bf2", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "7e5080c5", + "id": "9c0ce40b", "metadata": {}, "source": [ "#### 54. How to read the following file? (★★☆)\n", @@ -938,14 +938,14 @@ { "cell_type": "code", "execution_count": null, - "id": "00bf9c38", + "id": "aecef043", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "a0c35578", + "id": "bdfe9e5d", "metadata": {}, "source": [ "#### 55. What is the equivalent of enumerate for numpy arrays? (★★☆)" @@ -954,14 +954,14 @@ { "cell_type": "code", "execution_count": null, - "id": "f64d2874", + "id": "bcc193c9", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "64c4d38a", + "id": "fa2e5fc9", "metadata": {}, "source": [ "#### 56. Generate a generic 2D Gaussian-like array (★★☆)" @@ -970,14 +970,14 @@ { "cell_type": "code", "execution_count": null, - "id": "a4cc553c", + "id": "7b94a92a", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "8455392b", + "id": "465a1ea4", "metadata": {}, "source": [ "#### 57. How to randomly place p elements in a 2D array? (★★☆)" @@ -986,14 +986,14 @@ { "cell_type": "code", "execution_count": null, - "id": "8fbe4fb8", + "id": "c2d1d13f", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "6b89b37f", + "id": "d75998b5", "metadata": {}, "source": [ "#### 58. Subtract the mean of each row of a matrix (★★☆)" @@ -1002,14 +1002,14 @@ { "cell_type": "code", "execution_count": null, - "id": "a59d5bd0", + "id": "44c2043a", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "cab7bfe5", + "id": "4f2e8b7a", "metadata": {}, "source": [ "#### 59. How to sort an array by the nth column? (★★☆)" @@ -1018,14 +1018,14 @@ { "cell_type": "code", "execution_count": null, - "id": "ef0f4e9a", + "id": "d0b80ded", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "f7180832", + "id": "be56980d", "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": "3d261dd9", + "id": "6dc4eaf6", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "df0fc6b0", + "id": "92e0b60c", "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": "38f683dc", + "id": "0b37b462", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "38d042c0", + "id": "476bb933", "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": "ce3f3e39", + "id": "33c8566d", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "fb5f1468", + "id": "64aea808", "metadata": {}, "source": [ "#### 63. Create an array class that has a name attribute (★★☆)" @@ -1082,14 +1082,14 @@ { "cell_type": "code", "execution_count": null, - "id": "2e62ec16", + "id": "db81ac8b", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "26246179", + "id": "fb0a4de3", "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": "331b42fb", + "id": "4726a17d", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "f090f30a", + "id": "ae1a4b8d", "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": "ce79532a", + "id": "2d0f1ee4", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "c190e2c2", + "id": "a5127520", "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": "417894c2", + "id": "546bc0a2", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "ec743250", + "id": "1eaa1407", "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": "1459ed28", + "id": "7823c50c", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "3f0f49bb", + "id": "18cd6a16", "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": "4019dd26", + "id": "2185257f", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "9f58b3ee", + "id": "fb2b3f5d", "metadata": {}, "source": [ "#### 69. How to get the diagonal of a dot product? (★★★)" @@ -1178,14 +1178,14 @@ { "cell_type": "code", "execution_count": null, - "id": "eeeb4a9e", + "id": "8ada08d1", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "c54f9dac", + "id": "3bb7807e", "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": "debab1b2", + "id": "c173afdb", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "bc01953d", + "id": "40e4d74e", "metadata": {}, "source": [ "#### 71. Consider an array of dimension (5,5,3), how to mulitply it by an array with dimensions (5,5)? (★★★)" @@ -1210,14 +1210,14 @@ { "cell_type": "code", "execution_count": null, - "id": "6d090e70", + "id": "32d15813", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "b0baef43", + "id": "9ee9c5cc", "metadata": {}, "source": [ "#### 72. How to swap two rows of an array? (★★★)" @@ -1226,14 +1226,14 @@ { "cell_type": "code", "execution_count": null, - "id": "50cd4d2a", + "id": "8499665c", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "4fa94835", + "id": "2a16d4e2", "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": "6865bea3", + "id": "4f2c6e17", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "7e5e349b", + "id": "75bbcb12", "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": "77566eb3", + "id": "46ebf898", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "41b933ae", + "id": "605b5b91", "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": "615b277b", + "id": "b6f28d8a", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "151a2312", + "id": "0a83c91b", "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": "8c38e25f", + "id": "78b4cfb4", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "da1ab0c7", + "id": "4be00d5e", "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": "558a7ee2", + "id": "5ba94110", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "f0b73c9b", + "id": "23ff5523", "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": "60113555", + "id": "4621c4e1", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "e93b01c0", + "id": "bbba6526", "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": "501e422c", + "id": "a38de46e", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "ba44a416", + "id": "b70a0169", "metadata": {}, "source": [ "#### 80. Consider an arbitrary array, write a function that extract 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": "ce182c62", + "id": "e286aa38", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "a73b0984", + "id": "82577004", "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": "3888023d", + "id": "299febc3", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "c6abb5dc", + "id": "19914b7e", "metadata": {}, "source": [ "#### 82. Compute a matrix rank (★★★)" @@ -1386,14 +1386,14 @@ { "cell_type": "code", "execution_count": null, - "id": "877df10d", + "id": "71b6d05e", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "c935778c", + "id": "5b4702b1", "metadata": {}, "source": [ "#### 83. How to find the most frequent value in an array?" @@ -1402,14 +1402,14 @@ { "cell_type": "code", "execution_count": null, - "id": "ec8edd02", + "id": "47392a08", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "afb79a63", + "id": "e2b4e6d4", "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": "f122d621", + "id": "ecc7884c", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "dd42e7c9", + "id": "588931bf", "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": "a88da7d1", + "id": "16415e97", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "af05a48c", + "id": "3e2ada58", "metadata": {}, "source": [ "#### 86. Consider a set of p matrices wich 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": "99d208ee", + "id": "b830123b", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "a00fe56e", + "id": "dc24b132", "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": "71eb8301", + "id": "2aecf16c", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "88a439b1", + "id": "bc324c05", "metadata": {}, "source": [ "#### 88. How to implement the Game of Life using numpy arrays? (★★★)" @@ -1482,14 +1482,14 @@ { "cell_type": "code", "execution_count": null, - "id": "58d00342", + "id": "fb2f76d6", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "5857f23f", + "id": "84266863", "metadata": {}, "source": [ "#### 89. How to get the n largest values of an array (★★★)" @@ -1498,14 +1498,14 @@ { "cell_type": "code", "execution_count": null, - "id": "2bcc9c8b", + "id": "3b353230", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "f36eb793", + "id": "4f5587b9", "metadata": {}, "source": [ "#### 90. Given an arbitrary number of vectors, build the cartesian product (every combinations of every item) (★★★)" @@ -1514,14 +1514,14 @@ { "cell_type": "code", "execution_count": null, - "id": "6c02e6e6", + "id": "3f752a18", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "e70c8050", + "id": "b4c22657", "metadata": {}, "source": [ "#### 91. How to create a record array from a regular array? (★★★)" @@ -1530,14 +1530,14 @@ { "cell_type": "code", "execution_count": null, - "id": "3d13a2d5", + "id": "a0dc0426", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "2a5aefce", + "id": "bb9f1473", "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": "f905531d", + "id": "0d585a57", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "4218947c", + "id": "3acb5b65", "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": "9df857c8", + "id": "f9e1e127", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "d1a3499b", + "id": "f8f36be4", "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": "457835a5", + "id": "6caa0f63", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "92abcbb3", + "id": "2065f6b5", "metadata": {}, "source": [ "#### 95. Convert a vector of ints into a matrix binary representation (★★★)" @@ -1594,14 +1594,14 @@ { "cell_type": "code", "execution_count": null, - "id": "143ab446", + "id": "8a6afb88", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "533d9ad0", + "id": "15953492", "metadata": {}, "source": [ "#### 96. Given a two dimensional array, how to extract unique rows? (★★★)" @@ -1610,14 +1610,14 @@ { "cell_type": "code", "execution_count": null, - "id": "ce13ac74", + "id": "2fbe8159", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "45b80432", + "id": "95bce4fe", "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": "41cf9dd9", + "id": "07971442", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "c95c5032", + "id": "6d728760", "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": "c7259ce6", + "id": "2a30f221", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "c8512547", + "id": "5b3bbdc0", "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": "e4a0e867", + "id": "eb9bad02", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "6de8ff95", + "id": "6295db88", "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": "f62a28f2", + "id": "94174a6b", "metadata": {}, "outputs": [], "source": [] diff --git a/100_Numpy_exercises_with_hints_with_solutions.md b/100_Numpy_exercises_with_hints_with_solutions.md index 4730a3d..e346bb3 100644 --- a/100_Numpy_exercises_with_hints_with_solutions.md +++ b/100_Numpy_exercises_with_hints_with_solutions.md @@ -354,6 +354,11 @@ print(np.trunc(Z)) `hint: np.arange` ```python +Z = np.zeros((5,5)) +Z += np.arange(5) +print(Z) + +# without broadcasting Z = np.tile(np.arange(0, 5), (5,1)) print(Z) ``` diff --git a/100_Numpy_exercises_with_solutions.md b/100_Numpy_exercises_with_solutions.md index 5b21657..da812a9 100644 --- a/100_Numpy_exercises_with_solutions.md +++ b/100_Numpy_exercises_with_solutions.md @@ -354,6 +354,11 @@ print(np.trunc(Z)) ```python +Z = np.zeros((5,5)) +Z += np.arange(5) +print(Z) + +# without broadcasting Z = np.tile(np.arange(0, 5), (5,1)) print(Z) ``` diff --git a/100_Numpy_random.ipynb b/100_Numpy_random.ipynb index f7dbbc3..41904f8 100644 --- a/100_Numpy_random.ipynb +++ b/100_Numpy_random.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "markdown", - "id": "68fdc73a", + "id": "9187d1fe", "metadata": {}, "source": [ "# 100 numpy exercises\n", @@ -18,7 +18,7 @@ }, { "cell_type": "markdown", - "id": "fb354250", + "id": "7b3fced0", "metadata": {}, "source": [ "File automatically generated. See the documentation to update questions/answers/hints programmatically." @@ -26,7 +26,7 @@ }, { "cell_type": "markdown", - "id": "a6d059d8", + "id": "77575766", "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": "390f283b", + "id": "bcf0dd93", "metadata": {}, "outputs": [], "source": [ @@ -46,7 +46,7 @@ { "cell_type": "code", "execution_count": null, - "id": "ac498ee7", + "id": "f401c090", "metadata": {}, "outputs": [], "source": [ diff --git a/source/exercises100.ktx b/source/exercises100.ktx index 7a856a5..cb8f463 100644 --- a/source/exercises100.ktx +++ b/source/exercises100.ktx @@ -451,6 +451,11 @@ Create a 5x5 matrix with row values ranging from 0 to 4 (★★☆) hint: np.arange < a37 +Z = np.zeros((5,5)) +Z += np.arange(5) +print(Z) + +# without broadcasting Z = np.tile(np.arange(0, 5), (5,1)) print(Z)