update ch 2 exercises

This commit is contained in:
Bogumił Kamiński 2022-10-14 12:54:02 +02:00
parent ae41462776
commit 38398729ce

View File

@ -16,10 +16,32 @@ y[1] = 10
``` ```
What is the value of `x[1]` and why? What is the value of `x[1]` and why?
<details>
<summary>Solution</summary>
`x[1]` will be `10` because `y = x` is not copying data but it binds
the same value both to variable `x` and `y`.
</details>
### Exercise 2 ### Exercise 2
How can you type `⚡ = 1`. Check if this operation succeeds and what is its result. How can you type `⚡ = 1`. Check if this operation succeeds and what is its result.
<details>
<summary>Solution</summary>
In help mode (activated by `?`) copy-paste `⚡` to get:
```
help?> ⚡
"⚡" can be typed by \:zap:<tab>
```
After the `⚡ = 1` operation a new variable `⚡` is defined and it is bound
to value `1`.
</details>
### Exercise 3 ### Exercise 3
What will be the value of variable `x` after running of the following code and why? 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 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
<details> <details>
<summary>Solution</summary>
<summary>Show!</summary>
### 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:<tab>
```
After the `⚡ = 1` operation a new variable `⚡` is defined and it is bound
to value `1`.
### Exercise 3
`x` will have value `0.9999999999242748`. This value is below `1.0` because `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, 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 As you can see there is a difference at 17th place after decimal dot where we
have `4` vs `5`. have `4` vs `5`.
</details>
### Exercise 4 ### Exercise 4
Express the type `Matrix{Bool}` using `Array` type.
<details>
<summary>Solution</summary>
It is `Array{Bool, 2}`. You immediately get this information in REPL: It is `Array{Bool, 2}`. You immediately get this information in REPL:
``` ```
julia> Matrix{Bool} julia> Matrix{Bool}
Matrix{Bool} (alias for Array{Bool, 2}) Matrix{Bool} (alias for Array{Bool, 2})
``` ```
</details>
### Exercise 5 ### Exercise 5
Let `x` be a vector. Write code that prints an error if `x` is empty
(has zero elements)
<details>
<summary>Solution</summary>
You can do it like this: You can do it like this:
``` ```
length(x) == 0 && println("x is empty") 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")) isempty(x) && throw(ArgumentError("x is not allowed to be empty"))
``` ```
</details>
### Exercise 6 ### 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`.
<details>
<summary>Solution</summary>
Here are two ways to define the `exec` function: Here are two ways to define the `exec` function:
``` ```
exec1(x, y, op=+) = op(x, y) exec1(x, y, op=+) = op(x, y)
@ -144,15 +136,30 @@ julia> exec2(2, 3; op=*)
6 6
``` ```
</details>
### Exercise 7 ### Exercise 7
Write a function that calculates a sum of absolute values of values stored in
a collection passed to it.
<details>
<summary>Solution</summary>
Such a function can be written as: Such a function can be written as:
``` ```
sumabs(x) = sum(abs, x) sumabs(x) = sum(abs, x)
``` ```
</details>
### Exercise 8 ### Exercise 8
Write a function that swaps first and last element in an array in place.
<details>
<summary>Solution</summary>
This can be written for example as: This can be written for example as:
``` ```
function swap!(x) 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 is technically called tuple destructuring; we discuss it in later chapters of
the book) the book)
</details>
### Exercise 9 ### 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?
<details>
<summary>Solution</summary>
We used `@time` macro in chapter 1. We used `@time` macro in chapter 1.
Version in global scope: Version in global scope:
@ -246,8 +262,15 @@ julia> @time sum3loop(10^6)
``` ```
This is also much faster than a loop in global scope. This is also much faster than a loop in global scope.
</details>
### Exercise 10 ### Exercise 10
Explain the value of the result of summation obtained in exercise 9.
<details>
<summary>Solution</summary>
In exercise 9 we note that the result is `-8222430735553051648` which is a In exercise 9 we note that the result is `-8222430735553051648` which is a
negative value, although we are adding cubes of positive values. The negative value, although we are adding cubes of positive values. The
reason of the problem is that operations on integers overflow. If you reason of the problem is that operations on integers overflow. If you