Quiz 1.6: Other Data Structures


Q1: Data structures theory

Which of the following sentences are correct?

RESOLUTION

A few notes concerning the incorrect sentences: tuples and named tuples being immutable can not be used in algorithms where a modification of an element is required (nor where an appending of elements is); tuples do NOT support linear algebra operations, only arrays do; delete! is not supported for arrays. Use deleteat! instead

The correct answers are:

  • "Arrays, tuples, dictionaries, named tuples, sets are all example of data structure to manage data offering different funcionalities"
  • "Tuples and named tuples can efficiently host heterogeneous types differently than arrays and dictionaries where hosting different types would result in less efficient code"
  • "Arrays, tuple, named tuples are data structures where the initial or insertion order is retained, contrary to dictionaries and sets"
  • "The function push! is supported for arrays, dictionaries and sets"
  • "The function collect returns an array of the data being \"collected\""

Q2: Conversion between data structures

Given the following code snippet:

a  = (a=1,b=2,c=3,d=4)
b  = Dict([k => v for (k,v) in pairs(a)] )
c1 = keys(b)
c2 = values(b)
d  = NamedTuple([k=>v for (k,v) in zip(c1,c2)])

Which is the value of d ?

RESOLUTION

The script converts the named tuple to a dictionary, to a pair of arrays and then back to the original named tuple. However the passage trough the dictionary make the order to be lost and the final named tuple has an order different than the original one.

The correct answers are:

  • A named tuple different than a

Q3: Date functions (1)

The following snippet computes the number of working hours (9-18, Mo-Fr) for the month of February 2030:

wHours = sum(18-9 for d in Date(?₁,?₂,?₃):Day(1):?₄(d -> Dates.day(d+Day(1)) == 1, Date(?₁,?₂,?₃)) if ?₅(d) in 1:5)

Where ?₁, ?₂ and ?₃ are numbers and ?₄ and ?₅ are names of functions.

Which are ?₁, ?₂ and ?₃ ? Type your answer in the form (?₁,?₂,?₃), e.g. (1,2,3) without spaces and without leading zeros.


RESOLUTION

We are interested in summing the hours of February 2030. Date(?₁,?₂,?₃) represents the beginning of our range of interest, so it is the 1st of February 2030}

The correct answer is: (2030,2,1)


Q4: Date functions (2)

The following snippet computes the number of working hours (9-18, Mo-Fr) for the month of February 2030:

wHours = sum(18-9 for d in Date(?₁,?₂,?₃):Day(1):?₄(d -> Dates.day(d+Day(1)) == 1, Date(?₁,?₂,?₃)) if ?₅(d) in 1:5)

Where ?₁, ?₂ and ?₃ are numbers and ?₄ and ?₅ are names of functions.

Which is ?₄ ? Type your answer with just the function name, without parhenthesis, e.g. println


RESOLUTION

Our objective it to select the last day of the range, that is the last day of the specific month requested. As we don't know a priori whether 2030 is a leap year or not, we use tonext until we find a day whose following day is the first of the month.

The correct answer is: tonext


Q5: Date functions (3)

The following snippet computes the number of working hours (9-18, Mo-Fr) for the month of February 2030:

wHours = sum(18-9 for d in Date(?₁,?₂,?₃):Day(1):?₄(d -> Dates.day(d+Day(1)) == 1, Date(?₁,?₂,?₃)) if ?₅(d) in 1:5)

Where ?₁, ?₂ and ?₃ are numbers and ?₄ and ?₅ are names of functions.

Which is ?₅ ? Type your answer with just the function name, without parhenthesis, e.g. println


RESOLUTION

Our objective it to count only working hours between Monday to Friday, and the function dayofweek allows us to filter only such days.

The correct answer is: dayofweek