【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"
相关推荐
曾凡宇先生9 分钟前
无法远程连接 MySQL
android·开发语言·数据库·sql·tcp/ip·mysql·adb
Q一件事1 小时前
R语言随机森林分析显示R方和P值
开发语言·随机森林·r语言
Mr.Jessy1 小时前
JavaScript学习第六天:函数
开发语言·前端·javascript·学习·html·1024程序员节
九皇叔叔2 小时前
Java循环结构全解析:从基础用法到性能优化
java·开发语言·性能优化
sulikey2 小时前
Qt 入门简洁笔记:从框架概念到开发环境搭建
开发语言·前端·c++·qt·前端框架·visual studio·qt框架
zzzsde2 小时前
【C++】stack和queue:优先级队列的使用及底层原理
开发语言·c++
烛阴3 小时前
循环背后的魔法:Lua 迭代器深度解析
前端·lua
是苏浙3 小时前
零基础入门C语言之操作符详解2
c语言·开发语言
总有刁民想爱朕ha4 小时前
银河麒麟v10批量部署Python Flask项目小白教程
开发语言·python·flask·银河麒麟v10
yi碗汤园4 小时前
【一文了解】八大排序-插入排序、希尔排序
开发语言·算法·unity·c#·1024程序员节