{"id":13789357,"url":"https://github.com/RoyiAvital/Julia100Exercises","last_synced_at":"2025-05-12T06:31:40.538Z","repository":{"id":41185323,"uuid":"475554741","full_name":"RoyiAvital/Julia100Exercises","owner":"RoyiAvital","description":"A set of introductory exercises for Julia. Based on [100 NumPy Exercises](https://github.com/rougier/numpy-100).","archived":false,"fork":false,"pushed_at":"2022-12-07T18:01:03.000Z","size":251,"stargazers_count":129,"open_issues_count":0,"forks_count":19,"subscribers_count":9,"default_branch":"main","last_synced_at":"2024-11-18T04:33:05.972Z","etag":null,"topics":["exercise","julia","questions-and-answers","self-learning","self-study"],"latest_commit_sha":null,"homepage":"","language":"Julia","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"unlicense","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/RoyiAvital.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"ko_fi":"royia","liberapay":"Royi"}},"created_at":"2022-03-29T17:43:13.000Z","updated_at":"2024-11-17T19:40:47.000Z","dependencies_parsed_at":"2023-01-23T18:55:11.471Z","dependency_job_id":null,"html_url":"https://github.com/RoyiAvital/Julia100Exercises","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RoyiAvital%2FJulia100Exercises","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RoyiAvital%2FJulia100Exercises/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RoyiAvital%2FJulia100Exercises/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RoyiAvital%2FJulia100Exercises/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/RoyiAvital","download_url":"https://codeload.github.com/RoyiAvital/Julia100Exercises/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253687553,"owners_count":21947694,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["exercise","julia","questions-and-answers","self-learning","self-study"],"created_at":"2024-08-03T22:00:19.609Z","updated_at":"2025-05-12T06:31:40.220Z","avatar_url":"https://github.com/RoyiAvital.png","language":"Julia","funding_links":["https://ko-fi.com/royia","https://liberapay.com/Royi"],"categories":["Uncategorized"],"sub_categories":["Uncategorized"],"readme":"[![Visitors](https://hits.seeyoufarm.com/api/count/incr/badge.svg?url=https%3A%2F%2Fgithub.com%2FRoyiAvital%2FStackExchangeCodes\u0026count_bg=%2379C83D\u0026title_bg=%23555555\u0026icon=\u0026icon_color=%23E7E7E7\u0026title=Visitors+%28Daily+%2F+Total%29\u0026edge_flat=false)](https://github.com/RoyiAvital/Julia100Exercises)\n\n# 100 Julia Exercises with Solutions\n\nA set of introductory exercises for Julia. Based on [100 NumPy Exercises](https://github.com/rougier/numpy-100).\n\nIn order to generate this file:\n1. Clone the repository (Or download).\n2. Activate and instantiate the project.\n3. Run:\n```Julia\nusing Literate;\nLiterate.markdown(\"Julia100Exercises.jl\", name = \"README\", execute = true, flavor = Literate.CommonMarkFlavor());\n```\n\n**Remark**: Tested with Julia `1.8.2`.\n\n**To Do**:\n1. Reevaluate the difficulty level of each question.\n\n````julia\nusing Literate;\nusing LinearAlgebra;\nusing Statistics;\nusing Dates;\nusing DelimitedFiles;\nusing UnicodePlots;\nusing Random;\nusing Tullio;\nusing StaticKernels;\n````\n\n## Question 001\nImport the `LinearAlgebra` package under the name `LA`. (★☆☆)\n\n````julia\nimport LinearAlgebra as LA;\n````\n\n## Question 002\nPrint the version of Julia. (★☆☆)\n\n````julia\nprintln(VERSION);\n````\n\n````\n1.8.2\n\n````\n\n## Question 003\nCreate a non initialized vector of size 10 of `Float64`. (★☆☆)\n\n````julia\nvA = Vector{Float64}(undef, 10)\n````\n\n````\n10-element Vector{Float64}:\n 0.0\n 1.314224183e-315\n 1.314224183e-315\n 0.0\n 1.314248214e-315\n 1.31427351e-315\n 0.0\n 1.314248214e-315\n 1.314106556e-315\n 0.0\n````\n\nWhich is equivalent of\n\n````julia\nvA = Array{Float64, 1}(undef, 10)\n````\n\n````\n10-element Vector{Float64}:\n 0.0\n 1.314248214e-315\n 1.314106556e-315\n 0.0\n 1.314248214e-315\n 1.314106556e-315\n 0.0\n 1.314248214e-315\n 1.314106556e-315\n 0.0\n````\n\n## Question 004\nFind the memory size of any array. (★☆☆)\n\n````julia\nsizeof(vA)\n````\n\n````\n80\n````\n\n## Question 005\nShow the documentation of the `+` (Add) method. (★☆☆)\n\n````julia\n@doc +\n````\n\n```\n+(x, y...)\n```\n\nAddition operator. `x+y+z+...` calls this function with all arguments, i.e. `+(x, y, z, ...)`.\n\n# Examples\n\n```jldoctest\njulia\u003e 1 + 20 + 4\n25\n\njulia\u003e +(1, 20, 4)\n25\n```\n\n```\ndt::Date + t::Time -\u003e DateTime\n```\n\nThe addition of a `Date` with a `Time` produces a `DateTime`. The hour, minute, second, and millisecond parts of the `Time` are used along with the year, month, and day of the `Date` to create the new `DateTime`. Non-zero microseconds or nanoseconds in the `Time` type will result in an `InexactError` being thrown.\n\n\n## Question 006\nCreate a vector of zeros of size 10 but the fifth value which is 1. (★☆☆)\n\n````julia\nvA = zeros(10);\nvA[5] = 1.0;\nvA\n````\n\n````\n10-element Vector{Float64}:\n 0.0\n 0.0\n 0.0\n 0.0\n 1.0\n 0.0\n 0.0\n 0.0\n 0.0\n 0.0\n````\n\n## Question 007\nCreate a vector with values ranging from 7 to 12. (★☆☆)\n\n````julia\nvA = 7:12\n````\n\n````\n7:12\n````\n\nThe above is efficient type. In order to explicitly create a vector:\n\n````julia\nvA = collect(7:12)\n````\n\n````\n6-element Vector{Int64}:\n  7\n  8\n  9\n 10\n 11\n 12\n````\n\n## Question 008\nReverse a vector (first element becomes last). (★☆☆)\n\n````julia\nvA = collect(1:3);\nvB = vA[end:-1:1];\nvB\n````\n\n````\n3-element Vector{Int64}:\n 3\n 2\n 1\n````\n\nAlternative 001:\n\n````julia\nvB = reverse(vA);\n````\n\nAlternative 002 (In place):\n\n````julia\nreverse!(vA);\nvA\n````\n\n````\n3-element Vector{Int64}:\n 3\n 2\n 1\n````\n\n## Question 009\nCreate a `3x3` matrix with values ranging from 0 to 8. (★☆☆)\n\n````julia\nmA = reshape(0:8, 3, 3)\n````\n\n````\n3×3 reshape(::UnitRange{Int64}, 3, 3) with eltype Int64:\n 0  3  6\n 1  4  7\n 2  5  8\n````\n\nAnother way would be:\n\n````julia\nmA = Matrix{Float64}(undef, 3, 3);\nmA[:] = 0:8;\n````\n\n## Question 010\nFind indices of non zero elements from `[1, 2, 0, 0, 4, 0]`. (★☆☆)\n\n````julia\nfindall(!iszero, [1, 2, 0, 0, 4, 0])\n````\n\n````\n3-element Vector{Int64}:\n 1\n 2\n 5\n````\n\n## Question 011\nCreate a 3x3 identity matrix. (★☆☆)\n\n````julia\nmA = I(3)\n````\n\n````\n3×3 LinearAlgebra.Diagonal{Bool, Vector{Bool}}:\n 1  ⋅  ⋅\n ⋅  1  ⋅\n ⋅  ⋅  1\n````\n\nAn alternative method (Explicit matrix) would be:\n\n````julia\nmA = Matrix(I, 3, 3) #\u003c! For Float64: Matrix{Float64}(I, 3, 3)\n````\n\n````\n3×3 Matrix{Bool}:\n 1  0  0\n 0  1  0\n 0  0  1\n````\n\n## Question 012\nCreate a `2x2x2` array with random values. (★☆☆)\n\n````julia\nmA = randn(2, 2, 2)\n````\n\n````\n2×2×2 Array{Float64, 3}:\n[:, :, 1] =\n -1.34961   -0.225955\n  0.116267   1.03802\n\n[:, :, 2] =\n  0.737672  2.2642\n -0.839961  0.218133\n````\n\n## Question 013\nCreate a `5x5` array with random values and find the minimum and maximum values. (★☆☆)\n\n````julia\nmA = rand(5, 5);\nminVal = minimum(mA)\n````\n\n````\n0.02305480092860468\n````\n\n````julia\nmaxVal = maximum(mA)\n````\n\n````\n0.9708814560256962\n````\n\nUsing `extrema()` one could get both values at once:\n\n````julia\nminVal, maxVal = extrema(mA);\n````\n\n## Question 014\nCreate a random vector of size 30 and find the mean value. (★☆☆)\n\n````julia\nmeanVal = mean(randn(30))\n````\n\n````\n-0.04218053798839749\n````\n\n## Question 015\nCreate a 2d array with 1 on the border and 0 inside. (★☆☆)\n\n````julia\nmA = zeros(4, 4);\nmA[:, [1, end]] .= 1;\nmA[[1, end], :] .= 1;\nmA\n````\n\n````\n4×4 Matrix{Float64}:\n 1.0  1.0  1.0  1.0\n 1.0  0.0  0.0  1.0\n 1.0  0.0  0.0  1.0\n 1.0  1.0  1.0  1.0\n````\n\nAn alternative way (Different dimensions):\n\n````julia\nmA = ones(4, 5);\nmA[2:(end - 1), 2:(end - 1)] .= 0;\n````\n\nUsing one line code:\n\n````julia\nmA = zeros(4, 5);\nmA[[LinearIndices(mA)[cartIdx] for cartIdx in CartesianIndices(mA) if (any(cartIdx.I .== 1) || cartIdx.I[1] == size(mA, 1) || cartIdx.I[2] == size(mA, 2))]] .= 1;\n````\n\nBy [Tomer Arnon](https://github.com/tomerarnon):\n\n````julia\nnumRows = 5;\nnumCols = 4;\nmA = Int[ii ∈ (1, numRows) || jj ∈ (1, numCols) for ii in 1:numRows, jj in 1:numCols];\n````\n\n## Question 016\nAdd a border of zeros around the array. (★☆☆)\n\n````julia\nmB = zeros(size(mA) .+ 2);\nmB[2:(end - 1), 2:(end - 1)] = mA;\nmB\n````\n\n````\n7×6 Matrix{Float64}:\n 0.0  0.0  0.0  0.0  0.0  0.0\n 0.0  1.0  1.0  1.0  1.0  0.0\n 0.0  1.0  0.0  0.0  1.0  0.0\n 0.0  1.0  0.0  0.0  1.0  0.0\n 0.0  1.0  0.0  0.0  1.0  0.0\n 0.0  1.0  1.0  1.0  1.0  0.0\n 0.0  0.0  0.0  0.0  0.0  0.0\n````\n\n## Question 017\nEvaluate the following expressions. (★☆☆)\n\n````julia\n0 * NaN\n````\n\n````\nNaN\n````\n\n````julia\nNaN == NaN\n````\n\n````\nfalse\n````\n\n````julia\nInf \u003e NaN\n````\n\n````\nfalse\n````\n\n````julia\nNaN - NaN\n````\n\n````\nNaN\n````\n\n````julia\nNaN in [NaN]\n````\n\n````\nfalse\n````\n\n````julia\n0.3 == 3 * 0.1\n````\n\n````\nfalse\n````\n\n## Question 018\nCreate a `5x5` matrix with values `[1, 2, 3, 4]` just below the diagonal. (★☆☆)\n\n````julia\nmA = diagm(5, 5, -1 =\u003e 1:4)\n````\n\n````\n5×5 Matrix{Int64}:\n 0  0  0  0  0\n 1  0  0  0  0\n 0  2  0  0  0\n 0  0  3  0  0\n 0  0  0  4  0\n````\n\n## Question 019\nCreate a `8x8` matrix and fill it with a checkerboard pattern. (★☆☆)\n\n````julia\nmA = zeros(8, 8);\nmA[2:2:end, 1:2:end] .= 1;\nmA[1:2:end, 2:2:end] .= 1;\nmA\n````\n\n````\n8×8 Matrix{Float64}:\n 0.0  1.0  0.0  1.0  0.0  1.0  0.0  1.0\n 1.0  0.0  1.0  0.0  1.0  0.0  1.0  0.0\n 0.0  1.0  0.0  1.0  0.0  1.0  0.0  1.0\n 1.0  0.0  1.0  0.0  1.0  0.0  1.0  0.0\n 0.0  1.0  0.0  1.0  0.0  1.0  0.0  1.0\n 1.0  0.0  1.0  0.0  1.0  0.0  1.0  0.0\n 0.0  1.0  0.0  1.0  0.0  1.0  0.0  1.0\n 1.0  0.0  1.0  0.0  1.0  0.0  1.0  0.0\n````\n\nBy Tomer Arnon (https://github.com/tomerarnon):\n\n````julia\nmA = Int[isodd(ii + jj) for ii in 1:8, jj in 1:8];\n````\n\n## Question 020\nConvert the linear index 100 to a _Cartesian Index_ of a size `(6,7,8)`. (★☆☆)\n\n````julia\nmA = rand(6, 7, 8);\ncartIdx = CartesianIndices(mA)[100]; #\u003c! See https://discourse.julialang.org/t/14666\nmA[cartIdx] == mA[100]\n````\n\n````\ntrue\n````\n\n## Question 021\nCreate a checkerboard `8x8` matrix using the `repeat()` function. (★☆☆)\n\n````julia\nmA = repeat([0 1; 1 0], 4, 4)\n````\n\n````\n8×8 Matrix{Int64}:\n 0  1  0  1  0  1  0  1\n 1  0  1  0  1  0  1  0\n 0  1  0  1  0  1  0  1\n 1  0  1  0  1  0  1  0\n 0  1  0  1  0  1  0  1\n 1  0  1  0  1  0  1  0\n 0  1  0  1  0  1  0  1\n 1  0  1  0  1  0  1  0\n````\n\n## Question 022\nNormalize a `4x4` random matrix. (★☆☆)\n\n````julia\nmA = rand(4, 4);\nmA .= (mA .- mean(mA)) ./ std(mA) #\u003c! Pay attention that `@.` will yield error (`std()` and `mean()`)\n````\n\n````\n4×4 Matrix{Float64}:\n -1.0589     0.696722    0.474169   0.0807478\n  1.33922    1.23329    -0.559528  -1.24305\n -0.652489   0.919517    1.16875   -1.49035\n -0.0565761  0.0550669   0.711533  -1.61812\n````\n\n## Question 023\nCreate a custom type that describes a color as four unsigned bytes (`RGBA`). (★☆☆)\n\n````julia\nstruct sColor\n    R::UInt8;\n    G::UInt8;\n    B::UInt8;\n    A::UInt8;\nend\n\nsMyColor = sColor(rand(UInt8, 4)...)\n````\n\n````\nMain.var\"##312\".sColor(0xb0, 0xae, 0x35, 0x81)\n````\n\n## Question 024\nMultiply a `2x4` matrix by a `4x3` matrix. (★☆☆)\n\n````julia\nmA = rand(2, 4) * randn(4, 3)\n````\n\n````\n2×3 Matrix{Float64}:\n -1.47402   -1.10919   -1.22996\n -0.745242   0.188422  -3.19746\n````\n\n## Question 025\nGiven a 1D array, negate all elements which are between 3 and 8, in place. (★☆☆)\n\n````julia\nvA = rand(1:10, 8);\nmap!(x -\u003e ((x \u003e 3) \u0026\u0026 (x \u003c 8)) ? -x : x, vA, vA)\n````\n\n````\n8-element Vector{Int64}:\n -6\n  3\n  1\n -4\n  3\n -4\n -5\n -4\n````\n\nJulia allows Math like notation as well (See `Q0027`):\n\n````julia\nvA = rand(1:10, 8);\nmap!(x -\u003e 3 \u003c x \u003c 8 ? -x : x, vA, vA)\n````\n\n````\n8-element Vector{Int64}:\n -7\n -6\n -4\n  1\n -6\n 10\n  8\n -5\n````\n\nUsing logical indices one could use:\n\n````julia\nvA[3 .\u003c vA .\u003c 8] .*= -1;\n````\n\n## Question 026\nSum the array `1:4` with initial value of -10. (★☆☆)\n\n````julia\nsum(1:4, init = -10)\n````\n\n````\n0\n````\n\n## Question 027\nConsider an integer vector `vZ` validate the following expressions. (★☆☆)\n```julia\nvZ .^ vZ\n2 \u003c\u003c vZ \u003e\u003e 2\nvZ \u003c- vZ\n1im * vZ\nvZ / 1 / 1\nvZ \u003c Z \u003e Z\n```\n\n````julia\nvZ = rand(1:10, 3);\n````\n\n````julia\nvZ .^ vZ\n````\n\n````\n3-element Vector{Int64}:\n 387420489\n 387420489\n     46656\n````\n\n````julia\ntry\n    2 \u003c\u003c vZ \u003e\u003e 2\ncatch e\n    println(e)\nend\n````\n\n````\nMethodError(\u003c\u003c, (2, [9, 9, 6]), 0x0000000000007f1f)\n\n````\n\n````julia\nvZ \u003c- vZ\n````\n\n````\nfalse\n````\n\n````julia\n1im * vZ\n````\n\n````\n3-element Vector{Complex{Int64}}:\n 0 + 9im\n 0 + 9im\n 0 + 6im\n````\n\n````julia\nvZ / 1 / 1\n````\n\n````\n3-element Vector{Float64}:\n 9.0\n 9.0\n 6.0\n````\n\n````julia\nvZ \u003c vZ \u003e vZ\n````\n\n````\nfalse\n````\n\n## Question 028\nEvaluate the following expressions. (★☆☆)\n\n````julia\n[0] ./ [0]\n````\n\n````\n1-element Vector{Float64}:\n NaN\n````\n\n````julia\ntry\n    [0] .÷ [0]\ncatch e\n    println(e)\nend\n````\n\n````\nDivideError()\n\n````\n\n````julia\ntry\n    convert(Float, convert(Int, NaN))\ncatch e\n    println(e)\nend\n````\n\n````\nInexactError(:Int64, Int64, NaN)\n\n````\n\n## Question 029\nRound away from zero a float array. (★☆☆)\n\n````julia\nvA = randn(10);\nmap(x -\u003e x \u003e 0 ? ceil(x) : floor(x), vA)\n````\n\n````\n10-element Vector{Float64}:\n  1.0\n -1.0\n -1.0\n -1.0\n  2.0\n -1.0\n  2.0\n  2.0\n  2.0\n  1.0\n````\n\n## Question 030\nFind common values between two arrays. (★☆☆)\n\n````julia\nvA = rand(1:10, 6);\nvB = rand(1:10, 6);\n\nvA[findall(in(vB), vA)]\n````\n\n````\n2-element Vector{Int64}:\n 7\n 5\n````\n\n## Question 031\nSuppress Julia's warnings. (★☆☆)\n\nOne could use [Suppressor.jl](https://github.com/JuliaIO/Suppressor.jl).\n\n## Question 032\nCompare `sqrt(-1)` and `sqrt(-1 + 0im)`. (★☆☆)\n\n````julia\ntry\n    sqrt(-1)\ncatch e\n    println(e)\nend\n````\n\n````\nDomainError(-1.0, \"sqrt will only return a complex result if called with a complex argument. Try sqrt(Complex(x)).\")\n\n````\n\n````julia\nsqrt(-1 + 0im)\n````\n\n````\n0.0 + 1.0im\n````\n\n## Question 033\nDisplay yesterday, today and tomorrow's date. (★☆☆)\n\n````julia\nprintln(\"Yesterday: $(today() - Day(1))\");\nprintln(\"Today: $(today())\");\nprintln(\"Tomorrow: $(today() + Day(1))\");\n````\n\n````\nYesterday: 2022-10-26\nToday: 2022-10-27\nTomorrow: 2022-10-28\n\n````\n\n## Question 034\nDisplay all the dates corresponding to the month of July 2016. (★★☆)\n\n````julia\ncollect(Date(2016,7,1):Day(1):Date(2016,7,31))\n````\n\n````\n31-element Vector{Dates.Date}:\n 2016-07-01\n 2016-07-02\n 2016-07-03\n 2016-07-04\n 2016-07-05\n 2016-07-06\n 2016-07-07\n 2016-07-08\n 2016-07-09\n 2016-07-10\n 2016-07-11\n 2016-07-12\n 2016-07-13\n 2016-07-14\n 2016-07-15\n 2016-07-16\n 2016-07-17\n 2016-07-18\n 2016-07-19\n 2016-07-20\n 2016-07-21\n 2016-07-22\n 2016-07-23\n 2016-07-24\n 2016-07-25\n 2016-07-26\n 2016-07-27\n 2016-07-28\n 2016-07-29\n 2016-07-30\n 2016-07-31\n````\n\n## Question 035\nCompute `((mA + mB) * (-mA / 2))` in place. (★★☆)\n\n````julia\nmA = rand(2, 2);\nmB = rand(2, 2);\nmA .= ((mA .+ mB) .* (.-mA ./ 2))\n````\n\n````\n2×2 Matrix{Float64}:\n -0.35038   -0.685683\n -0.510546  -0.444504\n````\n\nUsing the dot macro:\n\n````julia\n@. mA = ((mA + mB) * (-mA / 2));\n````\n\n## Question 036\nExtract the integer part of a random array of positive numbers using 4 different methods. (★★☆)\n\n````julia\nmA = 5 * rand(3, 3);\n````\n\nOption 1:\n\n````julia\nfloor.(mA)\n````\n\n````\n3×3 Matrix{Float64}:\n 0.0  0.0  4.0\n 2.0  3.0  4.0\n 0.0  3.0  3.0\n````\n\nOption 2:\n\n````julia\nround.(mA .- 0.5) #\u003c! Generates -0.0 for numbers smaller than 0.5\n````\n\n````\n3×3 Matrix{Float64}:\n  0.0  0.0  4.0\n  2.0  3.0  4.0\n -0.0  3.0  3.0\n````\n\nOption 3:\n\n````julia\nmA .÷ 1\n````\n\n````\n3×3 Matrix{Float64}:\n 0.0  0.0  4.0\n 2.0  3.0  4.0\n 0.0  3.0  3.0\n````\n\nOption 4:\n\n````julia\nmA .- rem.(mA, 1)\n````\n\n````\n3×3 Matrix{Float64}:\n 0.0  0.0  4.0\n 2.0  3.0  4.0\n 0.0  3.0  3.0\n````\n\n## Question 037\nCreate a `5x5` matrix with row values ranging from 0 to 4. (★★☆)\n\n````julia\nmA = repeat(reshape(0:4, 1, 5), 5, 1)\n````\n\n````\n5×5 Matrix{Int64}:\n 0  1  2  3  4\n 0  1  2  3  4\n 0  1  2  3  4\n 0  1  2  3  4\n 0  1  2  3  4\n````\n\nOne could also generate _row like_ range using tranpose:\n\n````julia\nmA = repeat((0:4)', 5, 1);\n````\n\n## Question 038\nGenerate an array using a generator of 10 numbers. (★☆☆)\n\n````julia\nvA = collect(x for x in 1:10)\n````\n\n````\n10-element Vector{Int64}:\n  1\n  2\n  3\n  4\n  5\n  6\n  7\n  8\n  9\n 10\n````\n\nIn Julia the result of collect can be achieved directly using _Array Comprehension_:\n\n````julia\nvA = [x for x in 1:10];\n````\n\n## Question 039\nCreate a vector of size 10 with values ranging from 0 to 1, both excluded. (★★☆)\n\n````julia\nvA = LinRange(0, 1, 12)[2:(end - 1)]\n````\n\n````\n10-element LinRange{Float64, Int64}:\n 0.0909091,0.181818,0.272727,0.363636,…,0.636364,0.727273,0.818182,0.909091\n````\n\n## Question 040\nCreate a random vector of size 10 and sort it. (★★☆)\n\n````julia\nvA = rand(1:10, 10);\nsort(vA) #\u003c! Use `sort!()` for inplace sorting\n````\n\n````\n10-element Vector{Int64}:\n  1\n  3\n  3\n  4\n  4\n  4\n  7\n  8\n  9\n 10\n````\n\n## Question 041\nImplement the `sum()` function manually. (★★☆)\n\n````julia\nvA = rand(100);\n\nfunction MySum(vA::Vector{T}) where{T \u003c: Number}\nsumVal = vA[1];\nfor ii in 2:length(vA)\n    sumVal += vA[ii];\nend\n\nreturn sumVal;\n\nend\n\nMySum(vA)\n````\n\n````\n51.34991384741698\n````\n\n## Question 042\nCheck for equality of 2 arrays. (★★☆)\n\n````julia\nvA = rand(10);\nvB = rand(10);\n\nall(vA .== vB)\n````\n\n````\nfalse\n````\n\n## Question 043\nMake an array immutable (Read only). (★★☆)\n\nThis is a work in progress for Julia at in [Issue 31630](https://github.com/JuliaLang/julia/pull/31630).\n\n## Question 044\nConsider a random `10x2` matrix representing cartesian coordinates, convert them to polar coordinates. (★★☆)\n\n````julia\nmA = rand(10, 2);\n\nConvToPolar = vX -\u003e [hypot(vX[1], vX[2]), atan(vX[2], vX[1])]\n\nmB = [ConvToPolar(vX) for vX in eachrow(mA)]\n````\n\n````\n10-element Vector{Vector{Float64}}:\n [0.9505364569557584, 0.031648622825633764]\n [0.704392126760974, 0.5147716023358581]\n [0.29679995764188105, 1.201649642883764]\n [0.637435149299262, 0.02987122275134911]\n [0.933745980010277, 0.7244055260446777]\n [0.9187482092583683, 0.36651502794834323]\n [1.0164226188075605, 0.9581226557842126]\n [0.5946171056720952, 1.4392243895565575]\n [0.7889136757232136, 0.2964469880257975]\n [1.044698619009772, 0.8754548962664869]\n````\n\nIn order to have the same output size:\n\n````julia\nmC = reduce(hcat, mB)';\n````\n\n## Question 045\nCreate random vector of size 10 and replace the maximum value by 0. (★★☆)\n\n````julia\nvA = randn(10);\n````\n\nIn case of a single maximum or all different values:\n\n````julia\nvA[argmax(vA)] = 0;\nvA\n````\n\n````\n10-element Vector{Float64}:\n  0.15593661212531384\n  0.8177229521444311\n -1.4850097210409072\n  0.0\n  0.8552383451908633\n  1.476127811761641\n  0.2407698166125247\n -0.6363509339561092\n -0.5514594178114751\n  0.8756142494364301\n````\n\nGeneral solution:\n\n````julia\nmaxVal = maximum(vA);\nvA .= (valA == maxVal ? 0 : valA for valA in vA); #\u003c! Non allocating generator by using `.=`\n````\n\n## Question 046\nCreate a a grid of `x` and `y` coordinates covering the `[0, 1] x [0, 1]` area. (★★☆)\n\n````julia\nnumGridPts = 5;\nvX = LinRange(0, 1, numGridPts);\nvY = LinRange(0, 1, numGridPts);\nMeshGrid = (vX, vY) -\u003e ([x for _ in vY, x in vX], [y for y in vY, _ in vX]);\n\nmX, mY = MeshGrid(vX, vY); #\u003c! See https://discourse.julialang.org/t/48679\n@show mX\n````\n\n````\n5×5 Matrix{Float64}:\n 0.0  0.25  0.5  0.75  1.0\n 0.0  0.25  0.5  0.75  1.0\n 0.0  0.25  0.5  0.75  1.0\n 0.0  0.25  0.5  0.75  1.0\n 0.0  0.25  0.5  0.75  1.0\n````\n\n````julia\n@show mY\n````\n\n````\n5×5 Matrix{Float64}:\n 0.0   0.0   0.0   0.0   0.0\n 0.25  0.25  0.25  0.25  0.25\n 0.5   0.5   0.5   0.5   0.5\n 0.75  0.75  0.75  0.75  0.75\n 1.0   1.0   1.0   1.0   1.0\n````\n\nBy [Tomer Arnon](https://github.com/tomerarnon):\n\n````julia\nmXY = [(ii, jj) for ii in 0:0.25:1, jj in 0:0.25:1]; #\u003c! Also `tuple.(0:0.25:1, (0:0.25:1)')`\n````\n\n## Question 047\nGiven two vectors, `vX` and `vY`, construct the Cauchy matrix `mC`: `(Cij = 1 / (xi - yj))`. (★★☆)\n\n````julia\nvX = rand(5);\nvY = rand(5);\n\nmC = 1 ./ (vX .- vY')\n````\n\n````\n5×5 Matrix{Float64}:\n 21.646    3.23455  34.1025   3.79522  -3.10044\n 12.1532   2.89647  15.2886   3.33807  -3.49101\n  5.11271  2.18076   5.59545  2.422    -5.77562\n 27.6005   3.34229  51.6618   3.94442  -3.00751\n  2.36374  1.45768   2.46193  1.56164  18.4072\n````\n\n## Question 048\nPrint the minimum and maximum representable value for each Julia scalar type. (★★☆)\n\n````julia\nvT = [UInt8 UInt16 UInt32 UInt64 Int8 Int16 Int32 Int64 Float16 Float32 Float64]\n\nfor juliaType in vT\n    println(typemin(juliaType));\n    println(typemax(juliaType));\nend\n````\n\n````\n0\n255\n0\n65535\n0\n4294967295\n0\n18446744073709551615\n-128\n127\n-32768\n32767\n-2147483648\n2147483647\n-9223372036854775808\n9223372036854775807\n-Inf\nInf\n-Inf\nInf\n-Inf\nInf\n\n````\n\n## Question 049\nPrint all the values of an array. (★★☆)\n\n````julia\nmA = rand(3, 3);\nprint(mA);\n````\n\n````\n[0.6807535316053605 0.849791747148149 0.32690265736495716; 0.9773227682466125 0.5276250185081583 0.9071015474523568; 0.3192975731085147 0.009594822627075117 0.21726819354916904]\n````\n\n## Question 050\nFind the closest value to a given scalar in a vector. (★★☆)\n\n````julia\ninputVal = 0.5;\nvA = rand(10);\n\nvA[argmin(abs.(vA .- inputVal))]\n````\n\n````\n0.5023858297186229\n````\n\nBy [Tomer Arnon](https://github.com/tomerarnon):\n\n````julia\nfunction ClosestValue(vA::Vector{T}, inputVal::T) where{T \u003c: Number}\n    return argmin(y -\u003e abs(y - inputVal), vA);\nend\n\nClosestValue(vA, inputVal)\n````\n\n````\n0.5023858297186229\n````\n\n## Question 051\nCreate a structured array representing a position `(x, y)` and a color `(r, g, b)`. (★★☆)\n\n````julia\nstruct sPosColor\n    x::Int\n    y::Int\n    R::UInt8;\n    G::UInt8;\n    B::UInt8;\n    A::UInt8;\nend\n\nnumPixels   = 10;\nmaxVal      = typemax(UInt32);\nvMyColor    = [sPosColor(rand(1:maxVal, 2)..., rand(UInt8, 4)...) for _ in 1:numPixels];\n````\n\n## Question 052\nConsider a random vector with shape `(5, 2)` representing coordinates, find the distances matrix `mD`: $ {D}_{i, j} = {\\left\\| {x}_{i} - {x}_{j} \\right\\|}_{2} $. (★★☆)\n\n````julia\nmX = rand(5, 2);\nvSumSqr = sum(vX -\u003e vX .^ 2, mX, dims = 2);\nmD = vSumSqr .+ vSumSqr' - 2 * (mX * mX');\nmD #\u003c! Apply `sqrt.()` for the actual norm\n````\n\n````\n5×5 Matrix{Float64}:\n -4.44089e-16  0.135171   0.729876     0.724544   0.525945\n  0.135171     0.0        0.539868     0.255352   0.136451\n  0.729876     0.539868  -1.38778e-17  0.403845   0.814356\n  0.724544     0.255352   0.403845     0.0        0.135001\n  0.525945     0.136451   0.814356     0.135001  -4.44089e-16\n````\n\n## Question 053\nConvert a float (32 bits) array into an integer (32 bits) in place. (★★☆)\n\n````julia\nvA = 9999 .* rand(Float32, 5);\nvB = reinterpret(Int32, vA); #\u003c! Creates a view\n@. vB = trunc(Int32, vA) #\u003c! Updates the byes in th view (Inplace for `vA`)\n````\n\n````\n5-element reinterpret(Int32, ::Vector{Float32}):\n 8898\n 1815\n 6821\n 6523\n 9953\n````\n\nThe above is equivalent of:\n```julia\nfor ii in eachindex(vB)\n    vB[ii] = trunc(Int32, vA[ii]);\nend\nvB\n```\n\n## Question 054\nRead the following file (`Q0054.txt`). (★★☆)\n```\n1, 2, 3, 4, 5\n6,  ,  , 7, 8\n ,  , 9,10,11\n```\n\n````julia\nmA = readdlm(\"Q0054.txt\", ',')\n````\n\n````\n3×5 Matrix{Any}:\n 1     2      3       4   5\n 6      \"  \"   \"  \"   7   8\n  \" \"   \"  \"  9      10  11\n````\n\n## Question 055\nEnumerate array in a loop. (★★☆)\n\n````julia\nmA = rand(3, 3);\n\nfor (elmIdx, elmVal) in enumerate(mA) #\u003c! See https://discourse.julialang.org/t/48877\n    println(elmIdx);\n    println(elmVal);\nend\n````\n\n````\n1\n0.7028850425818378\n2\n0.9169645026836734\n3\n0.887675432103485\n4\n0.6988267487719038\n5\n0.7143993871851885\n6\n0.8303140449034555\n7\n0.43387559304673373\n8\n0.13163225199507878\n9\n0.6590727215475687\n\n````\n\n## Question 056\nGenerate a generic 2D Gaussian like array with `μ = 0`, `σ = 1` and indices over `{-5, -4, ..., 0, 1, ..., 5}`. (★★☆)\n\n````julia\nvA = -5:5;\nμ = 0;\nσ = 1;\nmG = [(1 / (2 * pi * σ)) * exp(-0.5 * ((([x, y] .- μ)' * ([x, y] .- μ)) / (σ * σ))) for x in vA, y in vA];\n\nheatmap(mG)\n````\n\n````\n      ┌───────────┐              \n   11 │▄▄▄▄▄▄▄▄▄▄▄│  ┌──┐ 0.2    \n      │▄▄▄▄▄▄▄▄▄▄▄│  │▄▄│        \n      │▄▄▄▄▄▄▄▄▄▄▄│  │▄▄│        \n      │▄▄▄▄▄▄▄▄▄▄▄│  │▄▄│        \n      │▄▄▄▄▄▄▄▄▄▄▄│  │▄▄│        \n    1 │▄▄▄▄▄▄▄▄▄▄▄│  └──┘ 2.0e-12\n      └───────────┘              \n       1        11               \n````\n\nUsing the separability of the Gaussian function:\n\n````julia\nvG = (1 / (sqrt(2 * pi) * σ)) .* exp.(-0.5 .* (((vA .- μ) .^ 2) / (σ * σ)));\nmG = vG * vG';\n````\n\n## Question 057\nPlace `5` elements in a `5x5` array randomly. (★★☆)\n\n````julia\nmA = rand(5, 5);\nmA[rand(1:25, 5)] = rand(5);\n````\n\nAnother option which avoids setting into the same indices:\n\n````julia\nmA[randperm(25)[1:5]] = rand(5);\n````\n\n## Question 058\nSubtract the mean of each row of a matrix. (★★☆)\n\n````julia\nmA = rand(3, 3);\nmA .-= mean(mA, dims = 2);\nmean(mA, dims = 1)\n````\n\n````\n1×3 Matrix{Float64}:\n 0.261517  -0.0218978  -0.23962\n````\n\n## Question 059\nSort an array by a column. (★★☆)\n\n````julia\ncolIdx = 2;\n\nmA = rand(3, 3);\nmA[sortperm(mA[:, colIdx]), :]\n````\n\n````\n3×3 Matrix{Float64}:\n 0.870197  0.229455  0.462788\n 0.812345  0.61344   0.0990058\n 0.271644  0.722126  0.182457\n````\n\nUsing `sortslices()`:\n\n````julia\nsortslices(mA, dims = 1, by = x -\u003e x[colIdx]);\n````\n\n## Question 060\nTell if a given 2D array has null (All zeros) columns. (★★☆)\n\n````julia\nmA = rand(0:1, 3, 9);\nany(all(iszero.(mA), dims = 1))\n````\n\n````\nfalse\n````\n\n## Question 061\nFind the 2nd nearest value from a given value in an array. (★★☆)\n\n````julia\ninputVal = 0.5;\nvA = rand(10);\n\nvA[sortperm(abs.(vA .- inputVal))[2]]\n````\n\n````\n0.5947029703004421\n````\n\nAlternative way (More efficient)\n\n````julia\ncloseFirst  = Inf;\ncloseSecond = Inf;\ncloseFirstIdx  = 0;\ncloseSecondIdx = 0;\n\n# Using `global` for scope in Literate\nfor (elmIdx, elmVal) in enumerate(abs.(vA .- inputVal))\n    if (elmVal \u003c closeFirst)\n        global closeSecond = closeFirst;\n        global closeFirst = elmVal;\n        global closeSecondIdx  = closeFirstIdx;\n        global closeFirstIdx   = elmIdx;\n    elseif (elmVal \u003c closeSecond)\n        global closeSecond = elmVal;\n        global closeSecondIdx = elmIdx;\n    end\nend\n\nvA[closeSecondIdx] == vA[sortperm(abs.(vA .- inputVal))[2]]\n````\n\n````\ntrue\n````\n\nBy [Tomer Arnon](https://github.com/tomerarnon):\n\n````julia\nvA[partialsortperm(abs.(vA .- inputVal), 2)]\n````\n\n````\n0.5947029703004421\n````\n\n## Question 062\nConsidering two arrays with shape `(1, 3)` and `(3, 1)`, Compute their sum using an iterator. (★★☆)\n\n````julia\nvA = rand(1, 3);\nvB = rand(3, 1);\n\nsum(aVal + bVal for aVal in vA, bVal in vB)\n````\n\n````\n11.149909948305584\n````\n\n## Question 063\nCreate an array class that has a name attribute. (★★☆)\n\nOne could use `NamedArrays.jl` or `AxisArrays.jl`.\n\n## Question 064\nGiven a vector, add `1` to each element indexed by a second vector (Be careful with repeated indices). (★★★)\n\n````julia\nvA = rand(1:10, 5);\nvB = rand(1:5, 3);\n\nprintln(vA);\n\n# Julia is very efficient with loops\nfor bIdx in vB\n    vA[bIdx] += 1;\nend\n\nprintln(vA);\n````\n\n````\n[1, 6, 2, 6, 2]\n[3, 7, 2, 6, 2]\n\n````\n\n## Question 065\nAccumulate elements of a vector `X` to an array `F` based on an index list `I`. (★★★)\n\n````julia\nvX = rand(1:5, 10);\nvI = rand(1:15, 10);\n\nnumElements = maximum(vI);\nvF = zeros(numElements);\n\nfor (ii, iIdx) in enumerate(vI)\n    vF[iIdx] += vX[ii];\nend\n\nprintln(\"vX: $vX\");\nprintln(\"vI: $vI\");\nprintln(\"vF: $vF\");\n````\n\n````\nvX: [5, 4, 3, 5, 2, 5, 2, 5, 5, 1]\nvI: [3, 2, 6, 11, 12, 10, 5, 11, 12, 2]\nvF: [0.0, 5.0, 5.0, 0.0, 2.0, 3.0, 0.0, 0.0, 0.0, 5.0, 10.0, 7.0]\n\n````\n\nOne could also use `counts()` from `StatsBase.jl`.\n\n## Question 066\nConsidering an image of size `w x h x 3` image of type `UInt8`, compute the number of unique colors. (★★☆)\n\n````julia\nmI = rand(UInt8, 1000, 1000, 3);\n\nnumColors = length(unique([reinterpret(UInt32, [iPx[1], iPx[2], iPx[3], 0x00])[1] for iPx in eachrow(reshape(mI, :, 3))])); #\u003c! Reshaping as at the moment `eachslice()` doesn't support multiple `dims`.\nprint(\"Number of Unique Colors: $numColors\");\n````\n\n````\nNumber of Unique Colors: 970711\n````\n\nAnother option:\n\n````julia\nnumColors = length(unique([UInt32(iPx[1]) + UInt32(iPx[2]) \u003c\u003c 8 + UInt32(iPx[3]) \u003c\u003c 16 for iPx in eachrow(reshape(mI, :, 3))]));\nprint(\"Number of Unique Colors: $numColors\");\n````\n\n````\nNumber of Unique Colors: 970711\n````\n\nSimpler way to slice a pixel:\n\n````julia\nnumColors = length(unique([UInt32(mI[ii, jj, 1]) + UInt32(mI[ii, jj, 2]) \u003c\u003c 8 + UInt32(mI[ii, jj, 3]) \u003c\u003c 16 for ii in 1:size(mI, 1), jj in 1:size(mI, 2)]));\nprint(\"Number of Unique Colors: $numColors\");\n````\n\n````\nNumber of Unique Colors: 970711\n````\n\n## Question 067\nConsidering a four dimensions array, get sum over the last two axis at once. (★★★)\n\n````julia\nmA = rand(2, 2, 2, 2);\nsum(reshape(mA, (2, 2, :)), dims = 3)\n````\n\n````\n2×2×1 Array{Float64, 3}:\n[:, :, 1] =\n 1.817    1.16234\n 2.44291  2.32796\n````\n\n## Question 068\nConsidering a one dimensional vector `vA`, how to compute means of subsets of `vA` using a vector `vS` of same size describing subset indices. (★★★)\n\n````julia\n# Bascially extending `Q0065` with another vector of number of additions.\n\nvX = rand(1:5, 10);\nvI = rand(1:15, 10);\n\nnumElements = maximum(vI);\nvF = zeros(numElements);\nvN = zeros(Int, numElements);\n\nfor (ii, iIdx) in enumerate(vI)\n    vF[iIdx] += vX[ii];\n    vN[iIdx] += 1;\nend\n\n# We only divide the mean if the number of elements accumulated is bigger than 1\nfor ii in 1:numElements\n    vF[ii] = ifelse(vN[ii] \u003e 1, vF[ii] / vN[ii], vF[ii]);\nend\n\nprintln(\"vX: $vX\");\nprintln(\"vI: $vI\");\nprintln(\"vF: $vF\");\n````\n\n````\nvX: [5, 5, 4, 2, 1, 1, 3, 5, 3, 2]\nvI: [14, 7, 8, 10, 5, 10, 14, 2, 7, 13]\nvF: [0.0, 5.0, 0.0, 0.0, 1.0, 0.0, 4.0, 4.0, 0.0, 1.5, 0.0, 0.0, 2.0, 4.0]\n\n````\n\n## Question 069\nGet the diagonal of a matrix product. (★★★)\n\n````julia\nmA = rand(5, 7);\nmB = rand(7, 4);\n\nnumDiagElements = min(size(mA, 1), size(mB, 2));\nvD = [dot(mA[ii, :], mB[:, ii]) for ii in 1:numDiagElements]\n````\n\n````\n4-element Vector{Float64}:\n 1.8937792321469207\n 1.035584608236753\n 2.0251852803210024\n 2.2065505485118653\n````\n\nAlternative way:\n\n````julia\nvD = reshape(sum(mA[1:numDiagElements, :]' .* mB[:, 1:numDiagElements], dims = 1), numDiagElements)\n````\n\n````\n4-element Vector{Float64}:\n 1.8937792321469207\n 1.035584608236753\n 2.0251852803210024\n 2.2065505485118653\n````\n\n## Question 070\nConsider the vector `[1, 2, 3, 4, 5]`, build a new vector with 3 consecutive zeros interleaved between each value. (★★★)\n\n````julia\nvA = 1:5;\n\n# Since Julia is fast with loops, it would be the easiest choice\n\nnumElements = (4 * length(vA)) - 3;\nvB = zeros(Int, numElements);\n\nfor (ii, bIdx) in enumerate(1:4:numElements)\n    vB[bIdx] = vA[ii];\nend\nprintln(vB);\n\n# Alternative (MATLAB style) way:\n\nmB = [reshape(collect(vA), 1, :); zeros(Int, 3, length(vA))];\nvB = reshape(mB[1:(end - 3)], :);\nprintln(vB);\n````\n\n````\n[1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0, 5]\n[1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0, 5]\n\n````\n\n## Question 071\nConsider an array of dimension `5 x 5 x 3`, mulitply it by an array with dimensions `5 x 5` using broadcasting. (★★★)\n\n````julia\nmA = rand(5, 5, 3);\nmB = rand(5, 5);\n\nmA .* mB #\u003c! Very easy in Julia\n````\n\n````\n5×5×3 Array{Float64, 3}:\n[:, :, 1] =\n 0.492335  0.301617  0.0158155   0.411494  0.0331824\n 0.555254  0.148368  0.111992    0.366332  0.192798\n 0.104442  0.249306  0.0138448   0.018588  0.721434\n 0.665075  0.136419  0.00305929  0.430373  0.208216\n 0.290075  0.642668  0.519127    0.511527  0.151719\n\n[:, :, 2] =\n 0.234647   0.447608  0.000903852  0.00299349  0.0626502\n 0.0159529  0.164814  0.0807125    0.27415     0.180858\n 0.103613   0.291711  0.00759939   0.013771    0.11713\n 0.314552   0.210101  0.00966737   0.343302    0.377711\n 0.129541   0.642804  0.833245     0.372239    0.816422\n\n[:, :, 3] =\n 0.774094  0.181579   0.0151706   0.26963    0.0364216\n 0.167574  0.0016081  0.0130672   0.823491   0.398857\n 0.062678  0.40945    0.00790564  0.0141378  0.468662\n 0.344903  0.18419    0.00697708  0.487335   0.504131\n 0.118821  0.0130137  0.803091    0.754898   0.0470187\n````\n\n## Question 072\nSwap two rows of a 2D array. (★★★)\n\n````julia\nmA = rand(UInt8, 3, 2);\nprintln(mA);\nmA[[1, 2], :] .= mA[[2, 1], :];\nprintln(mA);\n````\n\n````\nUInt8[0xc7 0x2f; 0xbf 0x8f; 0xac 0x2a]\nUInt8[0xbf 0x8f; 0xc7 0x2f; 0xac 0x2a]\n\n````\n\n## Question 073\nConsider a set of 10 triplets describing 10 triangles (with shared vertices), find the set of unique line segments composing all the triangles. (★★★)\n\n````julia\nmA = rand(0:100, 10, 3); #\u003c! Each row composes 3 veritces ([1] -\u003e [2], [2] -\u003e [3], [3] -\u003e [1])\nmC = [sort!([vC[mod1(ii, end)], vC[mod1(ii + 1, end)]]) for ii in 1:(size(mA, 2) + 1), vC in eachrow(mA)][:] #\u003c! Sorted combinations of vertices\nmC = unique(mC)\n````\n\n````\n30-element Vector{Vector{Int64}}:\n [52, 86]\n [23, 86]\n [23, 52]\n [53, 88]\n [53, 95]\n [88, 95]\n [4, 31]\n [4, 24]\n [24, 31]\n [34, 80]\n [34, 99]\n [80, 99]\n [19, 69]\n [19, 21]\n [21, 69]\n [38, 74]\n [36, 74]\n [36, 38]\n [18, 20]\n [20, 48]\n [18, 48]\n [0, 23]\n [23, 77]\n [0, 77]\n [23, 42]\n [42, 75]\n [23, 75]\n [20, 69]\n [7, 69]\n [7, 20]\n````\n\n## Question 074\nGiven a sorted array `vC` that corresponds to a bincount, produce an array `vA` such that `bincount(vA) == vC`. (★★★)\n\n````julia\nvC = rand(0:7, 5);\nnumElements = sum(vC);\nvA = zeros(Int, numElements);\n\nelmIdx = 1;\n# Using `global` for scope in Literate\nfor (ii, binCount) in enumerate(vC)\n    for jj in 1:binCount\n        vA[elmIdx] = ii;\n        global elmIdx += 1;\n    end\nend\n````\n\n## Question 075\nCompute averages using a sliding window over an array. (★★★)\n\n````julia\nnumElements = 10;\nwinRadius   = 1;\nwinReach    = 2 * winRadius;\nwinLength   = 1 + winReach;\n\nvA = rand(0:3, numElements);\nvB = zeros(numElements - (2 * winRadius));\n\naIdx = 1 + winRadius;\n# Using `global` for scope in Literate\nfor ii in 1:length(vB)\n    vB[ii] = mean(vA[(aIdx - winRadius):(aIdx + winRadius)]); #\u003c! Using integral / running sum it would be faster.\n    global aIdx += 1;\nend\n````\n\nAnother method using running sum:\n\n````julia\nvC = zeros(numElements - winReach);\n\njj = 1;\nsumVal = sum(vA[1:winLength]);\nvC[jj] = sumVal / winLength;\njj += 1;\n\n# Using `global` for scope in Literate\nfor ii in 2:(numElements - winReach)\n    global sumVal += vA[ii + winReach] - vA[ii - 1];\n    vC[jj] = sumVal / winLength;\n    global jj += 1;\nend\n\nmaximum(abs.(vC - vB)) \u003c 1e-8\n````\n\n````\ntrue\n````\n\n## Question 076\nConsider a one dimensional array `vA`, build a two dimensional array whose first row is `[ vA[0], vA[1], vA[2] ]`  and each subsequent row is shifted by 1. (★★★)\n\n````julia\nvA = rand(10);\nnumCols = 3;\n\nnumRows = length(vA) - numCols + 1;\nmA = zeros(numRows, numCols);\n\nfor ii in 1:numRows\n    mA[ii, :] = vA[ii:(ii + numCols - 1)]; #\u003c! One could optimize the `-1` out\nend\n````\n\n## Question 077\nNegate a boolean or to change the sign of a float inplace. (★★★)\n\n````julia\nvA = rand(Bool, 10);\nvA .= .!vA;\n\nvA = randn(10);\nvA .*= -1;\n````\n\n## Question 078\nConsider 2 sets of points `mP1`, `mP2` describing lines (2d) and a point `vP`, how to compute distance from the point `vP` to each line `i`: `[mP1[i, :], mP2[i, :]`. (★★★)\n\n````julia\n# See distance of a point from a line in Wikipedia (https://en.wikipedia.org/wiki/Distance_from_a_point_to_a_line).\n# Specifically _Line Defined by Two Points_.\n\nnumLines = 10;\nmP1 = randn(numLines, 2);\nmP2 = randn(numLines, 2);\nvP  = randn(2);\n\nvD = [(abs(((vP2[1] - vP1[1]) * (vP1[2] - vP[2])) - ((vP1[1] - vP[1]) * (vP2[2] - vP1[2]))) / hypot((vP2 - vP1)...)) for (vP1, vP2) in zip(eachrow(mP1), eachrow(mP2))];\nminDist = minimum(vD);\n\nprintln(\"Min Distance: $minDist\");\n````\n\n````\nMin Distance: 0.42811311783896533\n\n````\n\n## Question 079\nConsider 2 sets of points `mP1`, `mP2` describing lines (2d) and a set of points `mP`, how to compute distance from the point `vP = mP[j, :]` to each line `i`: `[mP1[i, :], mP2[i, :]`. (★★★)\n\n````julia\nnumLines = 5;\nmP1 = randn(numLines, 2);\nmP2 = randn(numLines, 2);\nmP  = randn(numLines, 2);\n\nmD = [(abs(((vP2[1] - vP1[1]) * (vP1[2] - vP[2])) - ((vP1[1] - vP[1]) * (vP2[2] - vP1[2]))) / hypot((vP2 - vP1)...)) for (vP1, vP2) in zip(eachrow(mP1), eachrow(mP2)), vP in eachrow(mP)];\n\nfor jj in 1:numLines\n    minDist = minimum(mD[jj, :]);\n    println(\"The minimum distance from the $jj -th point: $minDist\");\nend\n````\n\n````\n┌ Warning: Assignment to `minDist` in soft scope is ambiguous because a global variable by the same name exists: `minDist` will be treated as a new local. Disambiguate by using `local minDist` to suppress this warning or `global minDist` to assign to the existing global variable.\n└ @ string:9\nThe minimum distance from the 1 -th point: 0.33333028326949365\nThe minimum distance from the 2 -th point: 0.09596286876613971\nThe minimum distance from the 3 -th point: 0.15987404532559377\nThe minimum distance from the 4 -th point: 0.09922388894592074\nThe minimum distance from the 5 -th point: 0.0701461047753776\n\n````\n\n## Question 080\nConsider an arbitrary 2D array, write a function that extract a subpart with a fixed shape and centered on a given element (Handel out of bounds). (★★★)\n\n````julia\n# One could use `PaddedViews.jl` to easily solve this.\n\narrayLength = 10;\nwinRadius   = 3;\nvWinCenter  = [7, 9];\n\nmA = rand(arrayLength, arrayLength);\nwinLength = (2 * winRadius) + 1;\nmB = zeros(winLength, winLength);\n\nverShift = -winRadius;\n# Using `global` for scope in Literate\nfor ii in 1:winLength\n    horShift = -winRadius;\n    for jj in 1:winLength\n        mB[ii, jj] = mA[min(max(vWinCenter[1] + verShift, 1), arrayLength), min(max(vWinCenter[2] + horShift, 1), arrayLength)]; #\u003c! Nearest neighbor extrapolation\n        horShift += 1;\n    end\n    global verShift += 1;\nend\n````\n\n## Question 081\nConsider an array `vA = [1, 2, 3, ..., 13, 14]`, generate an array `vB = [[1, 2, 3, 4], [2, 3, 4, 5], [3, 4, 5, 6], ..., [11, 12, 13, 14]]`. (★★★)\n\n````julia\nvA = collect(1:14);\n\nwinNumElements  = 4;\nwinReach        = winNumElements - 1;\n\nvB = [vA[ii:(ii + winReach)] for ii in 1:(length(vA) - winReach)]\n````\n\n````\n11-element Vector{Vector{Int64}}:\n [1, 2, 3, 4]\n [2, 3, 4, 5]\n [3, 4, 5, 6]\n [4, 5, 6, 7]\n [5, 6, 7, 8]\n [6, 7, 8, 9]\n [7, 8, 9, 10]\n [8, 9, 10, 11]\n [9, 10, 11, 12]\n [10, 11, 12, 13]\n [11, 12, 13, 14]\n````\n\n## Question 082\nCompute a matrix rank. (★★★)\n\n````julia\nnumRows = 5;\nnumCols = 4;\nmA = randn(numRows, numCols);\nrank(mA)\n````\n\n````\n4\n````\n\n## Question 083\nFind the most frequent value in an array. (★★★)\n\n````julia\nvA = rand(1:5, 15);\n````\n\nMATLAB Style (Manual loop might be faster)\n\n````julia\nvB = unique(vA);\n# vB[argmax(sum(vA .== vB', dims = 1)[:])] #\u003c! The input to `argmax()` is a `1 x n` vector, hence squeezed so `argmax()` won't return Cartesian Index.\nvB[argmax(dropdims(sum(vA .== vB', dims = 1), dims = 1))] #\u003c! The input to `argmax()` is a `1 x n` vector, hence squeezed so `argmax()` won't return Cartesian Index.\n````\n\n````\n1\n````\n\nComparing bits:\n\nOne could convert at the bits level to integers and then use something like `counts()` from `StatsBase.jl`.\nSupport to 1:4 bytes of data:\n```julia\nnumBytes = sizeof(vA[1]);\nif (sizeof(vA[1]) == 1)\n    vB = reinterpret(UInt8, vA);\nelseif (sizeof(vA[1]) == 2)\n    vB = reinterpret(UInt16, vA);\nelseif (sizeof(vA[1]) == 4)\n    vB = reinterpret(UInt32, vA);\nelseif (sizeof(vA[1]) == 8)\n    vB = reinterpret(UInt64, vA);\nend\n```\n\n## Question 084\nExtract all the contiguous `3x3` blocks from a random `5x5` matrix. (★★★)\n\n````julia\nnumRows = 5;\nnumCols = 5;\n\nmA = rand(1:9, numRows, numCols);\n\nwinRadius   = 1;\nwinReach    = 2 * winRadius;\nwinLength   = winReach + 1;\n\nmB = [mA[ii:(ii + winReach), jj:(jj + winReach)] for ii in 1:(numRows - winReach), jj in 1:(numCols - winReach)]\n````\n\n````\n3×3 Matrix{Matrix{Int64}}:\n [9 1 1; 3 1 2; 9 4 6]  [1 1 4; 1 2 2; 4 6 2]  [1 4 7; 2 2 1; 6 2 2]\n [3 1 2; 9 4 6; 4 2 1]  [1 2 2; 4 6 2; 2 1 7]  [2 2 1; 6 2 2; 1 7 1]\n [9 4 6; 4 2 1; 2 9 8]  [4 6 2; 2 1 7; 9 8 1]  [6 2 2; 1 7 1; 8 1 4]\n````\n\n## Question 085\nCreate a 2D array struct such that `mA[i, j] == mA[j, i]` (Symmetric matrix). (★★★)\n\n````julia\nstruct SymmetricMatrix{T \u003c: Number} \u003c: AbstractArray{T, 2}\n    numRows::Int\n    data::Matrix{T}\n\n    function SymmetricMatrix(mA::Matrix{T}) where {T \u003c: Number}\n        size(mA, 1) == size(mA, 2) || throw(ArgumentError(\"Input matrix must be square\"))\n        new{T}(size(mA, 1), Matrix(Symmetric(mA)));\n    end\n\nend\n\nfunction Base.size(mA::SymmetricMatrix)\n    (mA.numRows, mA.numRows);\nend\nfunction Base.getindex(mA::SymmetricMatrix, ii::Int)\n    mA.data[ii];\nend\nfunction Base.getindex(mA::SymmetricMatrix, ii::Int, jj::Int)\n    mA.data[ii, jj];\nend\nfunction Base.setindex!(mA::SymmetricMatrix, v, ii::Int, jj::Int)\n    setindex!(mA.data, v, ii, jj);\n    setindex!(mA.data, v, jj, ii);\nend\n\nmA = SymmetricMatrix(zeros(Int, 2, 2));\nmA[1, 2] = 5;\nmA\n````\n\n````\n2×2 Main.var\"##312\".SymmetricMatrix{Int64}:\n 0  5\n 5  0\n````\n\n## Question 086\nConsider a set of `p` matrices of shape `nxn` and a set of `p` vectors with length `n`. Compute the sum of of the `p` matrix vector products at once (Result is a vector of length `n`). (★★★)\n\n````julia\n# One could use `TensorOperations.jl` or `Einsum.jl` for a more elegant solution.\n\nnumRows = 5;\nnumMat  = 3;\n\ntP = [randn(numRows, numRows) for _ in 1:numMat];\nmP = [randn(numRows) for _ in 1:numMat];\n\nvA = reduce(+, (mP * vP for (mP, vP) in zip(tP, mP)));\n````\n\n````julia\nvB = zeros(numRows);\nfor ii in 1:numMat\n    vB .+= tP[ii] * mP[ii];\nend\n\nvA == vB\n````\n\n````\ntrue\n````\n\n## Question 087\nConsider a `16x16` array, calculate the block sum (Block size is `4x4`). (★★★)\n\nWe solve a more general case for any size of blocks.\n\n````julia\nnumRows = 16;\nnumCols = 8;\n\nvBlockSize = [2, 4]; #\u003c! [numRows, numCols] ./ vBlockSize == integer\n\nmA = rand(numRows, numCols);\n\nnumBlocksVert   = numRows ÷ vBlockSize[1];\nnumBlocksHori   = numCols ÷ vBlockSize[2];\nnumBlocks       = numBlocksVert * numBlocksHori;\n\nmA = reshape(mA, vBlockSize[1], :);\n````\n\nWe number the blocks column wise and create their block index per column of the reshaped `mA`.\n\n````julia\nvBlockIdx = 1:numBlocks;\nmBlockIdx = reshape(vBlockIdx, numBlocksVert, numBlocksHori);\nmBlockIdx = repeat(mBlockIdx, 1, 1, vBlockSize[2]);\nmBlockIdx = permutedims(mBlockIdx, (1, 3, 2));\nvBlockIdx = mBlockIdx[:]; #\u003c! Matches the block index per column of the reshaped `mA`.\n\nvA = dropdims(sum(mA, dims = 1), dims = 1);\nvB = zeros(numBlocks);\n\nfor ii = 1:(numBlocks * vBlockSize[2])\n    vB[vBlockIdx[ii]] += vA[ii];\nend\nvB\n````\n\n````\n16-element Vector{Float64}:\n 4.354231620225193\n 3.734856822720345\n 3.297606250613403\n 4.256409222766379\n 3.2941384563138993\n 3.776165923653995\n 3.600353929303568\n 4.518180088610547\n 3.6679777425586186\n 4.6420262282688345\n 4.296040312870272\n 5.1547123230902825\n 3.0214360614973477\n 3.202315920090961\n 4.669003591708878\n 3.3257368418156616\n````\n\n## Question 088\nImplement the simulation _Game of Life_ using arrays. (★★★)\n\n````julia\nnumRows = 20;\nnumCols = 20;\n\ngofKernel           = @kernel w -\u003e w[-1, -1] + w[-1, 0] + w[-1, 1] + w[0, -1] + w[0, 1] + w[1, -1] + w[1, 0] + w[1, 1];\ngofNumLives         = round(Int, 0.05 * numRows * numCols);\ngofNumGenerations   = 50;\n\nvI = randperm(numRows * numCols)[1:gofNumLives];\n\nmG = zeros(UInt8, numRows, numCols);\nmG[vI] .= UInt8(1);\nmB = similar(mG);\n\nheatmap(mG) #\u003c! Initialization\n````\n\n````\n      ┌────────────────────┐        \n   20 │▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄│  ┌──┐ 1\n      │▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄│  │▄▄│  \n      │▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄│  │▄▄│  \n      │▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄│  │▄▄│  \n      │▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄│  │▄▄│  \n      │▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄│  │▄▄│  \n      │▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄│  │▄▄│  \n      │▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄│  │▄▄│  \n      │▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄│  │▄▄│  \n    1 │▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄│  └──┘ 0\n      └────────────────────┘        \n       1                 20         \n````\n\n````julia\nfor ii in 1:numGridPts\n    map!(gofKernel, mB, extend(mG, StaticKernels.ExtensionConstant(0))); #\u003c! One may use `ExtensionCircular`\n    for ii in eachindex(mB)\n        mG[ii] = UInt8((mB[ii] \u003e= 3) || ((mB[ii] \u003e 0) \u0026\u0026 (mB[ii] == 2))); #\u003c! Could be done with broadcasting\n    end\nend\n\nheatmap(mG) #\u003c! Final state\n````\n\n````\n      ┌────────────────────┐        \n   20 │▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄│  ┌──┐ 1\n      │▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄│  │▄▄│  \n      │▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄│  │▄▄│  \n      │▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄│  │▄▄│  \n      │▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄│  │▄▄│  \n      │▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄│  │▄▄│  \n      │▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄│  │▄▄│  \n      │▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄│  │▄▄│  \n      │▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄│  │▄▄│  \n    1 │▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄│  └──┘ 0\n      └────────────────────┘        \n       1                 20         \n````\n\nTODO: Use O(1) implementation for Box Blur.\n\n## Question 089\nGet the `n` largest values of an array. (★★★)\n\n````julia\nvA = rand(10);\nnumValues = 3;\n\nvA[partialsortperm(vA, 1:numValues, rev = true)]\n````\n\n````\n3-element Vector{Float64}:\n 0.8713271882275592\n 0.8486847944348448\n 0.8445471427655805\n````\n\n## Question 090\nGiven an arbitrary number of vectors, build the _Cartesian Product_ (Every combinations of every item). (★★★)\n\n````julia\nfunction CartesianProduct(tupleX)\n    return collect(Iterators.product(tupleX...))[:];\nend\n\nvA = 1:3;\nvB = 8:9;\nvC = 4:5;\n\nCartesianProduct((vA, vB, vC))\n````\n\n````\n12-element Vector{Tuple{Int64, Int64, Int64}}:\n (1, 8, 4)\n (2, 8, 4)\n (3, 8, 4)\n (1, 9, 4)\n (2, 9, 4)\n (3, 9, 4)\n (1, 8, 5)\n (2, 8, 5)\n (3, 8, 5)\n (1, 9, 5)\n (2, 9, 5)\n (3, 9, 5)\n````\n\n## Question 091\nCreate an array which can be accessed like a _record array_ in _NumPy_. (★★★)\n\nOne could use `StructArrays.jl`.\n\n## Question 092\nConsider a large vector `vA`, compute `vA` to the power of 3 using 3 different methods. (★★★)\n\n````julia\nvA = rand(1000);\n````\n\nMethod 001:\n\n````julia\nvB = vA .^ 3;\n````\n\nMethod 002:\n\n````julia\nvC = [valA ^ 3 for valA in vA];\n````\n\nMethod 003:\n\n````julia\nvD = zeros(length(vA));\nfor (ii, valA) in enumerate(vA)\n    vD[ii] = valA * valA * valA;\nend\n````\n\n````julia\nvB ≈ vC ≈ vD\n````\n\n````\ntrue\n````\n\n## Question 093\nConsider two arrays `mA` and `mB` of shape `8x3` and `2x2`. Find rows of `mA` that contain elements of each row of `mB` regardless of the order of the elements in `mB`. (★★★)\n\nThe way I interpret the question is rows in `mA` which contain at least 1 element from each row of `mB`.\n\n````julia\nmA = rand(0:4, 8, 3);\nmB = rand(0:4, 2, 2);\nmC = [any(vA .== vB') for vB in eachrow(mB), vA in eachrow(mA)]; #\u003c! General solution, will work for any size of `mA` and `mB`\nvD = [all(vC) for vC in eachcol(mC)]\n````\n\n````\n8-element Vector{Bool}:\n 0\n 1\n 1\n 1\n 0\n 0\n 1\n 1\n````\n\nIn order to have a solution without the intermediate array `mC`\n\n````julia\nfunction Iterate2(iA; iterState = missing)\n    if(ismissing(iterState))\n        valA, iterState = iterate(iA);\n    else\n        valA, iterState = iterate(iA, iterState);\n    end\n    valB, iterState = iterate(iA, iterState);\n    return (valA, valB), iterState\nend\n\ntT = (any(vA .== vB') for vB in eachrow(mB), vA in eachrow(mA));\n\niterState = missing;\n\nvE = zeros(Bool, size(mA, 1));\n\nfor ii = 1:length(vD)\n    global iterState;\n    (valA, valB), iterState = Iterate2(tT; iterState = iterState);\n    vE[ii] = valA \u0026\u0026 valB;\nend\n\nvD == vE\n````\n\n````\ntrue\n````\n\n## Question 094\nConsidering a `10x3` matrix, extract rows with unequal values. (★★★)\n\n````julia\nmA = rand(1:3, 10, 3);\nvD = [maximum(vA) != minimum(vA) for vA in eachrow(mA)]\n````\n\n````\n10-element Vector{Bool}:\n 1\n 0\n 1\n 1\n 1\n 1\n 1\n 1\n 1\n 1\n````\n\n## Question 095\nConvert a vector of ints into a matrix binary representation. (★★★)\n\n````julia\nvA = rand(UInt8, 10);\nmB = zeros(Bool, length(vA), 8);\n\n# See https://discourse.julialang.org/t/26663\nfor ii in 1:length(vA)\n    vS = bitstring(vA[ii]);\n    for jj in 1:size(mB, 2)\n        mB[ii, jj] = vS[jj] == '1';\n    end\nend\n\nmB\n````\n\n````\n10×8 Matrix{Bool}:\n 0  0  1  0  1  1  1  1\n 1  1  0  1  1  1  0  0\n 0  0  0  0  1  0  1  0\n 1  1  1  0  0  1  0  1\n 0  1  0  1  0  1  0  1\n 0  0  0  1  0  1  1  1\n 1  0  0  0  1  0  1  0\n 1  1  0  1  1  1  0  0\n 0  0  1  1  1  1  0  0\n 1  0  0  0  0  1  0  0\n````\n\nBy [Tomer Arnon](https://github.com/tomerarnon):\n\n````julia\nmB = reverse!(reduce(hcat, digits.(vA, base = 2, pad = 8))', dims = 2);\n````\n\n## Question 096\nGiven a two dimensional array, extract unique rows. (★★★)\n\n````julia\nmA = UInt8.(rand(1:3, 10, 3));\n\nvS = [reduce(*, bitstring(valA) for valA in vA) for vA in eachrow(mA)]; #\u003c! Supports any array!\nvU = unique(vS);\nvI = [findfirst(valU .== vS) for valU in vU];\n````\n\nAn alternative way:\n\n````julia\nvB = indexin(vU, vS);\nvB == vI\n````\n\n````\ntrue\n````\n\n## Question 097\nConsidering 2 vectors `vA` and `vB`, write the einsum equivalent (Using `Einsum.jl`) of inner, outer, sum, and mul function. (★★★)\n\n````julia\nvA = rand(5);\nvB = rand(5);\n````\n\nInner Product\n\n````julia\n@tullio tullioVal = vA[ii] * vB[ii];\ntullioVal ≈ dot(vA, vB) #\u003c! Inner product\n````\n\n````\ntrue\n````\n\nOuter  Product\n\n````julia\n@tullio mTullio[ii, jj] := vA[ii] * vB[jj]; #\u003c! Outer product\nmTullio ≈ vA * vB'\n````\n\n````\ntrue\n````\n\nSum\n\n````julia\n@tullio tullioVal = vA[ii];\ntullioVal ≈ sum(vA) #\u003c! Sum\n````\n\n````\ntrue\n````\n\nMultiplication\n\n````julia\n@tullio vTullio[ii] := vA[ii] * vB[ii];\nvTullio ≈ vA .* vB #\u003c! Multiplication\n````\n\n````\ntrue\n````\n\n## Question 098\nConsidering a path described by two vectors `vX` and `vY`, sample it using equidistant samples. (★★★)\n\nThe way I interpreted the question is to create sub segments of the same length.\n\n````julia\nnumPts      = 100;\nnumSegments = 1000;\n\nvX = sort(10 * rand(numPts));\nvY = sort(10 * rand(numPts));\n\nvR = cumsum(hypot.(diff(vX), diff(vY)));\npushfirst!(vR, 0.0);\nvRSegment = LinRange(0.0, vR[end], numSegments);\n\nstruct LinearInterpolator1D{T \u003c: Number} \u003c: AbstractArray{T, 1}\n    vX::Vector{T};\n    vY::Vector{T};\n\n    function LinearInterpolator1D(vX::Vector{T}, vY::Vector{T}) where {T \u003c: Number}\n        length(vX) == length(vX) || throw(ArgumentError(\"Input vectors must have the same length\"));\n        new{T}(vX, vY);\n    end\nend\n\nfunction Base.size(vA::LinearInterpolator1D)\n    size(vA.vX);\nend\nfunction Base.getindex(vA::LinearInterpolator1D, ii::Number)\n    if (ii \u003e= vA.vX[end])\n        return vA.vY[end];\n    end\n    if (ii \u003c= vA.vX[1])\n        return vA.vY[1];\n    end\n\n    rightIdx = findfirst(vA.vX .\u003e= ii);\n    leftIdx = rightIdx - 1;\n\n    tt = (ii - vA.vX[leftIdx]) / (vA.vX[rightIdx] - vA.vX[leftIdx]);\n\n    return ((1 - tt) * vA.vY[leftIdx]) + (tt * vA.vY[rightIdx]);\n\nend\nfunction Base.setindex!(vA::LinearInterpolator1D, valX, valY, ii::Int, jj::Int)\n    setindex!(sLinInterp.vX, valX, ii);\n    setindex!(sLinInterp.vY, valY, ii);\nend\n\nvXInt = LinearInterpolator1D(vR, vX);\nvYInt = LinearInterpolator1D(vR, vY);\n\nvXSegment = [vXInt[intIdx] for intIdx in vRSegment];\nvYSegment = [vYInt[intIdx] for intIdx in vRSegment];\n\nhP = lineplot(vX, vY, canvas = DotCanvas, name = \"Samples\");\nlineplot!(hP, vXSegment, vYSegment, name = \"Interpolated\");\nhP\n````\n\n````\n      ┌────────────────────────────────────────┐             \n   10 │                                     ..:│ Samples     \n      │                                 ...''  │ Interpolated\n      │                              .:''      │             \n      │                             .:         │             \n      │                            .:          │             \n      │                        .:'''           │             \n      │                       :'               │             \n      │                    ..:'                │             \n      │                  :''                   │             \n      │              ....:                     │             \n      │            ..:                         │             \n      │        :''''                           │             \n      │     .:''                               │             \n      │   .:'                                  │             \n    0 │..:'                                    │             \n      └────────────────────────────────────────┘             \n       0                                     10              \n````\n\n**Remark**: In order to be mathematically accurate the resolution of `vRSegment` must be high enough to be an integer factor of each segment.\nIt can be done if the resolution is `1 / (prod(vR))` which is easily infeasible with `Float64`. So this is a good enough approximation.\n\n## Question 099\nGiven an integer `n` and a 2D array `mA`, find the rows which can be interpreted as draws from a multinomial distribution with `n` (Rows which only contain integers and which sum to `n`). (★★★)\n\n````julia\nmA = rand([0, 0.5, 1, 2, 3], 15, 3);\nsumVal = 4;\nvI = [all(vA .== round.(vA)) \u0026\u0026 sum(vA) == sumVal for vA in eachrow(mA)];\n````\n\n## Question 100\nCompute bootstrapped `95%` confidence intervals for the mean of a 1D array `vA`. Namely, resample the elements of an array with replacement `N` times, compute the mean of each sample and then compute percentiles over the means. (★★★)\n\n````julia\nnumTrials   = 10000;\nnumSamples  = 1000;\nμ           = 0.5;\n\nvA = μ .+ randn(numSamples);\ntM = (mean(vA[rand(1:numSamples, numSamples)]) for _ in 1:numTrials);\nquantile(tM, [0.025, 0.975])\n````\n\n````\n2-element Vector{Float64}:\n 0.4620914889349693\n 0.5830619321705541\n````\n\n---\n\n*This page was generated using [Literate.jl](https://github.com/fredrikekre/Literate.jl).*\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FRoyiAvital%2FJulia100Exercises","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FRoyiAvital%2FJulia100Exercises","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FRoyiAvital%2FJulia100Exercises/lists"}