Show List
Lua Coding Questions
- Write a function that takes a string and returns a table containing the frequency of each character in the string.
luaCopy code
function count_chars(str)
local counts = {}
for i = 1, #str do
local char = str:sub(i, i)
counts[char] = (counts[char] or 0) + 1
end
return counts
end
print(count_chars("hello world"))
-- output: {["h"] = 1, ["e"] = 1, ["l"] = 3, ["o"] = 2, [" "] = 1, ["w"] = 1, ["r"] = 1, ["d"] = 1}
- Write a function that takes a table of numbers and returns the sum of the even numbers in the table.
luaCopy code
function sum_even_numbers(numbers)
local sum = 0
for _, number in ipairs(numbers) do
if number % 2 == 0 then
sum = sum + number
end
end
return sum
end
print(sum_even_numbers({1, 2, 3, 4, 5, 6}))
-- output: 12
- Write a function that takes a string and returns a new string with the first letter of each word capitalized.
luaCopy code
function capitalize_words(str)
return str:gsub("%w+", function(word)
return word:gsub("^%l", string.upper)
end)
end
print(capitalize_words("hello world"))
-- output: "Hello World"
- Write a function that takes a table of numbers and returns the median value.
luaCopy code
function median(numbers)
table.sort(numbers)
local count = #numbers
if count % 2 == 0 then
return (numbers[count/2] + numbers[count/2 + 1])/2
else
return numbers[(count + 1)/2]
end
end
print(median({1, 2, 3, 4, 5}))
-- output: 3
- Write a function that takes a table of strings and returns a new table containing only the strings that contain a given substring.
luaCopy code
function filter_strings(strings, substring)
local filtered_strings = {}
for _, str in ipairs(strings) do
if string.find(str, substring) then
table.insert(filtered_strings, str)
end
end
return filtered_strings
end
print(filter_strings({"hello", "world", "lua", "programming"}, "lu"))
-- output: {"lua", "programming"}
- Write a function that takes a string and returns a new string with all vowels replaced with the letter 'o'.
luaCopy code
function replace_vowels(str)
return str:gsub("[aeiouAEIOU]", "o")
end
print(replace_vowels("hello world"))
-- output: "hollo worlo"
- Write a function that takes a table of numbers and returns the product of the numbers.
luaCopy code
function product(numbers)
local result = 1
for _, number in ipairs(numbers) do
result = result * number
end
return result
end
print(product({1, 2, 3, 4, 5}))
-- output: 120
- Write a function that takes a table of numbers and returns a new table with the numbers sorted in descending order.
luaCopy code
function sort_descending(numbers)
table.sort(numbers, function(a, b) return a > b end)
return numbers
end
print(sort_descending({5, 2, 8, 3, 1}))
-- output: {8, 5, 3, 2, 1}
- Write a function that takes a string and returns a new string with all non-alphanumeric characters removed.
luaCopy code
function remove_non_alphanumeric(str)
return str:gsub("[^%w]+", "")
end
print(remove_non_alphanumeric("hello-world!"))
-- output: "helloworld"
- Write a function that takes a table of numbers and returns the average of the numbers.
luaCopy code
function average(numbers)
local sum = 0
for _, number in ipairs(numbers) do
sum = sum + number
end
return sum/#numbers
end
print(average({1, 2, 3, 4, 5}))
-- output: 3
- Write a function that takes a string and returns a new string with all letters shifted by a given number of positions in the alphabet.
luaCopy code
function shift_letters(str, shift)
return str:gsub("%a", function(char)
local base = string.byte("A")
local offset = (string.byte(char) - base + shift) % 26
return string.char(base + offset)
end)
end
print(shift_letters("hello world", 3))
-- output: "khoor zruog"
- Write a function that takes a table of numbers and returns a new table with only the unique numbers.
luaCopy code
function unique(numbers)
local seen = {}
local result = {}
for _, number in ipairs(numbers) do
if not seen[number] then
table.insert(result, number)
seen[number] = true
end
end
return result
end
print(unique({1, 2, 3, 2, 4, 3, 5}))
-- output: {1, 2, 3, 4, 5}
- Write a function that takes a string and returns a new string with each word reversed.
luaCopy code
function reverse_words(str)
return str:gsub("%S+", function(word)
return string.reverse(word)
end)
end
print(reverse_words("hello world"))
-- output: "olleh dlrow"
- Write a function that takes a table of numbers and returns the maximum value.
luaCopy code
function max(numbers)
local result = numbers[1]
for _, number in ipairs(numbers) do
if number > result then
result = number
end
end
return result
end
print(max({1, 2, 3, 4, 5}))
-- output: 5
- Write a function that takes two tables and returns a new table with the elements that are in both tables.
luaCopy code
function intersect(t1, t2)
local result = {}
for _, value in ipairs(t1) do
if table.contains(t2, value) then
table.insert(result, value)
end
end
return result
end
table.contains = function(table, element)
for _, value in ipairs(table) do
if value == element then
return true
end
end
return false
end
print(intersect({1, 2, 3, 4}, {3, 4, 5, 6}))
-- output: {3, 4}
- Write a function that takes a string and returns a new string with each character repeated a given number of times.
luaCopy code
function repeat_characters(str, n)
return str:gsub(".", function(char)
return string.rep(char, n)
end)
end
print(repeat_characters("hello", 2))
-- output: "hheelllloo"
- Write a function that takes a table of numbers and returns a new table with the numbers squared.
luaCopy code
function square(numbers)
local result = {}
for _, number in ipairs(numbers) do
table.insert(result, number^2)
end
return result
end
print(square({1, 2, 3, 4, 5}))
-- output: {1, 4, 9, 16, 25}
- Write a function that takes a string and returns a new string with each word capitalized.
luaCopy code
function capitalize_words(str)
return str:gsub("%S+", function(word)
return string.upper(string.sub(word, 1, 1))..string.sub(word, 2)
end)
end
print(capitalize_words("hello world"))
-- output: "Hello World"
- Write a function that takes a table of numbers and returns the minimum value.
luaCopy code
function min(numbers)
local result = numbers[1]
for _, number in ipairs(numbers) do
if number < result then
result = number
end
end
return result
end
print(min({1, 2, 3, 4, 5}))
-- output: 1
- Write a function that takes a string and returns a new string with each word reversed, but with the order of the words preserved.
luaCopy code
function reverse_words_preserve_order(str)
local result = {}
for word in str:gmatch("%S+") do
table.insert(result, string.reverse(word))
end
return table.concat(result, " ")
end
print(reverse_words_preserve_order("hello world"))
-- output: "olleh dlrow"
- Write a function that takes a table of numbers and returns a new table with the numbers rounded to the nearest integer.
luaCopy code
function round(numbers)
local result = {}
for _, number in ipairs(numbers) do
table.insert(result, math.floor(number + 0.5))
end
return result
end
print(round({1.4, 2.6, 3.1, 4.9}))
-- output: {1, 3, 3, 5}
- Write a function that takes a string and returns the first non-repeated character.
luaCopy code
function first_non_repeated_character(str)
local counts = {}
for char in str:gmatch(".") do
counts[char] = (counts[char] or 0) + 1
end
for char in str:gmatch(".") do
if counts[char] == 1 then
return char
end
end
end
print(first_non_repeated_character("hello world"))
-- output: "h"
- Write a function that takes a table of strings and returns the longest string.
luaCopy code
function longest_string(strings)
local result = ""
for _, str in ipairs(strings) do
if #str > #result then
result = str
end
end
return result
end
print(longest_string({"hello", "world", "lua"}))
-- output: "hello"
- Write a function that takes a number and returns true if it is prime, false otherwise.
luaCopy code
function is_prime(number)
if number < 2 then
return false
end
for i = 2, math.sqrt(number) do
if number % i == 0 then
return false
end
end
return true
end
print(is_prime(17))
-- output: true
- Write a function that takes a string and returns true if it is a palindrome, false otherwise.
luaCopy code
function is_palindrome(str)
local reversed = str:reverse()
return str == reversed
end
print(is_palindrome("racecar"))
-- output: true
- Write a function that takes a table of numbers and returns the sum of the even numbers.
luaCopy code
function sum_even(numbers)
local result = 0
for _, number in ipairs(numbers) do
if number % 2 == 0 then
result = result + number
end
end
return result
end
print(sum_even({1, 2, 3, 4, 5}))
-- output: 6
- Write a function that takes a string and a character, and returns the number of occurrences of the character in the string.
luaCopy code
function count_occurrences(str, char)
local count = 0
for c in str:gmatch(".") do
if c == char then
count = count + 1
end
end
return count
end
print(count_occurrences("hello world", "l"))
-- output: 3
- Write a function that takes a table of strings and returns a new table with the strings sorted alphabetically.
luaCopy code
function sort_strings(strings)
table.sort(strings)
return strings
end
print(sort_strings({"hello", "world", "lua"}))
-- output: {"hello", "lua", "world"}
- Write a function that takes a string and returns a new string with the words reversed, but with the order of the words preserved.
luaCopy code
function reverse_words_preserve_order(str)
local result = {}
for word in str:gmatch("%S+") do
table.insert(result, string.reverse(word))
end
return table.concat(result, " ")
end
print(reverse_words_preserve_order("hello world"))
-- output: "olleh dlrow"
Leave a Comment