### 100 Julia exercises with solutions

100_Julia_exercises_First50

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 #or2import LinearAgebra3const la = LinearAlgebra

#### 2. Print the version of julia package Javis

xxxxxxxxxx21] 2status Javis

#### 3. Create a zero vector of size 10 and of type Int64

xxxxxxxxxx11a = zeros(Int64,10)

#### 4. How to find the memory size of any array in bytes

xxxxxxxxxx21sizeof(a)2length(a)*sizeof(eltype(a))

#### 5. How to get the documentation of the Julia zero function from the command line?

xxxxxxxxxx11?zeros

#### 6. Create a Int vector of size 10 but the fifth value which is 1 (in two lines of code)

xxxxxxxxxx21a = zeros(Int,10)2a = 1

#### 7. Create a vector with values ranging from 10 to 49

xxxxxxxxxx21a = [10:49;]2a = collect(10:49)

#### 8. Return the reverse of a vector (first element becomes last)

xxxxxxxxxx31a[end:-1:1] #faster2reverse(a)3reverse!(a) #inplace

#### 9. Create a 3x3 matrix with values ranging from 0 to 8

xxxxxxxxxx11a = reshape(0:8,3,3)

#### 10. Find indices of non-zero elements from [1,2,0,0,4,0]

xxxxxxxxxx31findall(!isequal(0),a)2findall(!iszero,a)3findall(!=(0),a)

#### 11. Create a 3x3 identity matrix

xxxxxxxxxx31using LinearAlgebra2a = I(3)3a = Matrix(1.0I,3,3)

#### 12. Create a 3x3x3 array with random values

xxxxxxxxxx21a = rand(3,3,3) #numbers in the range 0 to 12b = randn(3,3,3) #Gaussian random numbers

#### 13. Create a 10x10 array with random values and find the minimum and maximum values

xxxxxxxxxx31a = 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

xxxxxxxxxx31using Statistics2a = rand(30) #rand(30,)3b = mean(a)

#### 15. Create a 2d array with 1 on the border and 0 inside

xxxxxxxxxx61a = zeros(5,5)2a[1,:] .= 13a[end,:] .= 14a[2:end-1,1] .= 1 5a[2:end-1,end] .= 16a[:,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?

xxxxxxxxxx31a = 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? (★☆☆)

xxxxxxxxxx81 (0 * NaN) >= NaN : false2NaN == NaN : false3NaN > NaN : false4NaN - NaN : NaN5NaN in Set([NaN]) : true6NaN in [NaN] : false70.3 == 3 * 0.1 : false #floating point rounding error8isapprox(0.3 , 3 * 0.1) : true

#### 18. Create a 5x5 matrix with values 1,2,3,4 just below the diagonal

xxxxxxxxxx11a = Bidiagonal(zeros(5,),collect(1:4),:L)

#### 19. Create a 8x8 matrix and fill it with a checkerboard pattern

xxxxxxxxxx31a = zeros(Int64,8,8)2a[1:2:end,1:2:end] .= 13a[2:2:end,2:2:end] .= 1 #or4a = [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 = 1002n,p,r = 6,7,83page = 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 (★☆☆)

xxxxxxxxxx11repeat([1 0;0 1],4,4)

#### 22. Normalize a 5x5 random matrix (★☆☆)

xxxxxxxxxx31a = 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) (★☆☆)

xxxxxxxxxx61mutable struct color2  R::UInt83  G::UInt84  B::UInt85  A::UInt86end

#### 24. Multiply a 5x3 matrix by a 3x2 matrix (real matrix product)

xxxxxxxxxx21a = 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. (★☆☆)

xxxxxxxxxx41a = 3;b = 8;2x = rand(1:10,20) # collect(1:10)3x[a .<= x .<= b] .= -x[a .<= x .<= b] #or4x[a .<= x .<= b] .*= -1.

#### 26. What is the output of the following script? (★☆☆) [Python only]

xxxxxxxxxx11

xxxxxxxxxx61Z.^Z #valid22 << Z >> 2 #not valid3Z <- Z # Z < (-Z)41im*Z #valid5Z/1/1 #valid,converted to float vector6Z<Z>Z #valid

#### 28. What are the result of the following expressions? (★☆☆)

xxxxxxxxxx51/ : 1x1 Matrix Float64 0.02 ./  : NaN3//  : MethodError4 .÷  : DivideError5Float64(Int64([NaN])) : MethodError

#### 29. How to round away from zero a float array ? (★☆☆)

xxxxxxxxxx11round.(-4.5, RoundNearestTiesAway)

#### 30. How to find common values between two arrays? (★☆☆)

xxxxxxxxxx11intersect(A,B) #A and B are sets

#### 32. Is the following expressions true? [Python only]★☆☆)

xxxxxxxxxx11np.sqrt(-1) == np.emath.sqrt(-1)

#### 33. How to get the dates of yesterday, today and tomorrow? (★☆☆)

xxxxxxxxxx41using Dates2tod = today()3tom = tod + Day(1)4yes = tod + Day(-1)

#### 34. How to get all the dates corresponding to the month of July 2016? (★★☆)

xxxxxxxxxx21using Dates2mon = collect(Date(2016,7,1):Day(1):Date(2016,7,31))

#### 35. How to compute ((A+B)*(-A/2)) in place (without copy)? (★★☆)

xxxxxxxxxx21A = 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 (★★☆)

xxxxxxxxxx51A = 10*rand(1,10)2Int64.(trunc.(A))3B = parse.(Int64,[split(string(x),".") for x in A])4Int64.(floor.(A))5Int64.(ceil.(A)) .- 1

#### 37. Create a 5x5 matrix with row values ranging from 0 to 4 (★★☆)

xxxxxxxxxx11a = rand(0:4,5,5)

#### 38. Consider a generator function that generates 10 integers and use it to build an array (★☆☆)

xxxxxxxxxx11a = collect(1:10)

#### 39. Create a vector of size 10 with values ranging from 0 to 1, both excluded (★★☆)

xxxxxxxxxx11a = rand(10)

#### 40. Create a random vector of size 10 and sort it (★★☆)

xxxxxxxxxx21a = rand(10,)2sort!(a)

#### 42. Consider two random array A and B, check if they are equal (★★☆)

xxxxxxxxxx11isequal(A,B)

#### 44. Consider a random 10x2 matrix representing cartesian coordinates, convert them to polar coordinates (★★☆)

x1a = 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#or6to_polar(x, y) = hypot(x, y) * cis(angle(x+im*y))7[to_polar(x...) for x in eachrow(a)] #or8[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 (★★☆)

xxxxxxxxxx21a = 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 (★★☆)

xxxxxxxxxx41mutable struct xy2x:Float643y:Float644end

#### 47. Given two arrays, X and Y, construct the Cauchy matrix C (Cij =1/(xi - yj)) (★★☆)

x1X = rand(5);Y = rand(5);2C = [1/(xi-yi) for xi in X, yi in Y]#or3C = 1 ./ (X .- Y')

#### 48. Print the minimum and maximum representable value for each Julia scalar type

xxxxxxxxxx21typemin()2typemax()

#### 49. How to print all the values of an array? (★★☆)

xxxxxxxxxx21print(a)2display(a)3foreach(println, a) #prints full array

#### 50. How to find the closest value (to a given scalar) in a vector? (★★☆)

x1a = rand(10)2x = 0.5 #given scalar3b = abs.(a .- x)4a[findmin(b)]

I will update remaining exercises in due course.