### 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`

`1``import LinearAlgebra as la #or`

2`import LinearAgebra`

3`const la = LinearAlgebra`

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

`xxxxxxxxxx`

21`] `

2`status Javis`

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

`xxxxxxxxxx`

11`a = zeros(Int64,10)`

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

`xxxxxxxxxx`

21`sizeof(a)`

2`length(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`

21`a = zeros(Int,10)`

2`a[5] = 1`

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

`xxxxxxxxxx`

21`a = [10:49;]`

2`a = collect(10:49)`

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

`xxxxxxxxxx`

31`a[end:-1:1] #faster`

2`reverse(a)`

3`reverse!(a) #inplace`

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

`xxxxxxxxxx`

11`a = reshape(0:8,3,3)`

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

`xxxxxxxxxx`

31`findall(!isequal(0),a)`

2`findall(!iszero,a)`

3`findall(!=(0),a)`

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

`xxxxxxxxxx`

31`using LinearAlgebra`

2`a = I(3)`

3`a = Matrix(1.0I,3,3)`

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

`xxxxxxxxxx`

21`a = rand(3,3,3) #numbers in the range 0 to 1`

2`b = randn(3,3,3) #Gaussian random numbers`

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

`xxxxxxxxxx`

31`a = rand(10,10)`

2`minm = minimum(a)`

3`maxm = maximum(a)`

4`minm, maxm = extrema(a)`

#### 14. Create a random vector of size 30 and find the mean value

`xxxxxxxxxx`

31`using Statistics`

2`a = rand(30) #rand(30,)`

3`b = mean(a)`

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

`xxxxxxxxxx`

61`a = zeros(5,5)`

2`a[1,:] .= 1`

3`a[end,:] .= 1`

4`a[2:end-1,1] .= 1 `

5`a[2:end-1,end] .= 1`

6`a[:,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`

31`a = rand(5,5)`

2`b = zeros(Float64,size(a,1)+2,size(a,2)+2)# b = zeros(eltype(a), size(a) .+ 1)`

3`b[2:end-1,2:end-1] = a`

#### 17. What is the result of the following expression? (★☆☆)

`xxxxxxxxxx`

81` (0 * NaN) >= NaN : false`

2`NaN == NaN : false`

3`NaN > NaN : false`

4`NaN - NaN : NaN`

5`NaN in Set([NaN]) : true`

6`NaN in [NaN] : false`

7`0.3 == 3 * 0.1 : false #floating point rounding error`

8`isapprox(0.3 , 3 * 0.1) : true`

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

`xxxxxxxxxx`

11`a = Bidiagonal(zeros(5,),collect(1:4),:L)`

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

`xxxxxxxxxx`

31`a = zeros(Int64,8,8)`

2`a[1:2:end,1:2:end] .= 1`

3`a[2:2:end,2:2:end] .= 1 #or`

4`a = [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?

`x1``m = 100`

2`n,p,r = 6,7,8`

3`page = Int64(ceil(m/n/p))`

4`col = Int64(ceil((m-(page-1)*n*p)/n))`

5`row = Int64(ceil((m-(page-1)*n*p-(col-1)*n)/n))`

6```
```

7`CartesianIndices((6,7,8))[m] #builtin command`

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

`xxxxxxxxxx`

11`repeat([1 0;0 1],4,4)`

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

`xxxxxxxxxx`

31`a = randn(5,5)`

2`a = a .- minimum(a)`

3`a = a./maximum(a)`

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

`xxxxxxxxxx`

61`mutable struct color`

2` R::UInt8`

3` G::UInt8`

4` B::UInt8`

5` A::UInt8`

6`end`

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

`xxxxxxxxxx`

21`a = rand(5,3);b = rand(3,2)`

2`c = a*b`

#### 25. Given a 1D array, negate all elements which are between 3 and 8, in place. (★☆☆)

`xxxxxxxxxx`

41`a = 3;b = 8;`

2`x = rand(1:10,20) # collect(1:10)`

3`x[a .<= x .<= b] .= -x[a .<= x .<= b] #or`

4`x[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`

61`Z.^Z #valid`

2`2 << Z >> 2 #not valid`

3`Z <- Z # Z < (-Z)`

4`1im*Z #valid`

5`Z/1/1 #valid,converted to float vector`

6`Z<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`

5`Float64(Int64([NaN])) : MethodError`

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

`xxxxxxxxxx`

11`round.(-4.5, RoundNearestTiesAway)`

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

`xxxxxxxxxx`

11`intersect(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`

11`np.sqrt(-1) == np.emath.sqrt(-1)`

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

`xxxxxxxxxx`

41`using Dates`

2`tod = today()`

3`tom = tod + Day(1)`

4`yes = tod + Day(-1)`

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

`xxxxxxxxxx`

21`using Dates`

2`mon = collect(Date(2016,7,1):Day(1):Date(2016,7,31))`

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

`xxxxxxxxxx`

21`A = 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`

51`A = 10*rand(1,10)`

2`Int64.(trunc.(A))`

3`B = parse.(Int64,[split(string(x),".")[1] for x in A])`

4`Int64.(floor.(A))`

5`Int64.(ceil.(A)) .- 1`

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

`xxxxxxxxxx`

11`a = rand(0:4,5,5)`

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

`xxxxxxxxxx`

11`a = collect(1:10)`

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

`xxxxxxxxxx`

11`a = rand(10)`

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

`xxxxxxxxxx`

21`a = rand(10,)`

2`sort!(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`

11`isequal(A,B)`

#### 43. Make an array immutable (read-only) (★★☆)

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

x

1`a = rand(10,2);`

2`ang = angle.(a[:,1] .+ 1im*a[:,2])`

3`r = hypot.(a[:,1] .+ a[:,2])# sqrt.(a[:,1].^2 .+ a[:,2].^2)`

4`p = r.*cis.(ang) # r.*exp.(1im*ang) `

5`#or`

6`to_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`

21`a = rand(10)`

2`a[a .== maximum(a)] .= 0`

#### 46. Create a structured array with `x`

and `y`

coordinates covering the [0,1]x[0,1] area (★★☆)

`xxxxxxxxxx`

41`mutable struct xy`

2`x:Float64`

3`y:Float64`

4`end`

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

x

1`X = rand(5);Y = rand(5);`

2`C = [1/(xi-yi) for xi in X, yi in Y]#or`

3`C = 1 ./ (X .- Y')`

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

`xxxxxxxxxx`

21`typemin()`

2`typemax()`

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

`xxxxxxxxxx`

21`print(a)`

2`display(a)`

3`foreach(println, a) #prints full array`

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

x

1`a = rand(10)`

2`x = 0.5 #given scalar`

3`b = abs.(a .- x)`

4`a[findmin(b)[2]]`

I will update remaining exercises in due course.

## Comments

## Post a Comment