티스토리 뷰

4_Loops

Loops

Reference

https://github.com/JuliaComputing/JuliaBoxTutorials/tree/master/introductory-tutorials/intro-to-julia (github : JuliaComputing/JuliaBoxTutorials/introductory-tutorials/intro-to-julia/)

Topics:

  1. while loops
  2. for loops
  3. Exercises

Series

while loops

Syntax:

<font color=green>while<\font> *condition*
    *loop body*
<font color=green>end<\font>

For example, we could use while to count or to iterate over an array.

In [1]:
n = 0
while n < 10
    n += 1
    println(n)
end
n
1
2
3
4
5
6
7
8
9
10
Out[1]:
10
In [2]:
myfriends = ["Ted", "Robyn", "Barney", "Lily", "Marshall"]

i = 1
while i <= length(myfriends)
    friend = myfriends[i]
    println("Hi $friend, it's great to see you!")
    i += 1
end
Hi Ted, it's great to see you!
Hi Robyn, it's great to see you!
Hi Barney, it's great to see you!
Hi Lily, it's great to see you!
Hi Marshall, it's great to see you!

for loops

Syntax:

<font color=green>for<\font> *var* <font color=green>in<\font> *loop iterable*
    *loop body*
<font color=green>end<\font>

We could use a for loop to generate the same results as either of the examples above:

In [3]:
for n in 1:10
    println(n)
end
1
2
3
4
5
6
7
8
9
10
In [4]:
myfriends = ["Ted", "Robyn", "Barney", "Lily", "Marshall"]

for friend in myfriends
    println("Hi $friend, it's great to see you!")
end
Hi Ted, it's great to see you!
Hi Robyn, it's great to see you!
Hi Barney, it's great to see you!
Hi Lily, it's great to see you!
Hi Marshall, it's great to see you!

Now let's use for loops to create some addition tables, where the value of every entry is the sum of its row and column indices.

First, we initialize an array with zeros.

In [5]:
m, n = 5, 5
A = fill(0, (m, n))
Out[5]:
5×5 Array{Int64,2}:
 0  0  0  0  0
 0  0  0  0  0
 0  0  0  0  0
 0  0  0  0  0
 0  0  0  0  0
In [6]:
for i in 1:m
    for j in 1:n
        A[i, j] = i + j
    end
end
A
Out[6]:
5×5 Array{Int64,2}:
 2  3  4  5   6
 3  4  5  6   7
 4  5  6  7   8
 5  6  7  8   9
 6  7  8  9  10

Here's some syntactic sugar for the same nested for loop

In [7]:
B = fill(0, (m, n))
Out[7]:
5×5 Array{Int64,2}:
 0  0  0  0  0
 0  0  0  0  0
 0  0  0  0  0
 0  0  0  0  0
 0  0  0  0  0
In [8]:
for i in 1:m, j in 1:n
    B[i, j] = i + j
end
B
Out[8]:
5×5 Array{Int64,2}:
 2  3  4  5   6
 3  4  5  6   7
 4  5  6  7   8
 5  6  7  8   9
 6  7  8  9  10

The more "Julia" way to create this addition table would have been with an array comprehension.

In [9]:
C = [i + j for i in 1:m, j in 1:n]
Out[9]:
5×5 Array{Int64,2}:
 2  3  4  5   6
 3  4  5  6   7
 4  5  6  7   8
 5  6  7  8   9
 6  7  8  9  10

Exercises

4.1

Loop over integers between 1 and 100 and print their squares.

In [10]:
n = 0
while n < 100
    n += 1
    print("$(n^2) ")
end
1 4 9 16 25 36 49 64 81 100 121 144 169 196 225 256 289 324 361 400 441 484 529 576 625 676 729 784 841 900 961 1024 1089 1156 1225 1296 1369 1444 1521 1600 1681 1764 1849 1936 2025 2116 2209 2304 2401 2500 2601 2704 2809 2916 3025 3136 3249 3364 3481 3600 3721 3844 3969 4096 4225 4356 4489 4624 4761 4900 5041 5184 5329 5476 5625 5776 5929 6084 6241 6400 6561 6724 6889 7056 7225 7396 7569 7744 7921 8100 8281 8464 8649 8836 9025 9216 9409 9604 9801 10000 
In [11]:
for n in 1:100
    print("$(n^2) ")
