ch05 exercises

This commit is contained in:
2026-02-25 23:58:37 +02:00
parent 53d5f2878c
commit d91930f1f9
4 changed files with 135 additions and 0 deletions

30
exercises/ch05/ex5_4.lua Normal file
View 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
View 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
View 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
View 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, "%"))