diff --git a/exercises/exercises02.md b/exercises/exercises02.md index c2ac52d..44e35e7 100644 --- a/exercises/exercises02.md +++ b/exercises/exercises02.md @@ -16,10 +16,32 @@ y[1] = 10 ``` What is the value of `x[1]` and why? +
+Solution + +`x[1]` will be `10` because `y = x` is not copying data but it binds +the same value both to variable `x` and `y`. + +
+ + ### Exercise 2 How can you type `⚡ = 1`. Check if this operation succeeds and what is its result. +
+Solution + +In help mode (activated by `?`) copy-paste `⚡` to get: +``` +help?> ⚡ +"⚡" can be typed by \:zap: +``` +After the `⚡ = 1` operation a new variable `⚡` is defined and it is bound +to value `1`. + +
+ ### Exercise 3 What will be the value of variable `x` after running of the following code and why? @@ -31,62 +53,8 @@ end x /= 1_000_000 ``` -### Exercise 4 - -Express the type `Matrix{Bool}` using `Array` type. - -### Exercise 5 - -Let `x` be a vector. Write code that prints an error if `x` is empty -(has zero elements) - -### Exercise 6 - -Write a function called `exec` that takes two values `x` and `y` and a function -accepting two arguments, call it `op` and returns `op(x, y)`. Make `+` to be -the default value of `op`. - -### Exercise 7 - -Write a function that calculates a sum of absolute values of values stored in -a collection passed to it. - -### Exercise 8 - -Write a function that swaps first and last element in an array in place. - -### Exercise 9 - -Write a loop in global scope that calculates the sum of cubes of numbers from -`1` to `10^6`. Next use the `sum` function to perform the same computation. -What is the difference in timing of these operations? - -### Exercise 10 - -Explain the value of the result of summation obtained in exercise 9. - -# Solutions -
- -Show! - -### Exercise 1 - -`x[1]` will be `10` because `y = x` is not copying data but it binds -the same value both to variable `x` and `y`. - -### Exercise 2 - -In help mode (activated by `?`) copy-paste `⚡` to get: -``` -help?> ⚡ -"⚡" can be typed by \:zap: -``` -After the `⚡ = 1` operation a new variable `⚡` is defined and it is bound -to value `1`. - -### Exercise 3 +Solution `x` will have value `0.9999999999242748`. This value is below `1.0` because representation of `1/7` using `Float64` type is less than rational number 1/7, @@ -105,16 +73,31 @@ julia> 1/big(7) # construct high-precision float directly As you can see there is a difference at 17th place after decimal dot where we have `4` vs `5`. +
+ ### Exercise 4 +Express the type `Matrix{Bool}` using `Array` type. + +
+Solution + It is `Array{Bool, 2}`. You immediately get this information in REPL: ``` julia> Matrix{Bool} Matrix{Bool} (alias for Array{Bool, 2}) ``` +
+ ### Exercise 5 +Let `x` be a vector. Write code that prints an error if `x` is empty +(has zero elements) + +
+Solution + You can do it like this: ``` length(x) == 0 && println("x is empty") @@ -127,8 +110,17 @@ passed as an argument to the function): isempty(x) && throw(ArgumentError("x is not allowed to be empty")) ``` +
+ ### Exercise 6 +Write a function called `exec` that takes two values `x` and `y` and a function +accepting two arguments, call it `op` and returns `op(x, y)`. Make `+` to be +the default value of `op`. + +
+Solution + Here are two ways to define the `exec` function: ``` exec1(x, y, op=+) = op(x, y) @@ -144,15 +136,30 @@ julia> exec2(2, 3; op=*) 6 ``` +
+ ### Exercise 7 +Write a function that calculates a sum of absolute values of values stored in +a collection passed to it. + +
+Solution + Such a function can be written as: ``` sumabs(x) = sum(abs, x) ``` +
+ ### Exercise 8 +Write a function that swaps first and last element in an array in place. + +
+Solution + This can be written for example as: ``` function swap!(x) @@ -184,8 +191,17 @@ Note the differences in the code: is technically called tuple destructuring; we discuss it in later chapters of the book) +
+ ### Exercise 9 +Write a loop in global scope that calculates the sum of cubes of numbers from +`1` to `10^6`. Next use the `sum` function to perform the same computation. +What is the difference in timing of these operations? + +
+Solution + We used `@time` macro in chapter 1. Version in global scope: @@ -246,8 +262,15 @@ julia> @time sum3loop(10^6) ``` This is also much faster than a loop in global scope. +
+ ### Exercise 10 +Explain the value of the result of summation obtained in exercise 9. + +
+Solution + In exercise 9 we note that the result is `-8222430735553051648` which is a negative value, although we are adding cubes of positive values. The reason of the problem is that operations on integers overflow. If you