Skip to content

Commit

Permalink
Added first two quizzes (0.1 and 1.1) and updated env of OO_INTRO and…
Browse files Browse the repository at this point in the history
… 01_JULIA
  • Loading branch information
sylvaticus committed Mar 30, 2022
1 parent 2e108fc commit a4b7566
Show file tree
Hide file tree
Showing 201 changed files with 591 additions and 51 deletions.
8 changes: 4 additions & 4 deletions Manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ version = "0.8.6"

[[deps.Documenter]]
deps = ["ANSIColoredPrinters", "Base64", "Dates", "DocStringExtensions", "IOCapture", "InteractiveUtils", "JSON", "LibGit2", "Logging", "Markdown", "REPL", "Test", "Unicode"]
git-tree-sha1 = "2c023382ab49c40475fcf59b90ba1c8edd9ff45e"
git-tree-sha1 = "7d9a46421aef53cbd6b8ecc40c3dcbacbceaf40e"
uuid = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
version = "0.27.13"
version = "0.27.15"

[[deps.IOCapture]]
deps = ["Logging", "Random"]
Expand Down Expand Up @@ -68,9 +68,9 @@ uuid = "ca575930-c2e3-43a9-ace4-1e988b2c1908"

[[deps.Parsers]]
deps = ["Dates"]
git-tree-sha1 = "13468f237353112a01b2d6b32f3d0f80219944aa"
git-tree-sha1 = "85b5da0fa43588c75bb1ff986493443f821c70b7"
uuid = "69de0a69-1ddd-5017-9359-2bf0b02dc9f0"
version = "2.2.2"
version = "2.2.3"

[[deps.Printf]]
deps = ["Unicode"]
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -0,0 +1,263 @@
# Quiz 01.1 on Modules, packages and environments

```@setup q0001
cd(@__DIR__)
using Pkg
Pkg.activate(".")
## Pkg.resolve()
## Pkg.instantiate()
using Random
Random.seed!(123)
using QuizQuestions
```

--------------------------------------------------------------------------------
### Q1: What is printed ?

Given a file `foo.jl` with the following code:
```julia
println("A - I am in foo.jl")

module Foo

println("B - I am in module Foo in foo.jl")
export x

const x=1
const y=2
z() = println("C - I am in a function of module Foo")
end

module Foo2

println("D - I am in module Foo2 in foo.jl")
export a

const a=1
const b=2
c() = println("E - I am in a function of module Foo2")
end
```

Which print statements will appear after running the command `include(foo.jl)` ?

```@example q0001
choices = [ # hide
"All statements from `A` to `E`", # hide
"No statements will be printed (e.g. due to an error)", # hide
"Statement `A` only", # hide
"Statements `A`, `B` and `D` only", # hide
"Statements `A`, `C` and `E` only", # hide
"Statements `B` and `D` only", # hide
] # hide
answers = [2] # hide
multiq(choices, answers;) # hide
```

```@raw html
<details><summary>RESOLUTION</summary>
```
The `include(foo.jl)` statement will lead to an error because there are no quotes around the filename. If the quotations would have been included it would have resulted in the statements `A`, `B` and `D`. Statements `C` and `E` are within function definition and would occur only when functions `z` or `c` would have been called.}

The correct answer is:
- "No statements will be printed (e.g. due to an error)"
```@raw html
</details>
```

--------------------------------------------------------------------------------
### Q2: Inclusion of a module

Given a file `foo.jl` with the following code:

```julia
module Foo

export x

const x=1
const y=2
z() = println("Hello world!")
end
```

and the following sequence of commands:

```julia
include("foo.jl") # Command 1
x # Command 2
Foo.x # Command 3
using Foo # Command 4
using .Foo # Command 5
x # Command 6
Foo.z() # Command 7
```

Which statements are correct ?

```@example q0001
choices = [ # hide
"Command 1 is wrong at it should have been `include foo` (without the .jl file extension)", # hide
"Command 2 returns the value `1`", # hide
"Command 3 returns the value `1`", # hide
"Command 4 returns an `ArgumentError: Package Foo not found in current path:`", # hide
"Command 5 returns an `ArgumentError: Package Foo not found in current path:`", # hide
"Command 6 returns the value `1`", # hide
"Command 7 returns an `UndefVarError: z not defined`", # hide
] # hide
answers = [3,4,6] # hide
multiq(choices, answers;keep_order=true) # hide
```

