在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:RoyiAvital/Julia100Exercises开源软件地址:https://github.com/RoyiAvital/Julia100Exercises开源编程语言:Julia 100.0%开源软件介绍:100 Julia Exercises with SolutionsA set of introductory exercises for Julia. Based on 100 NumPy Exercises. In order to generate this file:
using Literate;
Literate.markdown("Julia100Exercises.jl", name = "README", execute = true, flavor = Literate.CommonMarkFlavor()); Remark: Tested with Julia To Do:
using Literate;
using LinearAlgebra;
using Statistics;
using Dates;
using DelimitedFiles;
using UnicodePlots;
using Random;
using Tullio;
using StaticKernels; Question 001Import the import LinearAlgebra as LA; Question 002Print the version of Julia. (★☆☆) println(VERSION);
Question 003Create a non initialized vector of size 10 of vA = Vector{Float64}(undef, 10)
Which is equivalent of vA = Array{Float64, 1}(undef, 10)
Question 004Find the memory size of any array. (★☆☆) sizeof(vA)
Question 005Show the documentation of the @doc +
Addition operator. Examples
The addition of a Question 006Create a vector of zeros of size 10 but the fifth value which is 1. (★☆☆) vA = zeros(10);
vA[5] = 1.0;
vA
Question 007Create a vector with values ranging from 7 to 12. (★☆☆) vA = 7:12
The above is efficient type. In order to explicitly create a vector: vA = collect(7:12)
Question 008Reverse a vector (first element becomes last). (★☆☆) vA = collect(1:3);
vB = vA[end:-1:1];
vB
Alternative 001: vB = reverse(vA); Alternative 002 (In place): reverse!(vA);
vA
Question 009Create a mA = reshape(0:8, 3, 3)
Another way would be: mA = Matrix{Float64}(undef, 3, 3);
mA[:] = 0:8; Question 010Find indices of non zero elements from findall(!iszero, [1, 2, 0, 0, 4, 0])
Question 011Create a 3x3 identity matrix. (★☆☆) mA = I(3)
An alternative method (Explicit matrix) would be: mA = Matrix(I, 3, 3) #<! For Float64: Matrix{Float64}(I, 3, 3)
Question 012Create a mA = randn(2, 2, 2)
Question 013Create a mA = rand(5, 5);
minVal = minimum(mA)
maxVal = maximum(mA)
Using minVal, maxVal = extrema(mA); Question 014Create a random vector of size 30 and find the mean value. (★☆☆) meanVal = mean(randn(30))
Question 015Create a 2d array with 1 on the border and 0 inside. (★☆☆) mA = zeros(4, 4);
mA[:, [1, end]] .= 1;
mA[[1, end], :] .= 1;
mA
An alternative way: mA = ones(4, 5);
mA[2:(end - 1), 2:(end - 1)] .= 0; Using one line code: 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): numRows = 4;
numCols = 5;
mA = Int[ii ∈ (1, numRows) || jj ∈ (1, numCols) for ii in 1:numRows, jj in 1:numCols]; Question 016Add a border of zeros around the array. (★☆☆) mB = zeros(size(mA) .+ 2);
mB[2:(end - 1), 2:(end - 1)] = mA;
mB
Question 017Evaluate the following expressions. (★☆☆) 0 * NaN
NaN == NaN
Inf > NaN
NaN - NaN
NaN in [NaN]
0.3 == 3 * 0.1
Question 018Create a mA = diagm(5, 5, -1 => 1:4)
Question 019Create a mA = zeros(8, 8);
mA[2:2:end, 1:2:end] .= 1;
mA[1:2:end, 2:2:end] .= 1;
mA
By Tomer Arnon (https://github.com/tomerarnon): mA = Int[isodd(ii + jj) for ii in 1:8, jj in 1:8]; Question 020Convert the linear index 100 to a Cartesian Index of a size mA = rand(6, 7, 8);
cartIdx = CartesianIndices(mA)[100]; #<! See https://discourse.julialang.org/t/14666
mA[cartIdx] == mA[100]
Question 021Create a checkerboard mA = repeat([0 1; 1 0], 4, 4)
Question 022Normalize a mA = rand(4, 4);
mA .= (mA .- mean(mA)) ./ std(mA) #<! Pay attention that `@.` will yield error (`std()` and `mean()`)
Question 023Create a custom type that describes a color as four unsigned bytes ( struct sColor
R::UInt8;
G::UInt8;
B::UInt8;
A::UInt8;
end
sMyColor = sColor(rand(UInt8, 4)...)
Question 024Multiply a mA = rand(2, 4) * randn(4, 3)
Question 025Given a 1D array, negate all elements which are between 3 and 8, in place. (★☆☆) vA = rand(1:10, 8);
map!(x -> ((x > 3) && (x < 8)) ? -x : x, vA, vA)
Julia allows Math like notation as well (See vA = rand(1:10, 8);
map!(x -> 3 < x < 8 ? -x : x, vA, vA)
Using logical indices one could use: vA[3 .< vA .< 8] .*= -1; Question 026Sum the array sum(1:4, init = -10)
Question 027Consider an integer vector vZ .^ vZ
2 << vZ >> 2
vZ <- vZ
1im * vZ
vZ / 1 / 1
vZ < Z > Z vZ = rand(1:10, 3); vZ .^ vZ
try
2 << vZ >> 2
catch e
println(e)
end
vZ <- vZ
1im * vZ
vZ / 1 / 1
vZ < vZ > vZ
Question 028Evaluate the following expressions. (★☆☆) [0] ./ [0]
try
[0] .÷ [0]
catch e
println(e)
end
try
convert(Float, convert(Int, NaN))
catch e
println(e)
end
Question 029Round away from zero a float array. (★☆☆) vA = randn(10);
map(x -> x > 0 ? ceil(x) : floor(x), vA) 全部评论
专题导读
热门推荐
热门话题
阅读排行榜
|
请发表评论