【Lua】题目小练12
-- 1. 表操作
-- 给定表:
-- t = {10, 20, 30, 40, 50}
-- 写一个函数 sum(tbl) 计算表中所有元素的和。
local function sum(tbl)local total = 0for _, v in ipairs(tbl) doif type(v) == "number" thentotal = total + vendendreturn total endlocal t = {10, 20, 30, 40, 50} print(sum(t))
-- 2. 字符串处理
-- 写一个函数,输入一个字符串 "hello world",输出反转后的结果 "dlrow olleh"。
local function reverseStr(str)return string.reverse(str) endlocal s = "hello world" print(reverseStr(s))
-- 3. 条件判断
-- 写一个函数 isEven(n) 判断一个整数是否为偶数,返回 true 或 false。
local function isEven(n)if type(n) ~= "number" thenprint("请输入数字")returnendreturn n % 2 == 0 endprint(isEven(24))
-- 4. 频率统计
-- 给定表:
-- fruits = {"apple", "banana", "apple", "orange", "banana", "apple"}
-- 写一个函数 countFreq(tbl) 返回每个元素出现的次数,例如:
-- { apple = 3, banana = 2, orange = 1 }
local function countFreq(tbl)local freq = {}for _, v in ipairs(tbl) dofreq[v] = (freq[v] or 0) + 1endreturn freq endlocal fruits = {"apple", "banana", "apple", "orange", "banana", "apple"} local nT = countFreq(fruits)for index, value in pairs(nT) doprint(index .. value) end
-- 5. 闭包应用
-- 写一个函数 counter(),返回一个闭包函数,每次调用时返回一个递增的数字:
local c = counter() print(c()) --> 1 print(c()) --> 2 print(c()) --> 3local function counter()local currentCount = 0return function()currentCount = currentCount + 1return currentCountend endlocal c = counter() print(c()) print(c()) print(c())
-- 6. 冒泡排序
-- 给定一个数字表 {5, 1, 4, 2, 8},写一个函数用冒泡排序将其从小到大排列。
local function bubbleSort(t)local n = #tfor i = 1, n - 1 dofor j = 1, n - i doif t[j] > t[j+1] thent[j], t[j+1] = t[j+1], t[j]endendendreturn t endlocal o = {5, 1, 4, 2, 8} local newT = bubbleSort(o)for index, value in ipairs(newT) doprint(index .. " " .. value) end
-- 7. 迭代器
-- 写一个自定义迭代器 evenIterator(tbl),只遍历表中的偶数。
for v in evenIterator({1,2,3,4,5,6}) doprint(v) -- 输出 2, 4, 6 endlocal function evenIterator(tbl)local i = 0return function()i = i + 1while tbl[i] doif tbl[i] % 2 == 0 thenreturn tbl[i]endi = i + 1endend endfor v in evenIterator({1,2,3,4,5,6}) doprint(v) -- 2, 4, 6 endfor v in evenIterator({1,2,3,4,5,6}) doprint(v) -- 输出 2, 4, 6 end
-- 8. 元表应用
-- 定义一个表 Vector,支持向量加法:
-- v1 = Vector.new(1, 2)
-- v2 = Vector.new(3, 4)
-- v3 = v1 + v2 -- 结果应为 (4, 6)
local Vector = {} Vector.__index = Vector function Vector.__tostring(v)return "(" .. v.x .. ", " .. v.y .. ")" endVector.__add = function (v1, v2)local obj = {}obj = Vector:new(0, 0)if type(v1.x) == "number" and type(v2.x) == "number" and type(v1.y) == "number" and type(v2.y) == "number" thenobj.x = v1.x + v2.xobj.y = v1.y + v2.yendreturn obj endfunction Vector.new(x, y)local obj = {}obj.x = type(x) == "number" and x or 0obj.y = type(y) == "number" and y or 0setmetatable(obj, Vector)return obj endlocal v1 = Vector.new(1, 2) local v2 = Vector.new(3, 4) local v3 = v1 + v2 -- 结果应为 (4, 6)print(v3.x .. " " .. v3.y)
-- 9.协程
-- 写一个协程函数,每次调用只输出一句话:
local co = myCoroutine() coroutine.resume(co) -- 输出 "Step 1" coroutine.resume(co) -- 输出 "Step 2" coroutine.resume(co) -- 输出 "Step 3"local count = 1local co = coroutine.create(function()while true doprint("Step " .. tostring(count))count = count + 1coroutine.yield()end end)coroutine.resume(co) -- 输出 "Step 1" coroutine.resume(co) -- 输出 "Step 2" coroutine.resume(co) -- 输出 "Step 3" coroutine.resume(co) -- 输出 "Step 4"