Compare commits
2 Commits
2d9b2318a9
...
d91930f1f9
| Author | SHA1 | Date | |
|---|---|---|---|
| d91930f1f9 | |||
| 53d5f2878c |
52
exercises/ch04/ex4_9.lua
Normal file
52
exercises/ch04/ex4_9.lua
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
local function remove(str, start, _end)
|
||||||
|
-- print(start, _end, str:sub(start, start), str:sub(_end, _end))
|
||||||
|
local start_part = str:sub(1, start - 1)
|
||||||
|
local end_part = str:sub(_end + 1, -1)
|
||||||
|
return start_part .. end_part
|
||||||
|
end
|
||||||
|
|
||||||
|
local function remove_patterns(str, pattern)
|
||||||
|
while true do
|
||||||
|
local rem_start, rem_end, _ = str:find(pattern)
|
||||||
|
if rem_start == nil then
|
||||||
|
break
|
||||||
|
end
|
||||||
|
-- print(str, pattern, rem_start, rem_end)
|
||||||
|
str = remove(str, rem_start, rem_end)
|
||||||
|
-- print(str)
|
||||||
|
end
|
||||||
|
|
||||||
|
return str
|
||||||
|
end
|
||||||
|
|
||||||
|
local function ispali(str)
|
||||||
|
str = str:lower()
|
||||||
|
|
||||||
|
-- format
|
||||||
|
str = remove_patterns(str, "%s")
|
||||||
|
str = remove_patterns(str, "%p")
|
||||||
|
|
||||||
|
for i = 1, utf8.len(str) do
|
||||||
|
local idx = utf8.offset(str, i)
|
||||||
|
local idx_rev = utf8.offset(str, -i)
|
||||||
|
local ch_code = utf8.codepoint(str, idx)
|
||||||
|
local ch_code_rev = utf8.codepoint(str, idx_rev)
|
||||||
|
if ch_code ~= ch_code_rev then
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
|
||||||
|
local function test(str)
|
||||||
|
print(("%s: %q"):format(str, ispali(str)))
|
||||||
|
end
|
||||||
|
|
||||||
|
test("step ön nö pets")
|
||||||
|
test("bänänä")
|
||||||
|
|
||||||
|
print()
|
||||||
|
|
||||||
|
test("step. ön nö pets!")
|
||||||
|
test("bänänä??")
|
||||||
3
exercises/ch05/ex5_1.lua
Normal file
3
exercises/ch05/ex5_1.lua
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
local sunday = "monday"; monday = "sunday"
|
||||||
|
local t = {sunday = "monday", [sunday] = monday}
|
||||||
|
print(t.sunday, t[sunday], t[t.sunday])
|
||||||
16
exercises/ch05/ex5_3.lua
Normal file
16
exercises/ch05/ex5_3.lua
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
local escape_sequences = {
|
||||||
|
["\a"] = "bell",
|
||||||
|
["\b"] = "back space",
|
||||||
|
["\f"] = "form feed",
|
||||||
|
["\n"] = "newline",
|
||||||
|
["\r"] = "carriage return",
|
||||||
|
["\t"] = "horizontal tab",
|
||||||
|
["\v"] = "vertical tab",
|
||||||
|
["\\"] = "backslash",
|
||||||
|
["\""] = "double quote",
|
||||||
|
["\'"] = "single quote"
|
||||||
|
}
|
||||||
|
|
||||||
|
for k, v in pairs(escape_sequences) do
|
||||||
|
print(("k='%s', v='%s'"):format(k, v))
|
||||||
|
end
|
||||||
30
exercises/ch05/ex5_4.lua
Normal file
30
exercises/ch05/ex5_4.lua
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
local function polynomial(coefs, x)
|
||||||
|
local sum = 0
|
||||||
|
for i = 1, #coefs do
|
||||||
|
sum = sum + coefs[i] * x^i
|
||||||
|
end
|
||||||
|
|
||||||
|
return sum
|
||||||
|
end
|
||||||
|
|
||||||
|
local function table_str(t)
|
||||||
|
local str = "{ "
|
||||||
|
for _, v in pairs(t) do
|
||||||
|
str = str + v + ", "
|
||||||
|
end
|
||||||
|
|
||||||
|
str = str + "}"
|
||||||
|
end
|
||||||
|
|
||||||
|
local function test(coefs, x, should_be)
|
||||||
|
local res = polynomial(coefs, x)
|
||||||
|
if res ~= should_be then
|
||||||
|
print(
|
||||||
|
("Test failed. Should have been %f, was %f. coefs: %s, x: %f"):format(should_be, res, table_str(coefs), x)
|
||||||
|
)
|
||||||
|
else
|
||||||
|
print("Test passed.")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
test({1, 2, 3}, 2, 1 * 2^1 + 2 * 2^2 + 3 * 2^3)
|
||||||
35
exercises/ch05/ex5_6.lua
Normal file
35
exercises/ch05/ex5_6.lua
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
local function is_sequence(t)
|
||||||
|
local prev_k = 0
|
||||||
|
for k, _ in pairs(t) do
|
||||||
|
if type(k) ~= "number" or k ~= prev_k + 1 then
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
|
prev_k = prev_k + 1
|
||||||
|
end
|
||||||
|
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
|
||||||
|
local function table_str(t)
|
||||||
|
local str = "{"
|
||||||
|
for k, v in pairs(t) do
|
||||||
|
if type(k) == "string" then
|
||||||
|
k = '"' .. k .. '"'
|
||||||
|
end
|
||||||
|
str = ("%s [%s] = %s,"):format(str, k, v)
|
||||||
|
end
|
||||||
|
|
||||||
|
str = str .. " }"
|
||||||
|
|
||||||
|
return str
|
||||||
|
end
|
||||||
|
|
||||||
|
local function test(t)
|
||||||
|
print(("%s: %q"):format(table_str(t), is_sequence(t)))
|
||||||
|
end
|
||||||
|
|
||||||
|
test({ 1, 2, 3, word = "a word", [10] = 999 })
|
||||||
|
test({ 1, 2, 3 })
|
||||||
|
test({ [1.0] = 1, [2] = 2 })
|
||||||
|
test({ [1.1] = 1, [2] = 2 })
|
||||||
39
exercises/ch05/ex5_7.lua
Normal file
39
exercises/ch05/ex5_7.lua
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
local function insert_into(insert, into, pos)
|
||||||
|
local insert_i = 0
|
||||||
|
for into_i = pos, #into + #insert do
|
||||||
|
local new_into_i = into_i + #insert
|
||||||
|
into[new_into_i] = into[into_i]
|
||||||
|
into[into_i] = insert[insert_i]
|
||||||
|
insert_i = insert_i + 1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local function table_str(t)
|
||||||
|
local str = "{"
|
||||||
|
for _, v in pairs(t) do
|
||||||
|
if type(v) == "string" then
|
||||||
|
v = '"' .. v .. '"'
|
||||||
|
end
|
||||||
|
str = ("%s %s,"):format(str, v)
|
||||||
|
end
|
||||||
|
|
||||||
|
str = str .. " }"
|
||||||
|
|
||||||
|
return str
|
||||||
|
end
|
||||||
|
|
||||||
|
local function test(insert, into, pos)
|
||||||
|
print("------------------------------")
|
||||||
|
print(("pos: %d"):format(pos))
|
||||||
|
print(("insert: %s"):format(table_str(insert)))
|
||||||
|
print(("before: %s"):format(table_str(into)))
|
||||||
|
insert_into(insert, into, pos)
|
||||||
|
print(("after: %s"):format(table_str(into)))
|
||||||
|
print("------------------------------")
|
||||||
|
end
|
||||||
|
|
||||||
|
test({5, 6, 7}, {1, 2, 3, 10, 11}, 4)
|
||||||
|
|
||||||
|
test({"a", "b", "c"}, { "a", "b", "c", "d", "e" }, 5)
|
||||||
|
|
||||||
|
test({"brave", "new"}, {"hello", "world"}, 2)
|
||||||
31
exercises/ch05/ex5_8.lua
Normal file
31
exercises/ch05/ex5_8.lua
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
local function concat(strs)
|
||||||
|
local res = ""
|
||||||
|
for i = 1, #strs do
|
||||||
|
res = res .. strs[i]
|
||||||
|
end
|
||||||
|
|
||||||
|
return res
|
||||||
|
end
|
||||||
|
|
||||||
|
local function random_str()
|
||||||
|
return tostring(math.random(100000, 999999))
|
||||||
|
end
|
||||||
|
|
||||||
|
local strs = {}
|
||||||
|
for i = 1, 200000 do
|
||||||
|
strs[i] = random_str()
|
||||||
|
end
|
||||||
|
|
||||||
|
local time_start = os.clock()
|
||||||
|
concat(strs)
|
||||||
|
local own_concat_time = os.clock() - time_start
|
||||||
|
|
||||||
|
print("Own concat time: " .. own_concat_time)
|
||||||
|
|
||||||
|
time_start = os.clock()
|
||||||
|
_ = table.concat(strs)
|
||||||
|
local table_concat_time = os.clock() - time_start
|
||||||
|
|
||||||
|
print("table.concat time: " .. table_concat_time)
|
||||||
|
|
||||||
|
print(("table.concat was %f%s faster"):format(own_concat_time / table_concat_time * 100, "%"))
|
||||||
Reference in New Issue
Block a user