Week 21
Source Code
-- define main function
function main()
print("Main Function")
local done = false
local choice = nil
while not done do
print("Menu")
print("E1 - Example 1")
print("Q - Quit")
io.write("Choice: ")
choice = io.read()
if choice == "E1" then
-- call Example 1 Function
example1()
elseif choice == "E2" then
-- call Example 2 Function
example2()
elseif choice == "E3" then
-- call Example 3 Function
example3()
elseif choice == "E4" then
-- call Example 4 Function
example4()
elseif choice == "E5" then
-- call Example 5 Function
example5()
elseif choice == "Q" then
done = true
else
print("Invalid Choice")
end
end
end
-- define Example 1 Function
function example1()
local array = {}
local numElements = 1000
for i = 1, numElements do
table.insert(array, i)
end
-- Linear Search
local start_time = os.clock()
local result = linearSearch(array, numElements - 1)
local end_time = os.clock()
print("Linear search time: " .. (end_time - start_time) .. " seconds")
-- Binary Search
local start_time = os.clock()
local result = binarySearch(array, numElements - 1)
local end_time = os.clock()
print("Binary search time: " .. (end_time - start_time) .. " seconds")
end
-- define Example 2 Function
function example2()
local array = {}
local numElements = 10000
for i = 1, numElements do
table.insert(array, i)
end
local startTime = os.clock()
quadraticTimeAlgorithm(array)
local endTime = os.clock()
print("Quadratic Algorithm Time: " .. (endTime - startTime) .. " seconds")
end
-- define Example 3 Function
function example3()
local array = {}
local numElements = 15
for i = 1, numElements do
table.insert(array, i)
end
local startTime = os.clock()
generateSubsets(array, 1, {})
local endTime = os.clock()
print("Time taken: " .. (endTime - startTime) .. " seconds")
end
-- define Example 4 Function
function example4()
local array = {}
local numElements = 10
for i = 1, numElements do
table.insert(array, i)
end
local startTime = os.clock()
local totalPermutations = generatePermutations(array, #array)
local endTime = os.clock()
print("Factorial Algorithm (Permutations) Time: " .. (endTime - startTime) .. " seconds")
print("Total permutations generated: " .. totalPermutations)
end
-- define Example 5
function example5()
-- String Examples
local singleQuoted = 'Hello, World!'
local doubleQuoted = "Hello, Lua!"
local multiLineString = [[
This is a multi-line string
in Lua.
]]
-- Print String Variables
print(singleQuoted)
print(doubleQuoted)
print(multiLineString)
-- String Concatenation
print("hi" .. " " .. "world!")
-- Length of a String
-- Print a character
-- Loop and print every character
end
-- define Linear Search Function
function linearSearch(array, target)
for index, value in ipairs(array) do
if value == target then
return index
end
end
return -1
end
-- define Binary Search Function
function binarySearch(array, target)
local low = 1
local high = #array
while low <= high do
local mid = (low + high) // 2 -- midpoint
local guess = array[mid]
if guess == target then
return mid -- target found
elseif guess > target then
high = mid - 1
else
low = mid + 1
end
end
return -1
end
-- define Quadratic Time Algorithm
function quadraticTimeAlgorithm(array)
for i = 1, #array do
for j = 1, #array do
-- Assuming a simple operation here like printing
-- or a minimal computation
local a = array[i] + array[j]
end
end
end
-- define Generate Permutations Algorithm
function generatePermutations(array, n)
if n == 1 then
return 1 -- Return 1 for each completed permutation
else
local count = 0
for i = 1, n do
count = count + generatePermutations(array, n - 1)
if n % 2 == 0 then
array[i], array[n] = array[n], array[i]
else
array[1], array[n] = array[n], array[1]
end
end
return count
end
end
function printSet(set)
io.write("{")
for i, v in ipairs(set) do
io.write(v)
if i < #set then
io.write(", ")
end
end
io.write("}\n")
end
function generateSubsets(array, index, currentSubset)
if index > #array then
printSet(currentSubset)
return
end
-- Include the element at the current index
table.insert(currentSubset, array[index])
generateSubsets(array, index + 1, currentSubset)
-- Exclude the element at the current index
table.remove(currentSubset)
generateSubsets(array, index + 1, currentSubset)
end
-- call main function
main()
Last updated