3.8 Julia - Exercises

I once heard that in chess you can get only as much as you give. I believe it is also true for programming (and most likely many other human activities).

So, here are some exercises that you may want to solve to get from this chapter as much as you can.

Note: Some readers probably will not solve the exercises. They will not want to (because of the waste of time) or will not be able to solve them (in that case my apology for the inappropriate difficulty level). Either way, I suggest you read the tasks’ descriptions and the solutions (and try to understand them). In those sections I may use, e.g. some language constructs that I will not explain again in the upcoming chapters.

3.8.1 Exercise 1

Imagine the following situation. You and your friends make a call to order out a pizza. You got only $50 and you are pretty hungry. But you got a dilemma, for exactly $50 you can either order 2 pizzas 30 cm in diameter each, or 1 pizza 45 cm in diameter. Which one is more worth it?

Hint: Assume that the pizza is flat and that you are eating its surface.

Hint: You may want to search the documentation for Base.MathConstants and use one of them.

3.8.2 Exercise 2

When we talked about float comparisons (Section 3.3.3) we said to be careful since

(0.1 * 3) == 0.3

false

Write a function with the following signature areApproxEqual(f1::Float64, f2::Float64)::Bool. It should return true when called with those numbers (areApproxEqual(0.1*3, 0.3)). For the task you may use round with a precision of, let’s say, 16 digits.

Note: Probably there is no point of greater precision than 16 digits since your machine won’t be able to see it anyway. For technical details see Base.eps.

3.8.3 Exercise 3

Remember getMin from previous chapter (see Section 3.5.2)

function getMin(vect::Vector{Int}, isSortedAsc::Bool)::Int
    return isSortedAsc ? vect[1] : sort(vect)[1]
end

Write getMax with the following signature getMax(vect::Vector{Int}, isSortedDesc::Bool)::Int use only the elements from previous version of the function (you should modify them).

3.8.4 Exercise 4

Someone once told me that the simplest interview question for a candidate programmer is fizz buzz. If a person doesn’t know how to do that there is no point of examining them further.

I don’t know if that’s true, but here we go.

Write a program for a range of numbers 1 to 30.

If you feel stuck right now, don’t worry. It sounds difficult, because so far you haven’t met all the necessary elements to solve it. Still, I believe you can do this by reading the Julia’s docs or using your favorite web search engine.

Here are some constructs that might be useful to solve this task:

You may use some or all of them. Or perhaps you can come up with something else. Good luck.

3.8.5 Exercise 5

I once heard a story about chess.

According to the story the game was created by a Hindu wise man. He presented the invention to his king who was so impressed that he offered to fulfill his request as a reward.

A laughingly small request, thought the king. Or is it?

Use Julia to answer how many grains are on the last (64th) field.

Hint. If you get a strange looking result, use BigInt data type instead of Int.

3.8.6 Exercise 6

Lastly, to cool down a little write a function getInit that takes a vector of any type as an argument and returns the vector without its last element.

You may either use the generics (preferred way to solve it, see Section 3.4.2) or write the function without type declarations (acceptable solution).

Remember about the indexing (see Section 3.3.6). Think (or search for the answer e.g. in the internet) how to get one but last element of an array.

Usage examples:

getInit([1, 2, 3, 4])
# output: [1, 2, 3]
getInit(["ab", "cd", "ef", "gh"])
# output: ["ab", "cd", "ef"]
getInit([3.3])
# output: Float64[]
getInit([])
# output: Any[]


CC BY-NC-SA 4.0 Bartlomiej Lukaszuk