First commit

This commit is contained in:
2026-02-19 18:51:17 +02:00
commit 2d9b2318a9
16 changed files with 358 additions and 0 deletions

12
exercises/ch04/ex4_1.lua Normal file
View File

@@ -0,0 +1,12 @@
local str1 = "<![CDATA[\n Hello World\n]]>"
print(str1)
print("\n------------------------------\n")
local str2 = [=[
<![CDATA[
Hello World
]]>]=]
print(str2)

18
exercises/ch04/ex4_3.lua Normal file
View File

@@ -0,0 +1,18 @@
local function insert(str, i, o)
local first_half = str:sub(1, i - 1)
local latter_half = str:sub(i, -1)
return first_half .. o .. latter_half
end
local function tests()
local res = insert("hello world", 1, "start: ")
print(('insert("hello world", 1, "start: ") --> %s'):format(res))
res = insert("hello world", 7, "small ")
print(('insert("hello world", 7, "small ") --> %s'):format(res))
res = insert("hello world", 12, "!")
print(('insert("hello world", 12, "!") --> %s'):format(res))
end
tests()

13
exercises/ch04/ex4_4.lua Normal file
View File

@@ -0,0 +1,13 @@
local function insert(str, i, o)
i = utf8.offset(str, i)
local first_half = str:sub(1, i - 1)
local latter_half = str:sub(i, -1)
return first_half .. o .. latter_half
end
local function tests()
local res = insert("ação", 5, "!")
print(('insert("ação", 5, "!") --> %s'):format(res))
end
tests()

7
exercises/ch04/ex4_5.lua Normal file
View File

@@ -0,0 +1,7 @@
local function remove(str, start, length)
local start_part = str:sub(1, start - 1)
local end_part = str:sub(start + length, -1)
return start_part .. end_part
end
print(remove("hello world", 7, 4))

9
exercises/ch04/ex4_6.lua Normal file
View File

@@ -0,0 +1,9 @@
local function remove(str, start, length)
local slice_start = utf8.offset(str, start - 1)
local slice_end = utf8.offset(str, start + length)
local start_part = str:sub(1, slice_start)
local end_part = str:sub(slice_end)
return start_part .. end_part
end
print(remove("ação", 2, 2))

7
exercises/ch04/ex4_7.lua Normal file
View File

@@ -0,0 +1,7 @@
local function ispali(str)
str = str:lower()
return str == str:reverse()
end
print(ispali("step on no pets"))
print(ispali("banana"))

39
exercises/ch04/ex4_8.lua Normal file
View File

@@ -0,0 +1,39 @@
local function remove(str, start, _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
str = remove(str, rem_start, rem_end)
end
return str
end
local function ispali(str)
str = str:lower()
-- format
str = remove_patterns(str, "%s")
str = remove_patterns(str, "%p")
return str == str:reverse()
end
local function test(str)
print(("%s: %q"):format(str, ispali(str)))
end
test("step on no pets")
test("banana")
print()
test("Step. On no pets!")
test("Banana??")