diff --git a/Julia100Exercises.jl b/Julia100Exercises.jl index 4451773..a84775c 100644 --- a/Julia100Exercises.jl +++ b/Julia100Exercises.jl @@ -6,14 +6,14 @@ # # In order to generate this file: # 1. Clone the repository (Or download). -# 2. Activate the project. +# 2. Activate and instantiate the project. # 3. Run: # ```Julia # using Literate; # Literate.markdown("Julia100Exercises.jl", name = "README", execute = true, flavor = Literate.CommonMarkFlavor()); # ``` # -# **Remark**: Tested with Julia `1.7.2`. +# **Remark**: Tested with Julia `1.8.2`. # # **To Do**: # 1. Reevaluate the difficulty level of each question. @@ -143,7 +143,7 @@ mA[:, [1, end]] .= 1; mA[[1, end], :] .= 1; mA -# An alternative way: +# An alternative way (Different dimensions): mA = ones(4, 5); mA[2:(end - 1), 2:(end - 1)] .= 0; @@ -153,10 +153,10 @@ mA[2:(end - 1), 2:(end - 1)] .= 0; mA = zeros(4, 5); mA[[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; -# By Tomer Arnon (https://github.com/tomerarnon): +# By [Tomer Arnon](https://github.com/tomerarnon): -numRows = 4; -numCols = 5; +numRows = 5; +numCols = 4; mA = Int[ii ∈ (1, numRows) || jj ∈ (1, numCols) for ii in 1:numRows, jj in 1:numCols]; # ## Question 016 @@ -467,7 +467,7 @@ mX, mY = MeshGrid(vX, vY); # abs(y - inputVal), vA); @@ -647,7 +647,7 @@ end vA[closeSecondIdx] == vA[sortperm(abs.(vA .- inputVal))[2]] -# By Tomer Arnon (https://github.com/tomerarnon): +# By [Tomer Arnon](https://github.com/tomerarnon): vA[partialsortperm(abs.(vA .- inputVal), 2)] @@ -1223,7 +1223,7 @@ end mB -# By Tomer Arnon (https://github.com/tomerarnon): +# By [Tomer Arnon](https://github.com/tomerarnon): mB = reverse!(reduce(hcat, digits.(vA, base = 2, pad = 8))', dims = 2); @@ -1334,8 +1334,8 @@ vI = [all(vA .== round.(vA)) && sum(vA) == sumVal for vA in eachrow(mA)]; # ## Question 100 # Compute 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. (★★★) -numTrials = 1000; -numSamples = 100; +numTrials = 10000; +numSamples = 1000; μ = 0.5; vA = μ .+ randn(numSamples); diff --git a/README.md b/README.md index 43c30aa..498283b 100644 --- a/README.md +++ b/README.md @@ -6,14 +6,14 @@ A set of introductory exercises for Julia. Based on [100 NumPy Exercises](https: In order to generate this file: 1. Clone the repository (Or download). -2. Activate the project. +2. Activate and instantiate the project. 3. Run: ```Julia using Literate; Literate.markdown("Julia100Exercises.jl", name = "README", execute = true, flavor = Literate.CommonMarkFlavor()); ``` -**Remark**: Tested with Julia `1.7.2`. +**Remark**: Tested with Julia `1.8.2`. **To Do**: 1. Reevaluate the difficulty level of each question. @@ -30,6 +30,16 @@ using Tullio; using StaticKernels; ```` +```` +[ Info: Precompiling UnicodePlots [b8865327-cd53-5732-bb35-84acbb429228] +WARNING: method definition for centered at D:\Applications\Programming\JuliaWin\userdata\.julia\packages\GeometryBasics\3PqdK\src\primitives\rectangles.jl:515 declares type variable N but does not use it. +WARNING: method definition for volume at D:\Applications\Programming\JuliaWin\userdata\.julia\packages\GeometryBasics\3PqdK\src\meshes.jl:209 declares type variable FT but does not use it. +WARNING: method definition for volume at D:\Applications\Programming\JuliaWin\userdata\.julia\packages\GeometryBasics\3PqdK\src\meshes.jl:209 declares type variable VT but does not use it. +WARNING: method definition for volume at D:\Applications\Programming\JuliaWin\userdata\.julia\packages\GeometryBasics\3PqdK\src\meshes.jl:221 declares type variable FT but does not use it. +WARNING: method definition for volume at D:\Applications\Programming\JuliaWin\userdata\.julia\packages\GeometryBasics\3PqdK\src\meshes.jl:221 declares type variable VT but does not use it. + +```` + ## Question 001 Import the `LinearAlgebra` package under the name `LA`. (★☆☆) @@ -45,7 +55,7 @@ println(VERSION); ```` ```` -1.7.2 +1.8.2 ```` @@ -58,16 +68,16 @@ vA = Vector{Float64}(undef, 10) ```` 10-element Vector{Float64}: - 1.335846132e-314 - 9.473510105e-315 - 9.473550895e-315 - 9.47399579e-315 - 1.335904171e-314 - 1.3359041947e-314 - 1.3359042184e-314 - 9.473992946e-315 - 9.47399326e-315 - 9.47399358e-315 + 0.0 + 1.314224183e-315 + 1.314224183e-315 + 0.0 + 1.314248214e-315 + 1.31427351e-315 + 0.0 + 1.314248214e-315 + 1.314106556e-315 + 0.0 ```` Which is equivalent of @@ -78,16 +88,16 @@ vA = Array{Float64, 1}(undef, 10) ```` 10-element Vector{Float64}: - 1.5e-323 - 1.6e-322 - 6.95331311286057e-310 - 4.94e-322 - 4.94e-321 - 1.235e-321 - 1.0e-322 - 1.5e-323 - 1.6e-322 - 2.0e-323 + 0.0 + 1.314248214e-315 + 1.314106556e-315 + 0.0 + 1.314248214e-315 + 1.314106556e-315 + 0.0 + 1.314248214e-315 + 1.314106556e-315 + 0.0 ```` ## Question 004 @@ -289,12 +299,12 @@ mA = randn(2, 2, 2) ```` 2×2×2 Array{Float64, 3}: [:, :, 1] = - 0.757423 0.90535 - -1.42309 1.20069 + -1.34961 -0.225955 + 0.116267 1.03802 [:, :, 2] = - -0.325565 -0.341087 - -0.169036 -0.13069 + 0.737672 2.2642 + -0.839961 0.218133 ```` ## Question 013 @@ -306,7 +316,7 @@ minVal = minimum(mA) ```` ```` -0.22387297955883545 +0.02305480092860468 ```` ````julia @@ -314,7 +324,7 @@ maxVal = maximum(mA) ```` ```` -0.9681837108020949 +0.9708814560256962 ```` Using `extrema()` one could get both values at once: @@ -331,7 +341,7 @@ meanVal = mean(randn(30)) ```` ```` -0.10430740159495046 +-0.04218053798839749 ```` ## Question 015 @@ -352,7 +362,7 @@ mA 1.0 1.0 1.0 1.0 ```` -An alternative way: +An alternative way (Different dimensions): ````julia mA = ones(4, 5); @@ -366,11 +376,11 @@ mA = zeros(4, 5); mA[[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; ```` -By Tomer Arnon (https://github.com/tomerarnon): +By [Tomer Arnon](https://github.com/tomerarnon): ````julia -numRows = 4; -numCols = 5; +numRows = 5; +numCols = 4; mA = Int[ii ∈ (1, numRows) || jj ∈ (1, numCols) for ii in 1:numRows, jj in 1:numCols]; ```` @@ -384,13 +394,14 @@ mB ```` ```` -6×7 Matrix{Float64}: - 0.0 0.0 0.0 0.0 0.0 0.0 0.0 - 0.0 1.0 1.0 1.0 1.0 1.0 0.0 - 0.0 1.0 0.0 0.0 0.0 1.0 0.0 - 0.0 1.0 0.0 0.0 0.0 1.0 0.0 - 0.0 1.0 1.0 1.0 1.0 1.0 0.0 - 0.0 0.0 0.0 0.0 0.0 0.0 0.0 +7×6 Matrix{Float64}: + 0.0 0.0 0.0 0.0 0.0 0.0 + 0.0 1.0 1.0 1.0 1.0 0.0 + 0.0 1.0 0.0 0.0 1.0 0.0 + 0.0 1.0 0.0 0.0 1.0 0.0 + 0.0 1.0 0.0 0.0 1.0 0.0 + 0.0 1.0 1.0 1.0 1.0 0.0 + 0.0 0.0 0.0 0.0 0.0 0.0 ```` ## Question 017 @@ -530,10 +541,10 @@ mA .= (mA .- mean(mA)) ./ std(mA) # ((x > 3) && (x < 8)) ? -x : x, vA, vA) ```` 8-element Vector{Int64}: - 8 - -5 - 9 - 3 -6 + 3 + 1 + -4 + 3 + -4 -5 - 10 - -7 + -4 ```` Julia allows Math like notation as well (See `Q0027`): @@ -596,14 +607,14 @@ map!(x -> 3 < x < 8 ? -x : x, vA, vA) ```` 8-element Vector{Int64}: - 10 - 9 - 8 - 8 + -7 + -6 -4 1 + -6 10 8 + -5 ```` Using logical indices one could use: @@ -644,9 +655,9 @@ vZ .^ vZ ```` 3-element Vector{Int64}: - 10000000000 - 46656 - 3125 + 387420489 + 387420489 + 46656 ```` ````julia @@ -658,7 +669,7 @@ end ```` ```` -MethodError(<<, (2, [10, 6, 5]), 0x0000000000007d24) +MethodError(<<, (2, [9, 9, 6]), 0x0000000000007f1f) ```` @@ -676,9 +687,9 @@ false ```` 3-element Vector{Complex{Int64}}: - 0 + 10im + 0 + 9im + 0 + 9im 0 + 6im - 0 + 5im ```` ````julia @@ -687,9 +698,9 @@ vZ / 1 / 1 ```` 3-element Vector{Float64}: - 10.0 - 6.0 - 5.0 + 9.0 + 9.0 + 6.0 ```` ````julia @@ -748,16 +759,16 @@ map(x -> x > 0 ? ceil(x) : floor(x), vA) ```` 10-element Vector{Float64}: - -1.0 1.0 - -2.0 -1.0 -1.0 -1.0 - -3.0 - -3.0 - -2.0 2.0 + -1.0 + 2.0 + 2.0 + 2.0 + 1.0 ```` ## Question 030 @@ -771,13 +782,9 @@ vA[findall(in(vB), vA)] ```` ```` -6-element Vector{Int64}: - 2 - 10 - 2 - 10 - 8 - 8 +2-element Vector{Int64}: + 7 + 5 ```` ## Question 031 @@ -819,9 +826,9 @@ println("Tomorrow: $(today() + Day(1))"); ```` ```` -Yesterday: 2022-04-08 -Today: 2022-04-09 -Tomorrow: 2022-04-10 +Yesterday: 2022-10-26 +Today: 2022-10-27 +Tomorrow: 2022-10-28 ```` @@ -878,8 +885,8 @@ mA .= ((mA .+ mB) .* (.-mA ./ 2)) ```` 2×2 Matrix{Float64}: - -0.412244 -0.467578 - -0.180105 -0.272612 + -0.35038 -0.685683 + -0.510546 -0.444504 ```` Using the dot macro: @@ -903,9 +910,9 @@ floor.(mA) ```` 3×3 Matrix{Float64}: - 2.0 2.0 4.0 - 3.0 1.0 1.0 - 4.0 2.0 3.0 + 0.0 0.0 4.0 + 2.0 3.0 4.0 + 0.0 3.0 3.0 ```` Option 2: @@ -916,9 +923,9 @@ round.(mA .- 0.5) #