```@raw html
<details><summary>RESOLUTION</summary>
```

The `include("foo.jl")` statement evaluates the included content, but it doesn't yet bring it into scope. You can't yet refer directly to the objects of the `Foo` module, you need to use the qualified name as in command 3. `Foo` is a module, not a package, so command 4 will complain that it doesn't find the "package" `Foo`. After the module has been bring to scope we can refer to `x` directly as in command 6. Command 7, as we are using the qualified name, is indipenden than whether `z` was exported by `Foo` or not, and hence it works, and would have been worked even without the `using .Foo` of command 5.

The correct answers are:
- "Command 3 returns the value `1`"
- "Command 4 returns an `ArgumentError: Package Foo not found in current path:`"
- "Command 6 returns the value `1`"

```@raw html
</details>
```

--------------------------------------------------------------------------------
### Q3: Submodules

Given a file `Foo.jl` with the following code:
```julia
module Foo
export x, plusOne
x = 1
plusOne(x) = x + 1
module Foo2
export plusTwo
plusTwo(x) = plusOne(x)+1
end
end
```

After including the file we try to run the command `Foo.Foo2.plusTwo(10)`. Which of the following statements is correct ?

```@example q0001
choices = [ # hide
"The result is 12", # hide
"The result is 3", # hide
"The result is an error that we can avoid if we run instead the command `Main.Foo.Foo2.plusTwo(10)`", # hide
"The result is an error that we can avoid if we type `using Foo` before that command", # hide
"The result is an error that we can avoid if we type `using .Foo` before that command", # hide
"The result is an error that we can avoid if the function `plusTwo` in module `Foo2` is defined as `plusTwo(x) = Foo.plusOne(x)+1`", # hide
"The result is an error that we can avoid if the function `plusTwo` in module `Foo2` is defined as `plusTwo(x) = Main.Foo.plusOne(x)+1`", # hide
"The result is an error that we can avoid if the function `plusTwo` in module `Foo2` is defined as `plusTwo(x) = ..Foo.plusOne(x)+1`", # hide
"The result is an error that we can avoid if in module `Foo2` the function `plusTwo` is preceded by the statement `using Foo`", # hide
"The result is an error that we can avoid if in module `Foo2` the function `plusTwo` is preceded by the statement `using .Foo`", # hide
" The result is an error that we can avoid if in module `Foo2` the function `plusTwo` is preceded by the statement `using ..Foo`", # hide
] # hide
answers = [7,11] # hide
multiq(choices, answers;) # hide
```

```@raw html
<details><summary>RESOLUTION</summary>
```
The given command results in a `UndefVarError: plusOne not defined`. Indeed even if `Foo2` is a submodule of `Foo`, it doesn't inherit the scope of parent modules. So its code can't find the 'plusOne' function. When in the REPL we run the command we are in the `Main` module. Adding `using .Foo` doesn't change anything, as the problem is in the scope of the `Foo2` module, not in those of the REPL (`Main` - and , of course, typing `using Foo` looks-up for the package `Foo`, not the module `Foo`, and would end in a `Package Foo not found` error. So what can we do? One solution is using in the `plusTwo` function the full path of the `plusOne` function: `plusTwo(x) = Main.Foo.plusOne(x)+1`. While this works, it may be a less portable solution, as it then requires module Foo to be a child of `Main`. Perhaps a better solution is to use a relative path and use the statement `using ..Foo` in module `Foo2` before the definition of `plusTwo` (trying to use a relative path directly in the function definition as in `plusTwo(x) = ..Foo.plusOne(x)+1` results in a parsing error)

The correct answers are:
- The result is an error that we can avoid if the function `plusTwo` in module `Foo2` is defined as `plusTwo(x) = Main.Foo.plusOne(x)+1`
- The result is an error that we can avoid if in module `Foo2` the function `plusTwo` is preceded by the statement `using ..Foo`
```@raw html
</details>
```

