100 Julia exercises with solutions
Here is a Julia version of exercises with solutions which is based on the exercises given here: https://github.com/rougier/numpy-100
1. Import the julia package LinearAlgebra under the name la
1import LinearAlgebra as la #or
2import LinearAgebra
3const la = LinearAlgebra
2. Print the version of julia package Javis
xxxxxxxxxx
21]
2status Javis
3. Create a zero vector of size 10 and of type Int64
xxxxxxxxxx
11a = zeros(Int64,10)
4. How to find the memory size of any array in bytes
xxxxxxxxxx
21sizeof(a)
2length(a)*sizeof(eltype(a))
5. How to get the documentation of the Julia zero function from the command line?
xxxxxxxxxx
11?zeros
6. Create a Int vector of size 10 but the fifth value which is 1 (in two lines of code)
xxxxxxxxxx
21a = zeros(Int,10)
2a[5] = 1
7. Create a vector with values ranging from 10 to 49
xxxxxxxxxx
21a = [10:49;]
2a = collect(10:49)
8. Return the reverse of a vector (first element becomes last)
xxxxxxxxxx
31a[end:-1:1] #faster
2reverse(a)
3reverse!(a) #inplace
9. Create a 3x3 matrix with values ranging from 0 to 8
xxxxxxxxxx
11a = reshape(0:8,3,3)
10. Find indices of non-zero elements from [1,2,0,0,4,0]
xxxxxxxxxx
31findall(!isequal(0),a)
2findall(!iszero,a)
3findall(!=(0),a)
11. Create a 3x3 identity matrix
xxxxxxxxxx
31using LinearAlgebra
2a = I(3)
3a = Matrix(1.0I,3,3)
12. Create a 3x3x3 array with random values
xxxxxxxxxx
21a = rand(3,3,3) #numbers in the range 0 to 1
2b = randn(3,3,3) #Gaussian random numbers
13. Create a 10x10 array with random values and find the minimum and maximum values
xxxxxxxxxx
31a = rand(10,10)
2minm = minimum(a)
3maxm = maximum(a)
4minm, maxm = extrema(a)
14. Create a random vector of size 30 and find the mean value
xxxxxxxxxx
31using Statistics
2a = rand(30) #rand(30,)
3b = mean(a)
15. Create a 2d array with 1 on the border and 0 inside
xxxxxxxxxx
61a = zeros(5,5)
2a[1,:] .= 1
3a[end,:] .= 1
4a[2:end-1,1] .= 1
5a[2:end-1,end] .= 1
6a[:,1] .= 1;a[:,end] .= 1 #Last two steps can be replaced by this line
16. How to add a border (filled with 0's) around an existing array?
xxxxxxxxxx
31a = rand(5,5)
2b = zeros(Float64,size(a,1)+2,size(a,2)+2)# b = zeros(eltype(a), size(a) .+ 1)
3b[2:end-1,2:end-1] = a
17. What is the result of the following expression? (★☆☆)
xxxxxxxxxx
81 (0 * NaN) >= NaN : false
2NaN == NaN : false
3NaN > NaN : false
4NaN - NaN : NaN
5NaN in Set([NaN]) : true
6NaN in [NaN] : false
70.3 == 3 * 0.1 : false #floating point rounding error
8isapprox(0.3 , 3 * 0.1) : true
18. Create a 5x5 matrix with values 1,2,3,4 just below the diagonal
xxxxxxxxxx
11a = Bidiagonal(zeros(5,),collect(1:4),:L)
19. Create a 8x8 matrix and fill it with a checkerboard pattern
xxxxxxxxxx
31a = zeros(Int64,8,8)
2a[1:2:end,1:2:end] .= 1
3a[2:2:end,2:2:end] .= 1 #or
4a = [Int(isodd(i+j)) for i in 1:8, j in 1:8]
20. Consider a (6,7,8) shape array, what is the index (x,y,z) of the 100th element?
x1m = 100
2n,p,r = 6,7,8
3page = Int64(ceil(m/n/p))
4col = Int64(ceil((m-(page-1)*n*p)/n))
5row = Int64(ceil((m-(page-1)*n*p-(col-1)*n)/n))
6
7CartesianIndices((6,7,8))[m] #builtin command
21. Create a checkerboard 8x8 matrix using the repeat function (★☆☆)
xxxxxxxxxx
11repeat([1 0;0 1],4,4)
22. Normalize a 5x5 random matrix (★☆☆)
xxxxxxxxxx
31a = randn(5,5)
2a = a .- minimum(a)
3a = a./maximum(a)
23. Create a custom dtype that describes a color as four unsigned bytes (RGBA) (★☆☆)
xxxxxxxxxx
61mutable struct color
2 R::UInt8
3 G::UInt8
4 B::UInt8
5 A::UInt8
6end
24. Multiply a 5x3 matrix by a 3x2 matrix (real matrix product)
xxxxxxxxxx
21a = rand(5,3);b = rand(3,2)
2c = a*b
25. Given a 1D array, negate all elements which are between 3 and 8, in place. (★☆☆)
xxxxxxxxxx
41a = 3;b = 8;
2x = rand(1:10,20) # collect(1:10)
3x[a .<= x .<= b] .= -x[a .<= x .<= b] #or
4x[a .<= x .<= b] .*= -1.
26. What is the output of the following script? (★☆☆) [Python only]
xxxxxxxxxx
11
27. Consider an integer vector Z, which of these expressions are legal?
xxxxxxxxxx
61Z.^Z #valid
22 << Z >> 2 #not valid
3Z <- Z # Z < (-Z)
41im*Z #valid
5Z/1/1 #valid,converted to float vector
6Z<Z>Z #valid
28. What are the result of the following expressions? (★☆☆)
xxxxxxxxxx
51[0]/[0] : 1x1 Matrix Float64 0.0
2[0] ./ [0] : NaN
3[0]// [0] : MethodError
4[0] .÷ [0] : DivideError
5Float64(Int64([NaN])) : MethodError
29. How to round away from zero a float array ? (★☆☆)
xxxxxxxxxx
11round.(-4.5, RoundNearestTiesAway)
30. How to find common values between two arrays? (★☆☆)
xxxxxxxxxx
11intersect(A,B) #A and B are sets
31. How to ignore all Julia warnings (not recommended)? (★☆☆)
32. Is the following expressions true? [Python only]★☆☆)
xxxxxxxxxx
11np.sqrt(-1) == np.emath.sqrt(-1)
33. How to get the dates of yesterday, today and tomorrow? (★☆☆)
xxxxxxxxxx
41using Dates
2tod = today()
3tom = tod + Day(1)
4yes = tod + Day(-1)
34. How to get all the dates corresponding to the month of July 2016? (★★☆)
xxxxxxxxxx
21using Dates
2mon = collect(Date(2016,7,1):Day(1):Date(2016,7,31))
35. How to compute ((A+B)*(-A/2)) in place (without copy)? (★★☆)
xxxxxxxxxx
21A = rand(2,2);B = rand(2,2)
2@. A = ((A+B)*(-A/2))
36. Extract the integer part of a random array of positive numbers using 4 different methods (★★☆)
xxxxxxxxxx
51A = 10*rand(1,10)
2Int64.(trunc.(A))
3B = parse.(Int64,[split(string(x),".")[1] for x in A])
4Int64.(floor.(A))
5Int64.(ceil.(A)) .- 1
37. Create a 5x5 matrix with row values ranging from 0 to 4 (★★☆)
xxxxxxxxxx
11a = rand(0:4,5,5)
38. Consider a generator function that generates 10 integers and use it to build an array (★☆☆)
xxxxxxxxxx
11a = collect(1:10)
39. Create a vector of size 10 with values ranging from 0 to 1, both excluded (★★☆)
xxxxxxxxxx
11a = rand(10)
40. Create a random vector of size 10 and sort it (★★☆)
xxxxxxxxxx
21a = rand(10,)
2sort!(a)
41. How to sum a small array faster than np.sum?[Python only] (★★☆)
42. Consider two random array A and B, check if they are equal (★★☆)
xxxxxxxxxx
11isequal(A,B)
43. Make an array immutable (read-only) (★★☆)
44. Consider a random 10x2 matrix representing cartesian coordinates, convert them to polar coordinates (★★☆)
x
1a = rand(10,2);
2ang = angle.(a[:,1] .+ 1im*a[:,2])
3r = hypot.(a[:,1] .+ a[:,2])# sqrt.(a[:,1].^2 .+ a[:,2].^2)
4p = r.*cis.(ang) # r.*exp.(1im*ang)
5#or
6to_polar(x, y) = hypot(x, y) * cis(angle(x+im*y))
7[to_polar(x...) for x in eachrow(a)] #or
8[to_polar(a[i, 1], a[i, 2]) for i in axes(a, 1)]
45. Create random vector of size 10 and replace the maximum value by 0 (★★☆)
xxxxxxxxxx
21a = rand(10)
2a[a .== maximum(a)] .= 0
46. Create a structured array with x
and y
coordinates covering the [0,1]x[0,1] area (★★☆)
xxxxxxxxxx
41mutable struct xy
2x:Float64
3y:Float64
4end
47. Given two arrays, X and Y, construct the Cauchy matrix C (Cij =1/(xi - yj)) (★★☆)
x
1X = rand(5);Y = rand(5);
2C = [1/(xi-yi) for xi in X, yi in Y]#or
3C = 1 ./ (X .- Y')
48. Print the minimum and maximum representable value for each Julia scalar type
xxxxxxxxxx
21typemin()
2typemax()
49. How to print all the values of an array? (★★☆)
xxxxxxxxxx
21print(a)
2display(a)
3foreach(println, a) #prints full array
50. How to find the closest value (to a given scalar) in a vector? (★★☆)
x
1a = rand(10)
2x = 0.5 #given scalar
3b = abs.(a .- x)
4a[findmin(b)[2]]
I will update remaining exercises in due course.
Comments
Post a Comment