• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

RoyiAvital/Julia100Exercises: A set of introductory exercises for Julia. Based o ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

RoyiAvital/Julia100Exercises

开源软件地址:

https://github.com/RoyiAvital/Julia100Exercises

开源编程语言:

Julia 100.0%

开源软件介绍:

Visitors

100 Julia Exercises with Solutions

A set of introductory exercises for Julia. Based on 100 NumPy Exercises.

In order to generate this file:

  1. Clone the repository (Or download).
  2. Activate the project.
  3. Run:
using Literate;
Literate.markdown("Julia100Exercises.jl", name = "README", execute = true, flavor = Literate.CommonMarkFlavor());

Remark: Tested with Julia 1.7.2.

To Do:

  1. Reevaluate the difficulty level of each question.
using Literate;
using LinearAlgebra;
using Statistics;
using Dates;
using DelimitedFiles;
using UnicodePlots;
using Random;
using Tullio;
using StaticKernels;

Question 001

Import the LinearAlgebra package under the name LA. (★☆☆)

import LinearAlgebra as LA;

Question 002

Print the version of Julia. (★☆☆)

println(VERSION);
1.7.2

Question 003

Create a non initialized vector of size 10 of Float64. (★☆☆)

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

Which is equivalent of

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

Question 004

Find the memory size of any array. (★☆☆)

sizeof(vA)
80

Question 005

Show the documentation of the + (Add) method. (★☆☆)

@doc +
+(x, y...)

Addition operator. x+y+z+... calls this function with all arguments, i.e. +(x, y, z, ...).

Examples

julia> 1 + 20 + 4
25

julia> +(1, 20, 4)
25
dt::Date + t::Time -> DateTime

The 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.

Question 006

Create a vector of zeros of size 10 but the fifth value which is 1. (★☆☆)

vA = zeros(10);
vA[5] = 1.0;
vA
10-element Vector{Float64}:
 0.0
 0.0
 0.0
 0.0
 1.0
 0.0
 0.0
 0.0
 0.0
 0.0

Question 007

Create a vector with values ranging from 7 to 12. (★☆☆)

vA = 7:12
7:12

The above is efficient type. In order to explicitly create a vector:

vA = collect(7:12)
6-element Vector{Int64}:
  7
  8
  9
 10
 11
 12

Question 008

Reverse a vector (first element becomes last). (★☆☆)

vA = collect(1:3);
vB = vA[end:-1:1];
vB
3-element Vector{Int64}:
 3
 2
 1

Alternative 001:

vB = reverse(vA);

Alternative 002 (In place):

reverse!(vA);
vA
3-element Vector{Int64}:
 3
 2
 1

Question 009

Create a 3x3 matrix with values ranging from 0 to 8. (★☆☆)

mA = reshape(0:8, 3, 3)
3×3 reshape(::UnitRange{Int64}, 3, 3) with eltype Int64:
 0  3  6
 1  4  7
 2  5  8

Another way would be:

mA = Matrix{Float64}(undef, 3, 3);
mA[:] = 0:8;

Question 010

Find indices of non zero elements from [1, 2, 0, 0, 4, 0]. (★☆☆)

findall(!iszero, [1, 2, 0, 0, 4, 0])
3-element Vector{Int64}:
 1
 2
 5

Question 011

Create a 3x3 identity matrix. (★☆☆)

mA = I(3)
3×3 LinearAlgebra.Diagonal{Bool, Vector{Bool}}:
 1  ⋅  ⋅
 ⋅  1  ⋅
 ⋅  ⋅  1

An alternative method (Explicit matrix) would be:

mA = Matrix(I, 3, 3) #<! For Float64: Matrix{Float64}(I, 3, 3)
3×3 Matrix{Bool}:
 1  0  0
 0  1  0
 0  0  1

Question 012

Create a 2x2x2 array with random values. (★☆☆)

mA = randn(2, 2, 2)
2×2×2 Array{Float64, 3}:
[:, :, 1] =
  0.757423  0.90535
 -1.42309   1.20069

[:, :, 2] =
 -0.325565  -0.341087
 -0.169036  -0.13069

Question 013

Create a 5x5 array with random values and find the minimum and maximum values. (★☆☆)

mA = rand(5, 5);
minVal = minimum(mA)
0.22387297955883545
maxVal = maximum(mA)
0.9681837108020949

Using extrema() one could get both values at once:

minVal, maxVal = extrema(mA);

Question 014

Create a random vector of size 30 and find the mean value. (★☆☆)

meanVal = mean(randn(30))
0.10430740159495046

Question 015

Create a 2d array with 1 on the border and 0 inside. (★☆☆)

mA = zeros(4, 4);
mA[:, [1, end]] .= 1;
mA[[1, end], :] .= 1;
mA
4×4 Matrix{Float64}:
 1.0  1.0  1.0  1.0
 1.0  0.0  0.0  1.0
 1.0  0.0  0.0  1.0
 1.0  1.0  1.0  1.0

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 016

Add a border of zeros around the array. (★☆☆)

mB = zeros(size(mA) .+ 2);
mB[2:(end - 1), 2:(end - 1)] = mA;
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

Question 017

Evaluate the following expressions. (★☆☆)

0 * NaN
NaN
NaN == NaN
false
Inf > NaN
false
NaN - NaN
NaN
NaN in [NaN]
false
0.3 == 3 * 0.1
false

