편집
루아 연습 및 모듈 테스트를 위한 김찬의 모듈 연습장입니다.
여기서 쓰여진 Lua 코드는 연습장에 사용됩니다.
local p = {}
-- 헬로 월드!
function p.hello()
return "Hello, world!"
end
-- 헥스 코드를 RGB로 변환하는 함수
function p.hexToRGB(hex)
hex = hex:gsub("#", "") -- # 제거
local r = tonumber(hex:sub(1, 2), 16)
local g = tonumber(hex:sub(3, 4), 16)
local b = tonumber(hex:sub(5, 6), 16)
return r, g, b
end
-- 미디어위키에서 사용 가능한 인터페이스
function p.showRGB(frame)
local hex = frame.args[1] or "#000000" -- 기본값 검정색
local r, g, b = p.hexToRGB(hex)
return string.format("%d, %d, %d", r, g, b)
end
-- 별점 개선
function p.calculate_average(frame)
-- 점수 배열 가져오기
local args = frame.args
local scores = {}
for _, value in ipairs(args) do
local score = tonumber(value)
if score then
if score >= 0 and score <= 5 then
table.insert(scores, score)
else
return "점수는 0에서 5 사이여야 합니다."
end
end
end
-- 점수가 없으면 에러 메시지 출력
if #scores == 0 then
return "점수가 입력되지 않았습니다."
end
-- 점수 합산 및 평균 계산
local sum = 0
for _, score in ipairs(scores) do
sum = sum + score
end
local average = sum / #scores
-- 소수점 둘째 자리에서 반올림
average = math.floor(average * 100 + 0.5) / 100
-- 불필요한 0 제거 후 반환
local result = tostring(average)
if result:find("%.") then
result = result:gsub("0+$", ""):gsub("%.$", "")
end
return result
end
return p