--------------------------------------------------------------------------------
### Q4: Submodules2

Given a module `Foo` with the following code:

```julia
module Foo
export x
x = 1
module Foo2
export plusTwo
plusTwo(x) = x+2
end
module Foo3
export plusThree
[XXXX]
plusThree(x) = plusTwo(x)+1
end
end
```

Which of the following statements are correct ?

```@example q0001
choices = [ # hide
"`[XXXX]` should be `using Main.Foo.Foo2` for the function `plusThree` to work", # hide
"`[XXXX]` should be `using Foo2` for the function `plusThree` to work", # hide
"`[XXXX]` should be `using .Foo2` for the function `plusThree` to work", # hide
"`[XXXX]` should be `using ..Foo2` for the function `plusThree` to work", # hide
"`[XXXX]` should be `import Main.Foo.Foo2` for the function `plusThree` to work", # hide
"`[XXXX]` should be `import Foo2` for the function `plusThree` to work", # hide
"`[XXXX]` should be `import .Foo2` for the function `plusThree` to work", # hide
"`[XXXX]` should be `import ..Foo2` for the function `plusThree` to work", # hide
] # hide
answers = [1,4] # hide
multiq(choices, answers;) # hide
```

```@raw html
<details><summary>RESOLUTION</summary>
```
The function `plusTwo` needs to access a function on a sibling module. So the module `Foo2` must be retrieved by going up to one level with the two dots and then naming the module, i.e. `using ..Foo2` or using the full module path `using Main.Foo.Foo2`. `import` statemens alone will not work as the `plusThree` function call the `plusTwo` function using the unqualified name, without prefixing the module, so the `plusThree` function name need to be exported.

The correct answers are:
- "`[XXXX]` should be `using Main.Foo.Foo2` for the function `plusThree` to work"
- "`[XXXX]` should be `using ..Foo2` for the function `plusThree` to work"
```@raw html
</details>
```

--------------------------------------------------------------------------------
### Q5: Reproducibility

Which elements do you have to provide to others to guarantee reproducibility of your results obtained with a Julia project?

```@example q0001
choices = [ # hide
"The input data of your analysis", # hide
"The full source code of the scripts you have used", # hide
"The content of the Julia user folder on the machine your code ran to produce the results (e.g. `/home/[username]/.julia` in Linux)", # hide
"The file `Manifest.toml` of the environment where your code ran to produce the results", # hide
"The file `Project.toml` of the environment where your code ran to produce the results", # hide
] # hide
answers = [1,2,4] # hide
multiq(choices, answers;) # hide
```

```@raw html
<details><summary>RESOLUTION</summary>
```
To provide replicable results, assuming a deterministic algorithm or one where the random seed generator has been fixed, we need to provide the input data, the source code and the 'Manifest.toml' file that describe the _exact_ version of all packages. The `Project.toml` file instead, when present, is used to describe in which conditions our scripts could be used (i.e. the _list_ and eventually _range_ of dependent packages), but not a _unique_ environment state. The information of the `Manifest.toml` (and, for Julia versions before 1.7, the Julia version itself, as this info was not encoded in the `Manifest.toml` file) is enougth, we don't need to provide the whole content of the user Julia folder.

The correct answers are:
- "The input data of your analysis"
- "The full source code of the scripts you have used"
- "The file `Manifest.toml` of the environment where your code ran to produce the results"
```@raw html
</details>
```
105 changes: 105 additions & 0 deletions lessonsSources/00_-_INTRO_-_Introduction_julia_ml/Manifest.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
# This file is machine-generated - editing it directly is not advised

julia_version = "1.7.2"
manifest_format = "2.0"

[[deps.Artifacts]]
uuid = "56f22d72-fd6d-98f1-02f0-08ddc0907c33"

[[deps.Base64]]
uuid = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"

[[deps.CompilerSupportLibraries_jll]]
deps = ["Artifacts", "Libdl"]
uuid = "e66e0078-7015-5450-92f7-15fbd957f2ae"

