Quiz 1.8: Functions
Q1: Function comparison
Given the following code snippet:
function foo(x)
list = []
for i in 2:x
cond = true
for j in list
if i%j == 0
cond = false
end
end
if cond push!(list,i); end
end
return list
end
function foo2(x)
if x == 1 return []; end
ns = foo2(x-1)
cond = true
for i in ns
if x%i == 0
cond = false
end
end
if cond push!(ns,x); end
return ns
end
Which of the following sentences are correct ?
RESOLUTION
The two functions compute the list of prime numbers up to x
, with the second one being recursive. While often recursive functions are slow, here the computations to derive the prime numbers is roughly the same in the two cases with a O(x²)
complexity bound, and there isn't a version that is definitly slower. Note that much more efficient approaches to find prime number exists.
The correct answers are:
- "None of the (other) sentences is correct"
Q2: Function optimisation (1)
Given the following code snippet:
function foo(x)
list = XXXX[]
for i in 2:x
cond = true
for j in list
if i%j == 0
cond = false
end
end
if cond push!(list,i); end
end
return list
end
To what XXXX
should be replaced to make the function more efficient?
RESOLUTION
The function can be made orders of magnitute more efficient by simply specifying that the list is an array of integers rather than "any" values.
The correct answer is: "Int64"
Q3: Function optimisation (2)
Given the following code snippet:
function foo(x)
list = []
for i in 2:x
cond = true
for j in list
if i%j == 0
cond = false
XXXX
end
end
if cond push!(list,i); end
end
return list
end
To what XXXX
should be replaced to make the function more efficient?
RESOLUTION
The function can be made many times more efficent by breaking the loop for that specific item once the first condition leading to cond=false
has been found.
The correct answer is: breack
Q4: Call by sharing
Given the following code snippet:
a = ([1,2,3],4)
function foo(x)
x[1][1] = 10
x[2] = 40
end
foo(a)
Which of the following sentences are correct ?
RESOLUTION
a
is a Tuple
, that is a immutable type. The attempt to modify it in the command x[2] = 40
rises an error during the JIT compilation. The first modification, however (x[1][1] = 10
), had already taken place.
The correct answers are:
- An error is raised during compilation/running
a
is now([10,2,3],4)
Q5: Call by sharing 2
Given the following code snippet:
a = ([1,2,3],4)
function foo(x)
x[1][1] = 10
x = ([10,20,30],40)
end
foo(a)
Which of the following sentences are true ?
RESOLUTION
a
is a Tuple
, that is a immutable type. The first command doesn't mutate it but mutates an object contained in it (that being an Array is mutable and doesn't create a problem). The second statement in the foo
function rebinds x
to a new object, without influencing the original object. Note that as the function mutate its parameter a more appropriate name would have been foo!
. Append the function name with an exclamation mark for mutating functions is hovewer only a convention.
The correct answers are:
a
is now([10,2,3],4)