From d91930f1f906454e4f6e3e050fe05758237b00ca Mon Sep 17 00:00:00 2001 From: Aaro Saila Date: Wed, 25 Feb 2026 23:58:37 +0200 Subject: [PATCH] ch05 exercises --- exercises/ch05/ex5_4.lua | 30 ++++++++++++++++++++++++++++++ exercises/ch05/ex5_6.lua | 35 +++++++++++++++++++++++++++++++++++ exercises/ch05/ex5_7.lua | 39 +++++++++++++++++++++++++++++++++++++++ exercises/ch05/ex5_8.lua | 31 +++++++++++++++++++++++++++++++ 4 files changed, 135 insertions(+) create mode 100644 exercises/ch05/ex5_4.lua create mode 100644 exercises/ch05/ex5_6.lua create mode 100644 exercises/ch05/ex5_7.lua create mode 100644 exercises/ch05/ex5_8.lua diff --git a/exercises/ch05/ex5_4.lua b/exercises/ch05/ex5_4.lua new file mode 100644 index 0000000..9de5dba --- /dev/null +++ b/exercises/ch05/ex5_4.lua @@ -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) diff --git a/exercises/ch05/ex5_6.lua b/exercises/ch05/ex5_6.lua new file mode 100644 index 0000000..7653d90 --- /dev/null +++ b/exercises/ch05/ex5_6.lua @@ -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 }) diff --git a/exercises/ch05/ex5_7.lua b/exercises/ch05/ex5_7.lua new file mode 100644 index 0000000..3e7a9d6 --- /dev/null +++ b/exercises/ch05/ex5_7.lua @@ -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) diff --git a/exercises/ch05/ex5_8.lua b/exercises/ch05/ex5_8.lua new file mode 100644 index 0000000..ea71c11 --- /dev/null +++ b/exercises/ch05/ex5_8.lua @@ -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, "%"))