【Lua】题目小练12

-- 1. 表操作

-- 给定表:

-- t = {10, 20, 30, 40, 50}

-- 写一个函数 sum(tbl) 计算表中所有元素的和。

Lua 复制代码
local function sum(tbl)
    local total = 0
    for _, v in ipairs(tbl) do
        if type(v) == "number" then
            total = total + v
        end
    end
    return total
end

local t = {10, 20, 30, 40, 50}
print(sum(t))

-- 2. 字符串处理

-- 写一个函数,输入一个字符串 "hello world",输出反转后的结果 "dlrow olleh"。

Lua 复制代码
local function reverseStr(str)
    return string.reverse(str)
end

local s = "hello world"
print(reverseStr(s))

-- 3. 条件判断

-- 写一个函数 isEven(n) 判断一个整数是否为偶数,返回 true 或 false。

Lua 复制代码
local function isEven(n)
    if type(n) ~= "number" then
        print("请输入数字")
        return
    end
    return n % 2 == 0
end

print(isEven(24))

-- 4. 频率统计

-- 给定表:

-- fruits = {"apple", "banana", "apple", "orange", "banana", "apple"}

-- 写一个函数 countFreq(tbl) 返回每个元素出现的次数,例如:

-- { apple = 3, banana = 2, orange = 1 }

Lua 复制代码
local function countFreq(tbl)
    local freq = {}
    for _, v in ipairs(tbl) do
        freq[v] = (freq[v] or 0) + 1
    end
    return freq
end

local fruits = {"apple", "banana", "apple", "orange", "banana", "apple"}
local nT = countFreq(fruits)

for index, value in pairs(nT) do
    print(index .. value)
end

-- 5. 闭包应用

-- 写一个函数 counter(),返回一个闭包函数,每次调用时返回一个递增的数字:

Lua 复制代码
local c = counter()
print(c())  --> 1
print(c())  --> 2
print(c())  --> 3

local function counter()
    local currentCount = 0

    return function()
        currentCount = currentCount + 1
        return currentCount
    end
end

local c = counter()
print(c())
print(c())
print(c())

-- 6. 冒泡排序

-- 给定一个数字表 {5, 1, 4, 2, 8},写一个函数用冒泡排序将其从小到大排列。

Lua 复制代码
local function bubbleSort(t)
    local n = #t
    for i = 1, n - 1 do
        for j = 1, n - i do
            if t[j] > t[j+1] then
                t[j], t[j+1] = t[j+1], t[j]
            end
        end
    end
    return t
end

local o = {5, 1, 4, 2, 8}
local newT = bubbleSort(o)

for index, value in ipairs(newT) do
    print(index .. " " .. value)
end

-- 7. 迭代器

-- 写一个自定义迭代器 evenIterator(tbl),只遍历表中的偶数。

Lua 复制代码
for v in evenIterator({1,2,3,4,5,6}) do
    print(v) -- 输出 2, 4, 6
end

local function evenIterator(tbl)
    local i = 0
    return function()
        i = i + 1
        while tbl[i] do
            if tbl[i] % 2 == 0 then
                return tbl[i]
            end
            i = i + 1
        end
    end
end

for v in evenIterator({1,2,3,4,5,6}) do
    print(v) -- 2, 4, 6
end

for v in evenIterator({1,2,3,4,5,6}) do
    print(v) -- 输出 2, 4, 6
end

-- 8. 元表应用

-- 定义一个表 Vector,支持向量加法:

-- v1 = Vector.new(1, 2)

-- v2 = Vector.new(3, 4)

-- v3 = v1 + v2 -- 结果应为 (4, 6)

Lua 复制代码
local Vector = {}
Vector.__index = Vector
function Vector.__tostring(v)
    return "(" .. v.x .. ", " .. v.y .. ")"
end

Vector.__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" then
        obj.x = v1.x + v2.x
        obj.y = v1.y + v2.y
    end
    return obj
end

function Vector.new(x, y)
    local obj = {}
    obj.x = type(x) == "number" and x or 0
    obj.y = type(y) == "number" and y or 0
    setmetatable(obj, Vector)
    return obj
end

local v1 = Vector.new(1, 2)
local v2 = Vector.new(3, 4)
local v3 = v1 + v2   -- 结果应为 (4, 6)

print(v3.x .. "  " .. v3.y)

-- 9.协程

-- 写一个协程函数,每次调用只输出一句话:

Lua 复制代码
local co = myCoroutine()
coroutine.resume(co) -- 输出 "Step 1"
coroutine.resume(co) -- 输出 "Step 2"
coroutine.resume(co) -- 输出 "Step 3"

local count = 1

local co = coroutine.create(function()
    while true do
        print("Step " .. tostring(count))
        count = count + 1
        coroutine.yield()
    end
end)

coroutine.resume(co) -- 输出 "Step 1"
coroutine.resume(co) -- 输出 "Step 2"
coroutine.resume(co) -- 输出 "Step 3"
coroutine.resume(co) -- 输出 "Step 4"
相关推荐
冻柠檬飞冰走茶18 分钟前
字母移动游戏题解:贪心求最长可保留连续子串(附 C 实现)
c语言·开发语言·数据结构·算法·动态规划
FRing0333 分钟前
vue2干货总结7(自定义指令、插槽、单页应用程序、路由的使用(VueRouter))
开发语言·前端·vue.js·vue2
雪落漂泊38 分钟前
C++算法 二分查找篇(上)
开发语言·c++·算法
波力海苔夹心脆6752 小时前
C# WPF 资源字典详解:ResourceDictionary、MergedDictionaries 与自定义控件案例
开发语言·c#·wpf·入门教程·resource·资源字典
不会算法的笨小孩2 小时前
异常的使用以及会出现的问题
开发语言·c++·c++11·异常
Hhy_11074 小时前
《C++深度解构06》手撕 string —— 从底层内存管理到深浅拷贝彻底吃透
c语言·开发语言·c++·学习·visual studio
starzy19904 小时前
深入理解Scala函数定义:从方法到函数的类型跨越
开发语言·scala
starzy19904 小时前
深入理解Scala递归函数:从栈溢出到尾调用优化
开发语言·scala
三*一5 小时前
Mapbox‑GL JS 多边形折点编辑工具:实现原理、功能拆解与踩坑总结
开发语言·javascript·ecmascript
YSL0701245 小时前
C++基础
开发语言·c++