end
1 4 9 16 25 36 49 64 81 100 121 144 169 196 225 256 289 324 361 400 441 484 529 576 625 676 729 784 841 900 961 1024 1089 1156 1225 1296 1369 1444 1521 1600 1681 1764 1849 1936 2025 2116 2209 2304 2401 2500 2601 2704 2809 2916 3025 3136 3249 3364 3481 3600 3721 3844 3969 4096 4225 4356 4489 4624 4761 4900 5041 5184 5329 5476 5625 5776 5929 6084 6241 6400 6561 6724 6889 7056 7225 7396 7569 7744 7921 8100 8281 8464 8649 8836 9025 9216 9409 9604 9801 10000 

4.2

Add to the code above a bit to create a dictionary, squares that holds integers and their squares as key, value pairs such that

squares[10] == 100
In [12]:
squares1 = Dict(i => i^2 for i in 1:100)
Out[12]:
Dict{Int64,Int64} with 100 entries:
  68 => 4624
  2  => 4
  89 => 7921
  11 => 121
  39 => 1521
  46 => 2116
  85 => 7225
  25 => 625
  55 => 3025
  42 => 1764
  29 => 841
  58 => 3364
  66 => 4356
  59 => 3481
  8  => 64
  74 => 5476
  95 => 9025
  57 => 3249
  20 => 400
  90 => 8100
  14 => 196
  31 => 961
  78 => 6084
  70 => 4900
  33 => 1089
  ⋮  => ⋮
In [13]:
squares1[10] == 100
Out[13]:
true
In [14]:
squares2 = Dict()
n = 0
while n < 100
    n += 1
    squares2[n] = n^2
end
In [15]:
squares2
Out[15]:
Dict{Any,Any} with 100 entries:
  68 => 4624
  2  => 4
  89 => 7921
  11 => 121
  39 => 1521
  46 => 2116
  85 => 7225
  25 => 625
  55 => 3025
  42 => 1764
  29 => 841
  58 => 3364
  66 => 4356
  59 => 3481
  8  => 64
  74 => 5476
  95 => 9025
  57 => 3249
  20 => 400
  90 => 8100
  14 => 196
  31 => 961
  78 => 6084
  70 => 4900
  33 => 1089
  ⋮  => ⋮
In [16]:
squares2[10] == 100
Out[16]:
true
In [17]:
squares3 = Dict()
for n in 1:100
    squares3[n] = n^2
end
In [18]:
squares3
Out[18]:
Dict{Any,Any} with 100 entries:
  68 => 4624
  2  => 4
  89 => 7921
  11 => 121
  39 => 1521
  46 => 2116
  85 => 7225
  25 => 625
  55 => 3025
  42 => 1764
  29 => 841
  58 => 3364
  66 => 4356
  59 => 3481
  8  => 64
  74 => 5476
  95 => 9025
  57 => 3249
  20 => 400
  90 => 8100
  14 => 196
  31 => 961
  78 => 6084
  70 => 4900
  33 => 1089
  ⋮  => ⋮
In [19]:
squares3[10] == 100
Out[19]:
true

4.3

Use an array comprehension to create an an array that stores the squares for all integers between 1 and 100.

In [21]:
n_sq = [i^2 for i in 1:100]
println(n_sq)
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400, 441, 484, 529, 576, 625, 676, 729, 784, 841, 900, 961, 1024, 1089, 1156, 1225, 1296, 1369, 1444, 1521, 1600, 1681, 1764, 1849, 1936, 2025, 2116, 2209, 2304, 2401, 2500, 2601, 2704, 2809, 2916, 3025, 3136, 3249, 3364, 3481, 3600, 3721, 3844, 3969, 4096, 4225, 4356, 4489, 4624, 4761, 4900, 5041, 5184, 5329, 5476, 5625, 5776, 5929, 6084, 6241, 6400, 6561, 6724, 6889, 7056, 7225, 7396, 7569, 7744, 7921, 8100, 8281, 8464, 8649, 8836, 9025, 9216, 9409, 9604, 9801, 10000]

'Flux in Julia > Learning Julia (Intro_to_Julia)' 카테고리의 다른 글

05. Conditionals  (0) 2018.09.18
04. Loops (한글)  (0) 2018.09.15
03. Data structures (한글)  (0) 2018.09.13
03. Data structures  (0) 2018.09.13
02. Strings (한글)  (0) 2018.09.12
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
글 보관함