Question 018

Create a 5x5 matrix with values [1, 2, 3, 4] just below the diagonal. (★☆☆)

mA = diagm(5, 5, -1 => 1:4)
5×5 Matrix{Int64}:
 0  0  0  0  0
 1  0  0  0  0
 0  2  0  0  0
 0  0  3  0  0
 0  0  0  4  0

Question 019

Create a 8x8 matrix and fill it with a checkerboard pattern. (★☆☆)

mA = zeros(8, 8);
mA[2:2:end, 1:2:end] .= 1;
mA[1:2:end, 2:2:end] .= 1;
mA
8×8 Matrix{Float64}:
 0.0  1.0  0.0  1.0  0.0  1.0  0.0  1.0
 1.0  0.0  1.0  0.0  1.0  0.0  1.0  0.0
 0.0  1.0  0.0  1.0  0.0  1.0  0.0  1.0
 1.0  0.0  1.0  0.0  1.0  0.0  1.0  0.0
 0.0  1.0  0.0  1.0  0.0  1.0  0.0  1.0
 1.0  0.0  1.0  0.0  1.0  0.0  1.0  0.0
 0.0  1.0  0.0  1.0  0.0  1.0  0.0  1.0
 1.0  0.0  1.0  0.0  1.0  0.0  1.0  0.0

By Tomer Arnon (https://github.com/tomerarnon):

mA = Int[isodd(ii + jj) for ii in 1:8, jj in 1:8];

Question 020

Convert the linear index 100 to a Cartesian Index of a size (6,7,8). (★☆☆)

mA = rand(6, 7, 8);
cartIdx = CartesianIndices(mA)[100]; #<! See https://discourse.julialang.org/t/14666
mA[cartIdx] == mA[100]
true

Question 021

Create a checkerboard 8x8 matrix using the repeat() function. (★☆☆)

mA = repeat([0 1; 1 0], 4, 4)
8×8 Matrix{Int64}:
 0  1  0  1  0  1  0  1
 1  0  1  0  1  0  1  0
 0  1  0  1  0  1  0  1
 1  0  1  0  1  0  1  0
 0  1  0  1  0  1  0  1
 1  0  1  0  1  0  1  0
 0  1  0  1  0  1  0  1
 1  0  1  0  1  0  1  0

Question 022

Normalize a 4x4 random matrix. (★☆☆)

mA = rand(4, 4);
mA .= (mA .- mean(mA)) ./ std(mA) #<! Pay attention that `@.` will yield error (`std()` and `mean()`)
4×4 Matrix{Float64}:
  1.20607    -1.13748    0.823965   0.850322
 -0.0389568  -0.163611   0.926198  -0.9654
  0.0334464   0.969688  -1.95059   -0.81439
  0.323745   -1.55141    0.780498   0.707899

Question 023

Create a custom type that describes a color as four unsigned bytes (RGBA). (★☆☆)

struct sColor
    R::UInt8;
    G::UInt8;
    B::UInt8;
    A::UInt8;
end

sMyColor = sColor(rand(UInt8, 4)...)
Main.##300.sColor(0x7f, 0xe0, 0x77, 0x8d)

Question 024

Multiply a 2x4 matrix by a 4x3 matrix. (★☆☆)

mA = rand(2, 4) * randn(4, 3)
2×3 Matrix{Float64}:
 -1.69224     0.207454  -0.348213
 -0.0328024  -0.194292  -0.171239

Question 025

Given 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)
8-element Vector{Int64}:
  8
 -5
  9
  3
 -6
 -5
 10
 -7

Julia allows Math like notation as well (See Q0027):

vA = rand(1:10, 8);
map!(x -> 3 < x < 8 ? -x : x, vA, vA)
8-element Vector{Int64}:
 10
  9
  8
  8
 -4
  1
 10
  8

Using logical indices one could use:

vA[3 .< vA .< 8] .*= -1;

Question 026

Sum the array 1:4 with initial value of -10. (★☆☆)

sum(1:4, init = -10)
0

Question 027

Consider an integer vector vZ validate the following expressions. (★☆☆)

vZ .^ vZ
2 << vZ >> 2
vZ <- vZ
1im * vZ
vZ / 1 / 1
vZ < Z > Z
vZ = rand(1:10, 3);
vZ .^ vZ
3-element Vector{Int64}:
 10000000000
       46656
        3125
try
    2 << vZ >> 2
catch e
    println(e)
end
MethodError(<<, (2, [10, 6, 5]), 0x0000000000007d24)

vZ <- vZ
false
1im * vZ
3-element Vector{Complex{Int64}}:
 0 + 10im
 0 + 6im
 0 + 5im
vZ / 1 / 1
3-element Vector{Float64}:
 10.0
  6.0
  5.0
vZ < vZ > vZ
false

Question 028

Evaluate the following expressions. (★☆☆)

[0] ./ [0]
1-element Vector{Float64}:
 NaN
try
    [0]  [0]
catch e
    println(e)
end
DivideError()

try
    convert(Float, convert(Int, NaN))
catch e
    println(e)
end
InexactError(:Int64, Int64, NaN)

Question 029

Round away from zero a float array. (★☆☆)

vA = randn(10);
map(x -> x > 0 ? ceil(x) : floor(x), vA)

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap