/
ThinkJulia.jl
221 lines (183 loc) · 4.3 KB
/
ThinkJulia.jl
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# Think Julia
# Furuitful Functions
# recursion
function countdown(n)
if n ≤ 0
println("Blastoff!")
else
print(n, " ")
countdown(n - 1)
end
end
countdown(10)
# Exercise 6-5
function ack(m, n)
if m == 0
return n + 1
elseif m > 0 && n == 0
return ack(m - 1, 1)
elseif m > 0 && n > 0
tmp = ack(m, n - 1)
return ack(m - 1, tmp)
end
end
@time ack(3, 4)
@time ack(3, 6)
for i = 1:15 # n=15程度でオーバーフロー
@show i
@time ack(3, i)
end
for j = 1:15 # mはかなりちいさな値でもオーバーフローする
@show j
@time ack(j, 3)
end
# Exercise 6-7
"""
ispower(a, b)
Calculate a power, b, of a.
"""
function ispower(a::Int64, b::Int64)
dvd = a / b
if (dvd % b) == 0
print(b, " is a power of ", a, " and a power is ", convert(Int64, dvd))
return true
else
print(b, " is NOT a power of ", a)
return false
end
end
ispower(9, 2)
# Exercise 6-8
function gcd(a, b)
### This function remains to be coded
end
# 7. Iteration
function mysqrt(x, a, ε = 1e-10)
# x is initial value.
# a is a value to figure out its square root.
while true
# println(x)
y = (x + a / x) / 2
if abs(y - x) < ε
return y
break
end
x = y
end
end
mysqrt(10, 2) |> typeof
# Exercise 7-2
using DataFrames
function testsqareroot(upto)
# define sqrt function
function mysqrt(x, a, ε = 1e-10)
# x is initial value.
# a is a value to figure out its square root.
while true
y = (x + a / x) / 2
if abs(y - x) < ε
return y
break
end
x = y
end
end
# initialize
a = [1.0:1.0:upto;]
mysqrt_vec = zeros(length(a))
sqrt_vec = zeros(length(a))
diff_vec = zeros(length(a))
init = upto
@. mysqrt_vec = mysqrt(init, a)
@. sqrt_vec = sqrt(a)
@. diff_vec = mysqrt_vec - sqrt_vec
res = DataFrame(a = a, mysqrt = mysqrt_vec, sqrt = sqrt_vec, diff = diff_vec)
@show res
return res
end
test = testsqareroot(20)
# Exercise 7-3
Meta.parse("sqrt(π)")
function evalloop(eval_expr)
flag = true
while (flag)
Meta.parse(eval_expr)
print("Have you be satisfied with this iteration? If so, enter 'done'.")
txt = readline()
if (txt == "done")
flag = false
end
end
end
evalloop("1+3")
# 8. Strings
fruit = "🍌"
fruit |> typeof
fruit[1]
fruit[2] # Error
sizeof(fruit)
fruit = "banana"
fruit[2]
fruit[end]
sizeof(fruit)
fruits = "🍌🍎🍐"
fruits |> sizeof
fruits[1]
fruits[5]
fruits[4]
# Traversal
# スタート地点から順番になにかの文字を取得して,何かを実行し,最後までそれを続けること。そのようなprocessingがtraversalである。
# 数値的なインデックスを使うのか,それともベクトルの要素を取るのかで,書き方がちょっと異なる。
index = firstindex(fruits)
while index <= sizeof(fruits)
letter = fruits[index]
println(letter)
global index = nextind(fruits, index) # ここでグローバルを上書きしておく。
end
# さらに簡単なVer
for letter in fruits
println(letter)
end
# String Slice
str = "Julius Caesar";
str[1:6]
str[:]
"1 + 2 = $(1 + 2)"
# String Library
uppercase("Hello, World")
lowercase("ABCDSkofkofe")
findfirst("a", "banana")
findfirst("na", "banana")
findnext("na", "banana", 4)
# The ∈ Operator
# Returns `true` if first arg appears in the second.
'a' ∈ "banana"
function inboth(word1, word2)
for letter in word1
if letter ∈ word2
print(letter, " ")
end
end
end
inboth("apples", "oranges")
# 9. Case Study
# 10. Arrays
# Array of Arraysの形で生成できる。
["spam", 2.0, 5, [10, 20]]
# 配列のマッチ `∈`
[1, 2, 3, 4, 5, 6] ∈ 2
2 ∈ [1, 2, 3, 4, 5, 6]
[1, 2] ∈ [1, 2, 3, 4, 5, 6] # これはだめ
map(i -> in(i, [1, 2, 3, 4, 5, 6]), [1, 2, 10]) # これならいける。
# `eachindex`で配列の要素の位置をすべて取得できる。
random = randn(10)
eachindex(random)
for i in eachindex(random)
random[i] = 2 * random[i]
end
Int.(eachindex(random))
# Array slice
print(random[3:end])
# Array Library
# push! and append!
# sort!