[[deps.DataAPI]]
git-tree-sha1 = "cc70b17275652eb47bc9e5f81635981f13cea5c8"
uuid = "9a962f9c-6df0-11e9-0e5d-c546b8b5ee8a"
version = "1.9.0"

[[deps.DataValueInterfaces]]
git-tree-sha1 = "bfc1187b79289637fa0ef6d4436ebdfe6905cbd6"
uuid = "e2d170a0-9d28-54be-80f0-106bbe20a464"
version = "1.0.0"

[[deps.InteractiveUtils]]
deps = ["Markdown"]
uuid = "b77e0a4c-d291-57a0-90e8-8db25a27a240"

[[deps.IteratorInterfaceExtensions]]
git-tree-sha1 = "a3f24677c21f5bbe9d2a714f95dcd58337fb2856"
uuid = "82899510-4779-5014-852e-03e436cf321d"
version = "1.0.0"

[[deps.Libdl]]
uuid = "8f399da3-3557-5675-b5ff-fb832c97cbdb"

[[deps.LinearAlgebra]]
deps = ["Libdl", "libblastrampoline_jll"]
uuid = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"

[[deps.Logging]]
uuid = "56ddb016-857b-54e1-b83d-db4d58db5568"

[[deps.Markdown]]
deps = ["Base64"]
uuid = "d6f4376e-aef5-505a-96c1-9c027394607a"

[[deps.Mustache]]
deps = ["Printf", "Tables"]
git-tree-sha1 = "bfbd6fb946d967794498790aa7a0e6cdf1120f41"
uuid = "ffc61752-8dc7-55ee-8c37-f3e9cdd09e70"
version = "1.0.13"

[[deps.OpenBLAS_jll]]
deps = ["Artifacts", "CompilerSupportLibraries_jll", "Libdl"]
uuid = "4536629a-c528-5b80-bd46-f80d51c5b363"

[[deps.OrderedCollections]]
git-tree-sha1 = "85f8e6578bf1f9ee0d11e7bb1b1456435479d47c"
uuid = "bac558e1-5e72-5ebc-8fee-abe8a469f55d"
version = "1.4.1"

[[deps.Printf]]
deps = ["Unicode"]
uuid = "de0858da-6303-5e67-8744-51eddeeeb8d7"

[[deps.QuizQuestions]]
deps = ["Markdown", "Mustache", "Random"]
git-tree-sha1 = "2d4a8bb41b9d032c59e7c5560e20d26fcdddb47b"
uuid = "612c44de-1021-4a21-84fb-7261cf5eb2d4"
version = "0.2.0"

[[deps.Random]]
deps = ["SHA", "Serialization"]
uuid = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"

[[deps.SHA]]
uuid = "ea8e919c-243c-51af-8825-aaa63cd721ce"

[[deps.Serialization]]
uuid = "9e88b42a-f829-5b0c-bbe9-9e923198166b"

[[deps.TableTraits]]
deps = ["IteratorInterfaceExtensions"]
git-tree-sha1 = "c06b2f539df1c6efa794486abfb6ed2022561a39"
uuid = "3783bdb8-4a98-5b6b-af9a-565f29a5fe9c"
version = "1.0.1"

[[deps.Tables]]
deps = ["DataAPI", "DataValueInterfaces", "IteratorInterfaceExtensions", "LinearAlgebra", "OrderedCollections", "TableTraits", "Test"]
git-tree-sha1 = "5ce79ce186cc678bbb5c5681ca3379d1ddae11a1"
uuid = "bd369af6-aec1-5ad0-b16a-f7cc5008161c"
version = "1.7.0"

[[deps.Test]]
deps = ["InteractiveUtils", "Logging", "Random", "Serialization"]
uuid = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[[deps.Unicode]]
uuid = "4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5"

[[deps.libblastrampoline_jll]]
deps = ["Artifacts", "Libdl", "OpenBLAS_jll"]
uuid = "8e850b90-86db-534c-a0d3-1478176c7d93"
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[deps]
QuizQuestions = "612c44de-1021-4a21-84fb-7261cf5eb2d4"
Loading

0 comments on commit a4b7566

Please sign in to comment.