From 99457be917dec6bbbc8116db7a21d55b6fcda8a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bogumi=C5=82=20Kami=C5=84ski?= Date: Wed, 29 Dec 2021 16:08:09 +0100 Subject: [PATCH] add solutions to exercises codes --- README.md | 6 +++++ appB.jl | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 appB.jl diff --git a/README.md b/README.md index d6dd451..0f33921 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,12 @@ presented in the book please perform the following setup steps: The codes for each chapter are stored in files named *chXX.jl*, where *XX* is chapter number. +Solutions to the exercises that are presented in appendix B in +the book are stored in *appB.jl* file. These solutions assume that they are +executed in the same Julia session as the codes from the chapter where the +question was posted (so that appropriate variables and functions are defined +and appropriate packages are loaded). + To work with codes from some given chapter: * start a fresh Julia session using the `julia --project` command in a folder containing the downloaded material; diff --git a/appB.jl b/appB.jl new file mode 100644 index 0000000..372679d --- /dev/null +++ b/appB.jl @@ -0,0 +1,66 @@ +# Bogumił Kamiński, 2021 + +# Codes for appendix B + +# Code for exercise 3.1 + +using Statistics +using BenchmarkTools +aq = [10.0 8.04 10.0 9.14 10.0 7.46 8.0 6.58 + 8.0 6.95 8.0 8.14 8.0 6.77 8.0 5.76 + 13.0 7.58 13.0 8.74 13.0 12.74 8.0 7.71 + 9.0 8.81 9.0 8.77 9.0 7.11 8.0 8.84 + 11.0 8.33 11.0 9.26 11.0 7.81 8.0 8.47 + 14.0 9.96 14.0 8.1 14.0 8.84 8.0 7.04 + 6.0 7.24 6.0 6.13 6.0 6.08 8.0 5.25 + 4.0 4.26 4.0 3.1 4.0 5.39 19.0 12.50 + 12.0 10.84 12.0 9.13 12.0 8.15 8.0 5.56 + 7.0 4.82 7.0 7.26 7.0 6.42 8.0 7.91 + 5.0 5.68 5.0 4.74 5.0 5.73 8.0 6.89]; +@benchmark [cor($aq[:, i], $aq[:, i+1]) for i in 1:2:7] +@benchmark [cor(view($aq, :, i), view($aq, :, i+1)) for i in 1:2:7] + +# Code for exercise 3.2 + +function dice_distribution(dice1, dice2) + distribution = Dict{Int, Int}() + for i in dice1 + for j in dice2 + s = i + j + if haskey(distribution, s) + distribution[s] += 1 + else + distribution[s] = 1 + end + end + end + return distribution +end + +function test_dice() + all_dice = [[1, x2, x3, x4, x5, x6] + for x2 in 2:11 + for x3 in x2:11 + for x4 in x3:11 + for x5 in x4:11 + for x6 in x5:11] + + two_standard = dice_distribution(1:6, 1:6) + + for d1 in all_dice, d2 in all_dice + test = dice_distribution(d1, d2) + if test == two_standard + println(d1, " ", d2) + end + end +end + +test_dice() + + +# Code for exercise 3.3 + +plot(scatter(data.set1.x, data.set1.y, legend=false), + scatter(data.set2.x, data.set2.y, legend=false), + scatter(data.set3.x, data.set3.y, legend=false), + scatter(data.set4.x, data.set4.y, legend=false))