# 100 Julia exercises with solutions

**URL:** <https://discourse.julialang.org/t/100-julia-exercises-with-solutions/78580>\
**Category:** Teaching & Outreach\
**Tags:** question, tutorials\
**Created:** [March 28, 2022, 1:36am UTC](https://discourse.julialang.org/t/100-julia-exercises-with-solutions/78580 "2022-03-28T01:36:15Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![VinodV](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vinodv/32/32733_2.png) [@VinodV](https://discourse.julialang.org/u/VinodV)\
**Post date:** [March 28, 2022, 1:36am UTC](https://discourse.julialang.org/t/100-julia-exercises-with-solutions/78580/1 "2022-03-28T01:36:15Z")

</div>

Here is a Julia version of exercises with solutions

[100 Julia exercises with solutions](https://pythonjulia.blogspot.com/2022/03/100-julia-exercises-with-solutions.html)

which is based on the exercises given here: [https://github.com/rougier/numpy-100](https://github.com/rougier/numpy-100)

You are welcome with alternate and/or efficient solutions for the solutions to the exercises. Feel free to suggest your solutions on comments here/blog comments.

I have attempted the first 50 qns. Remaining qns will be attended in due course.

---

<div class="post-metadata">

**Author:** ![albheim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albheim/32/34660_2.png) [@albheim](https://discourse.julialang.org/u/albheim)\
**Post date:** [March 28, 2022, 5:23am UTC](https://discourse.julialang.org/t/100-julia-exercises-with-solutions/78580/2 "2022-03-28T05:23:56Z")

</div>

For exercise 10 it seems like you check for not equal to one instead of zero?

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

```julia
findall(!isequal(1),a)

```

---

<div class="post-metadata">

**Author:** ![VinodV](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vinodv/32/32733_2.png) [@VinodV](https://discourse.julialang.org/u/VinodV)\
**Post date:** [March 28, 2022, 6:21am UTC](https://discourse.julialang.org/t/100-julia-exercises-with-solutions/78580/3 "2022-03-28T06:21:35Z")

</div>

`findall(!isequal(0),a)` would be the correct command. I will change accordingly.

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [March 28, 2022, 7:46am UTC](https://discourse.julialang.org/t/100-julia-exercises-with-solutions/78580/4 "2022-03-28T07:46:23Z")

</div>

`!iszero` does the same thing.

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [March 28, 2022, 7:59am UTC](https://discourse.julialang.org/t/100-julia-exercises-with-solutions/78580/5 "2022-03-28T07:59:08Z")

</div>

> #### 8. Reverse a vector (first element becomes last)
> 
> 1 a[end:-1:1]  
> 2 reverse(a)

Either the question should be more like

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

Or the answer should be

```julia
reverse!(a)

```

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [March 28, 2022, 8:32am UTC](https://discourse.julialang.org/t/100-julia-exercises-with-solutions/78580/6 "2022-03-28T08:32:52Z")

</div>

> #### 14. Create a random vector of size 30 and find the mean value
> 
> 1 using Statistics  
> 2 a = rand(1,30)

This is a 1x30 Matrix. A `Vector` of size 30 would be:

```julia
a = rand(30)

```

So, answer depends on how stringent you want to be with the term “vector”.

---

<div class="post-metadata">

**Author:** ![VinodV](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vinodv/32/32733_2.png) [@VinodV](https://discourse.julialang.org/u/VinodV)\
**Post date:** [March 28, 2022, 2:01pm UTC](https://discourse.julialang.org/t/100-julia-exercises-with-solutions/78580/7 "2022-03-28T14:01:26Z")

</div>

> [@oheil](#):
>
> Return the reverse of a vector

Changed.

---

<div class="post-metadata">

**Author:** ![VinodV](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vinodv/32/32733_2.png) [@VinodV](https://discourse.julialang.org/u/VinodV)\
**Post date:** [March 28, 2022, 2:01pm UTC](https://discourse.julialang.org/t/100-julia-exercises-with-solutions/78580/8 "2022-03-28T14:01:44Z")

</div>

Updated the solution

---

<div class="post-metadata">

**Author:** ![VinodV](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vinodv/32/32733_2.png) [@VinodV](https://discourse.julialang.org/u/VinodV)\
**Post date:** [March 28, 2022, 2:02pm UTC](https://discourse.julialang.org/t/100-julia-exercises-with-solutions/78580/9 "2022-03-28T14:02:26Z")

</div>

Included in the answer.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [March 28, 2022, 2:36pm UTC](https://discourse.julialang.org/t/100-julia-exercises-with-solutions/78580/10 "2022-03-28T14:36:29Z")

</div>

> [@VinodV](#):
>
> You are welcome with alternate and/or efficient solutions for the solutions to the exercises. Feel free to suggest your solutions on comments here/blog comments.

It would be easier to submit pull requests (if this were on github).

For problem 1, you can just do `import LinearAlgebra as la` these days.

For 7, you can simply do `a = [10:49;]`

For 10, it’s probably more natural to use `!=(0)` than `!isequal(0)`.

For 11, you can do `I(3)`.

For 15, it would be simpler to just assign the corners twice, e.g. `a[:,1] .= 1` instead of `a[2:end-1,1] .= 1`. (The performance difference is negligible.)

For 16, I would do `b = zeros(eltype(a), size(a) .+ 1)`

For 20, you can do `CartesianIndices((6,7,8))[100]`

Exercise 22 seems ambiguous. Often in linear algebra, “normalize” would mean dividing by some norm, but that’s not what you’re doing here.

For exercise 24 I wouldn’t make it mutable, which would be a bad idea for efficiency of storing arrays of that type.

For 25, I would use `x[a .<= x .<= b] .*= -1`.

For 27, I would say “valid” rather than “legal”. (No one is going to arrest you if you throw an exception.)

For 28, the analogue of the numpy exercise would be `[0] ./ [0]` and `[0] .÷ [0]`, not `[0] / [0]` and `[0] // [0]`.

For 35, you could use `@. A = (A+B)*(-A/2)` to compute it in-place and store the result in `A`.

For 36, I wouldn’t call `Int64.`. If you call `trunc.(...)` the results are already integer-valued. (Even if they are stored as floating-point numbers, they are still integers.)

I don’t know why you have all the trailing commas in `a = rand(10,)` etcetera; it’s not idiomatic.

For 44, I would use `hypot` to compute the radius, and `cis(x)` instead of `exp(im*x)`.

---

<div class="post-metadata">

**Author:** ![VinodV](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vinodv/32/32733_2.png) [@VinodV](https://discourse.julialang.org/u/VinodV)\
**Post date:** [March 28, 2022, 4:41pm UTC](https://discourse.julialang.org/t/100-julia-exercises-with-solutions/78580/11 "2022-03-28T16:41:59Z")

</div>

> [@stevengj](#):
>
> hypot

Fantastic review. I have updated my blog considering your feedback.

---

<div class="post-metadata">

**Author:** ![AndiMD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andimd/32/6531_2.png) [@AndiMD](https://discourse.julialang.org/u/AndiMD)\
**Post date:** [March 28, 2022, 5:00pm UTC](https://discourse.julialang.org/t/100-julia-exercises-with-solutions/78580/12 "2022-03-28T17:00:22Z")

</div>

Nice work! Small typo: Exercise 33 should read Date **s**.Day(1)

---

<div class="post-metadata">

**Author:** ![VinodV](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vinodv/32/32733_2.png) [@VinodV](https://discourse.julialang.org/u/VinodV)\
**Post date:** [March 29, 2022, 12:29am UTC](https://discourse.julialang.org/t/100-julia-exercises-with-solutions/78580/13 "2022-03-29T00:29:31Z")

</div>

corrected. Actually Dates prefix is not a must.

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [March 29, 2022, 12:49am UTC](https://discourse.julialang.org/t/100-julia-exercises-with-solutions/78580/14 "2022-03-29T00:49:33Z")

</div>

A few suggestions to add to those made above:

- You frequently use a trailing comma in function calls, for example in `rand(10,)`. This comma isn’t necessary. Usually you would see it written `rand(10)`.
- You frequently vectorize code in the solutions (presumably coming from python, where it is needed for performance). Vectorizing is not considered best practice in julia, because it often involves allocation of unnecessary intermediate arrays. In python that allocation is worth the speed gain, but since julia is already fast allocating unnecessarily only slows it down.
- Some more notes on specific exercises:

1. `extrema` will find both numbers in one pass.

```julia
minm, maxm = extrema(a)

```

1. An array comprehension would work nicely here:

```julia
a = [Int(isodd(i+j)) for i in 1:8, j in 1:8]

```

1. We can make this more general (meaning for any number of dimensions) and also more “julian” by writing the following function:

```julia
function to_cartesian_ind(shape, linear_ind)
    map(shape) do l 
        linear_ind, cartesian_ind = fldmod1(linear_ind, l)
        cartesian_ind
    end
end

to_cartesian_ind((6,7,8), 100) # returns (4,3,3)

```

Using `fldmod1` is a nice trick here to get the index in the current dimension (with `mod1`, which has range (0, n]), and the remainder for the next iteration in a single line.

1. Again, don’t vectorize. It is helpful to use a helper function here:

```julia
to_polar(x, y) = hypot(x, y) * cis(atan(y, x))

[to_polar(x...) for x in eachrow(a)] # the "splat" operator `...` turns the 2-vector x into two separate numbers for `to_polar`.

# equivalent:
[to_polar(a[i, 1], a[i, 2]) for i in axes(a, 1)]  

```

Technically, this way (and yours) might not be in the spirit of the exercise, since the output doesn’t match the input (it’s a vector of complex numbers instead of a matrix). If we wanted a matrix with `(r, θ)` rows instead, we would have to go about things a bit differently (could be a useful exercise to try to do that as well!)

1. Using a comprehension here is nice! you could also take advantage of broadcasting, which takes into account the shape of the inputs (not just their length)

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

```

1. technically, this doesn’t print _all the values_ in the array. It prints as many as fit in the terminal (or other output). To print them all, you could do, for example, `foreach(println, a)`.

2. `argmin` with a function as the first argument will return the value that minimizes the function. If we take `abs(y-x)` as that function, we will get the value closest to `x`. So you could do:

```julia
argmin(y -> abs(y - x), a)

```

---

<div class="post-metadata">

**Author:** ![VinodV](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vinodv/32/32733_2.png) [@VinodV](https://discourse.julialang.org/u/VinodV)\
**Post date:** [March 29, 2022, 3:57pm UTC](https://discourse.julialang.org/t/100-julia-exercises-with-solutions/78580/15 "2022-03-29T15:57:30Z")

</div>

argmin(y → abs(y - x), a)  
ERROR: MethodError: no method matching argmin(::var"#11#12", ::Vector{Float64})  
Closest candidates are:  
argmin(::Any) at array.jl:2346  
Stacktrace:  
[1] top-level scope  
@ REPL[15]:1

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [March 29, 2022, 6:38pm UTC](https://discourse.julialang.org/t/100-julia-exercises-with-solutions/78580/16 "2022-03-29T18:38:59Z")

</div>

True, this method was added in julia 1.6

---

<div class="post-metadata">

**Author:** ![RoyiAvital](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/royiavital/32/571_2.png) [@RoyiAvital](https://discourse.julialang.org/u/RoyiAvital)\
**Post date:** [March 29, 2022, 10:58pm UTC](https://discourse.julialang.org/t/100-julia-exercises-with-solutions/78580/17 "2022-03-29T22:58:51Z")

</div>

This is so nice.  
Thought it was a great opportunity to take the ride and try to use `Literate.jl` to do the work of publishing.  
So I started a small project at [Julia 100 Exercises](https://github.com/RoyiAvital/Julia100Exercises).  
So everything is in a Julia file (`.jl`).

---

<div class="post-metadata">

**Author:** ![VinodV](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vinodv/32/32733_2.png) [@VinodV](https://discourse.julialang.org/u/VinodV)\
**Post date:** [March 30, 2022, 2:01am UTC](https://discourse.julialang.org/t/100-julia-exercises-with-solutions/78580/18 "2022-03-30T02:01:01Z")

</div>

Thats nice. Happy learning Julia

---

<div class="post-metadata">

**Author:** ![RoyiAvital](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/royiavital/32/571_2.png) [@RoyiAvital](https://discourse.julialang.org/u/RoyiAvital)\
**Post date:** [April 5, 2022, 8:08pm UTC](https://discourse.julialang.org/t/100-julia-exercises-with-solutions/78580/19 "2022-04-05T20:08:43Z")

</div>

I have finished all 100 questions in [Julia 100 Exercises](https://github.com/RoyiAvital/Julia100Exercises).  
It was a great experience to work with `Literate.jl`.

---

<div class="post-metadata">

**Author:** ![VinodV](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vinodv/32/32733_2.png) [@VinodV](https://discourse.julialang.org/u/VinodV)\
**Post date:** [June 19, 2022, 1:44pm UTC](https://discourse.julialang.org/t/100-julia-exercises-with-solutions/78580/20 "2022-06-19T13:44:28Z")

</div>

[https://forem.julialang.org/vinodv/100-julia-exercises-2o3o](https://forem.julialang.org/vinodv/100-julia-exercises-2o3o)  
I have cross posted to julia forem.
