Skip to content

Commit

Permalink
Solutions to class exercise
Browse files Browse the repository at this point in the history
  • Loading branch information
mauro3 committed Sep 22, 2023
1 parent afdb2e6 commit 156464a
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 69 deletions.
2 changes: 1 addition & 1 deletion slide-notebooks/deploy_notebooks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ using Literate
## include Literate scripts starting with following 3 letters in the deploy
incl = "l1_"
## Set `sol=true` to produce output with solutions contained and hints stripts. Otherwise the other way around.
sol = false
sol = true
##

function replace_string(str)
Expand Down
12 changes: 6 additions & 6 deletions slide-notebooks/l1_3-julia-intro.jl
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ Furthermore, documentation can be gotten with `?xyz`
#src #########################################################################
#nb # %% A slide [markdown] {"slideshow": {"slide_type": "slide"}}
md"""
## Variables, assignments, and types
## Variables, assignments, and types [5min]
[https://docs.julialang.org/en/v1/manual/variables/](https://docs.julialang.org/en/v1/manual/variables/)
"""

Expand Down Expand Up @@ -284,7 +284,7 @@ Dict("a"=>1, "b"=>cos)
#src #########################################################################
#nb # %% A slide [markdown] {"slideshow": {"slide_type": "slide"}}
md"""
### Array exercises
## Array exercises [15min]
We will use arrays extensively in this course.
Expand Down Expand Up @@ -501,7 +501,7 @@ The rest about Arrays you will learn-by-doing.
#src #########################################################################
#nb # %% A slide [markdown] {"slideshow": {"slide_type": "slide"}}
md"""
## Control flow
## Control flow [10min]
Julia provides a variety of [control flow constructs](https://docs.julialang.org/en/v1/manual/control-flow/), of which we look at:
Expand Down Expand Up @@ -604,14 +604,14 @@ end
#src #########################################################################
#nb # %% A slide [markdown] {"slideshow": {"slide_type": "slide"}}
md"""
## Functions
## Functions [15min]
Functions can be defined in Julia in a number of ways. In particular there is one variant
more suited to longer definitions, and one for one-liners:
```
function f(a, b)
a * b
return a * b
end
f(a, b) = a * b
```
Expand Down Expand Up @@ -789,7 +789,7 @@ This is a key characteristic of the Julia package ecosystem.
#src #########################################################################
#nb # %% A slide [markdown] {"slideshow": {"slide_type": "slide"}}
md"""
## Modules and packages
## Modules and packages [5min]
Modules can be used to structure code into larger entities, and be used to divide it into
different name spaces. We will not make much use of those, but if interested see
Expand Down
91 changes: 60 additions & 31 deletions slide-notebooks/notebooks/l1_3-julia-intro.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@
{
"cell_type": "markdown",
"source": [
"## Variables, assignments, and types\n",
"## Variables, assignments, and types [5min]\n",
"[https://docs.julialang.org/en/v1/manual/variables/](https://docs.julialang.org/en/v1/manual/variables/)"
],
"metadata": {
Expand Down Expand Up @@ -478,7 +478,7 @@
{
"cell_type": "markdown",
"source": [
"### Array exercises\n",
"## Array exercises [15min]\n",
"\n",
"We will use arrays extensively in this course.\n",
"\n",
Expand Down Expand Up @@ -506,8 +506,8 @@
"cell_type": "code",
"source": [
"a = [2, 3]\n",
"b = ...\n",
"[ ; ]"
"b = [4, 5]\n",
"[a ; b]"
],
"metadata": {},
"execution_count": null
Expand All @@ -523,7 +523,8 @@
"outputs": [],
"cell_type": "code",
"source": [
"#"
"push!(b, 1)\n",
"push!(b, 3, 4)"
],
"metadata": {},
"execution_count": null
Expand All @@ -546,7 +547,7 @@
"outputs": [],
"cell_type": "code",
"source": [
"[ ; ]"
"[1:10; [4,5]]"
],
"metadata": {},
"execution_count": null
Expand All @@ -562,7 +563,7 @@
"outputs": [],
"cell_type": "code",
"source": [
"#"
"a = rand(3,3)"
],
"metadata": {},
"execution_count": null
Expand All @@ -585,7 +586,7 @@
"outputs": [],
"cell_type": "code",
"source": [
"a[ ... ], a[ ... ]"
"a[1,2], a[2,1]"
],
"metadata": {},
"execution_count": null
Expand All @@ -601,7 +602,7 @@
"outputs": [],
"cell_type": "code",
"source": [
"#"
"[ a[1,2], a[2,1] ]"
],
"metadata": {},
"execution_count": null
Expand Down Expand Up @@ -635,8 +636,8 @@
"outputs": [],
"cell_type": "code",
"source": [
"a[...]\n",
"a[..., ...]"
"a[end]\n",
"a[end, end]"
],
"metadata": {},
"execution_count": null
Expand All @@ -659,7 +660,7 @@
"outputs": [],
"cell_type": "code",
"source": [
"a[... , ...]"
"a[end, 1:end]"
],
"metadata": {},
"execution_count": null
Expand All @@ -675,7 +676,7 @@
"outputs": [],
"cell_type": "code",
"source": [
"a[ ]"
"a[1:2, 1:2]"
],
"metadata": {},
"execution_count": null
Expand Down Expand Up @@ -799,7 +800,7 @@
"outputs": [],
"cell_type": "code",
"source": [
"@assert ..."
"@assert a[1] == 99"
],
"metadata": {},
"execution_count": null
Expand Down Expand Up @@ -858,7 +859,10 @@
"outputs": [],
"cell_type": "code",
"source": [
"#"
"a = Int[]\n",
"push!(a, 1) ## works\n",
"push!(a, 1.0) ## works\n",
"push!(a, 1.5) ## errors as 1.5 cannot be converted to an Int"
],
"metadata": {},
"execution_count": null
Expand All @@ -875,7 +879,9 @@
"outputs": [],
"cell_type": "code",
"source": [
"#"
"a = []\n",
"push!(a, 5)\n",
"push!(a, \"a\")"
],
"metadata": {},
"execution_count": null
Expand All @@ -891,7 +897,7 @@
"outputs": [],
"cell_type": "code",
"source": [
"#"
"[1][1] = 1.5 ## errors"
],
"metadata": {},
"execution_count": null
Expand All @@ -915,7 +921,7 @@
"outputs": [],
"cell_type": "code",
"source": [
"#"
"a = Array{Any}(undef, 3, 3)"
],
"metadata": {},
"execution_count": null
Expand All @@ -931,7 +937,7 @@
"outputs": [],
"cell_type": "code",
"source": [
"#"
"size(a)"
],
"metadata": {},
"execution_count": null
Expand All @@ -953,7 +959,7 @@
{
"cell_type": "markdown",
"source": [
"## Control flow\n",
"## Control flow [10min]\n",
"\n",
"Julia provides a variety of [control flow constructs](https://docs.julialang.org/en/v1/manual/control-flow/), of which we look at:\n",
"\n",
Expand Down Expand Up @@ -999,7 +1005,14 @@
"outputs": [],
"cell_type": "code",
"source": [
"#"
"a = \"Where are the flowers\"\n",
"if startswith(a, \"Wh\")\n",
" b = \"Likely a question\"\n",
"elseif startswith(a, \"The\")\n",
" b = \"Likely a noun\"\n",
"else\n",
" b = \"no idea\"\n",
"end"
],
"metadata": {},
"execution_count": null
Expand Down Expand Up @@ -1031,7 +1044,7 @@
"outputs": [],
"cell_type": "code",
"source": [
"#"
"a > 5 ? \"really big\" : \"not so big\""
],
"metadata": {},
"execution_count": null
Expand Down Expand Up @@ -1063,6 +1076,14 @@
],
"metadata": {}
},
{
"cell_type": "markdown",
"source": [
"If `a < 0` evaluates to `true` then the bit after the `&&` is evaluated too,\n",
"i.e. an error is thrown. Otherwise, only `a < 0` is evaluated and no error is thrown."
],
"metadata": {}
},
{
"cell_type": "markdown",
"source": [
Expand Down Expand Up @@ -1101,14 +1122,14 @@
{
"cell_type": "markdown",
"source": [
"## Functions\n",
"## Functions [15min]\n",
"\n",
"Functions can be defined in Julia in a number of ways. In particular there is one variant\n",
"more suited to longer definitions, and one for one-liners:\n",
"\n",
"```\n",
"function f(a, b)\n",
" a * b\n",
" return a * b\n",
"end\n",
"f(a, b) = a * b\n",
"```\n",
Expand Down Expand Up @@ -1143,7 +1164,13 @@
"outputs": [],
"cell_type": "code",
"source": [
"#"
"function fn(a, b)\n",
" if a> b\n",
" return a\n",
" else\n",
" return b\n",
" end\n",
"end"
],
"metadata": {},
"execution_count": null
Expand All @@ -1169,7 +1196,8 @@
"outputs": [],
"cell_type": "code",
"source": [
"#"
"mymap(fn, a) = [fn(aa) for aa in a]\n",
"mymap(sin, 1:10)"
],
"metadata": {},
"execution_count": null
Expand All @@ -1195,7 +1223,7 @@
"outputs": [],
"cell_type": "code",
"source": [
"#"
"sin.(1:10)"
],
"metadata": {},
"execution_count": null
Expand All @@ -1212,7 +1240,7 @@
"outputs": [],
"cell_type": "code",
"source": [
"#"
"(1:10) .+ (1:10)'"
],
"metadata": {},
"execution_count": null
Expand All @@ -1236,7 +1264,8 @@
"outputs": [],
"cell_type": "code",
"source": [
"#"
"x,y = 0:0.1:pi, -pi:0.1:pi\n",
"sin.(x) .+ cos.(y')"
],
"metadata": {},
"execution_count": null
Expand Down Expand Up @@ -1264,7 +1293,7 @@
"outputs": [],
"cell_type": "code",
"source": [
"#"
"map(x -> sin(x) + cos(x), 1:10)"
],
"metadata": {},
"execution_count": null
Expand Down Expand Up @@ -1406,7 +1435,7 @@
{
"cell_type": "markdown",
"source": [
"## Modules and packages\n",
"## Modules and packages [5min]\n",
"\n",
"Modules can be used to structure code into larger entities, and be used to divide it into\n",
"different name spaces. We will not make much use of those, but if interested see\n",
Expand Down
Loading

0 comments on commit 156464a

Please sign in to comment.