SkillAgentSearch skills...

Julia100Exercises

A set of introductory exercises for Julia. Based on [100 NumPy Exercises](https://github.com/rougier/numpy-100).

Install / Use

/learn @RoyiAvital/Julia100Exercises
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

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 and instantiate the project.
  3. Run:
using Literate;
Literate.markdown("Julia100Exercises.jl", name = "README", execute = true, flavor = Literate.CommonMarkFlavor());

Remark: Tested with Julia 1.8.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.8.2

Question 003

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

vA = Vector{Float64}(undef, 10)
10-element Vector{Float64}:
 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

vA = Array{Float64, 1}(undef, 10)
10-element Vector{Float64}:
 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

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] =
 -1.34961   -0.225955
  0.116267   1.03802

[:, :, 2] =
  0.737672  2.2642
 -0.839961  0.218133

Question 013

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

mA = rand(5, 5);
minVal = minimum(mA)
0.02305480092860468
maxVal = maximum(mA)
0.9708814560256962

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

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 (Different dimensions):

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:

numRows = 5;
numCols = 4;
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
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

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.0589     0.696722    0.474169   0.0807478
  1.33922    1.23329    -0.559528  -1.24305
 -0.652489   0.919517    1.16875   -1.49035
 -0.0565761  0.0550669   0.711533  -1.61812

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.var"##312".sColor(0xb0, 0xae, 0x35, 0x81)

Question 024

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

mA = rand(2, 4) * randn(4, 3)
2×3 Matrix{Float64}:
 -1.47402   -1.10919   -1.22996
 -0.745242   0.188422  -3.19746

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}:
 -6
  3
  1
 -4
  3
 -4
 -5
 -4

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}:
 -7
 -6
 -4
  1
 -6
 10
  8
 -5

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}:
 387420489
 387420489
     46656
try
    2 << vZ >> 2
catch e
    println(e)
end
MethodError(<<, (2, [9, 9, 6]), 0x0000000000007f1f)

vZ <- vZ
false
1im * vZ
3-element Vector{Complex{Int64}}:
 0 + 9im
 0

Related Skills

View on GitHub
GitHub Stars144
CategoryEducation
Updated2d ago
Forks25

Languages

Julia

Security Score

100/100

Audited on Mar 26, 2026

No findings