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()
-- Initialize parallel lists
local titles = {"Avengers: Endgame", "A Quiet Place", "Fast & Furious 9", "Parasite",
"Spider-Man: No Way Home", "The Lighthouse", "Titanic", "Moonlight",
"The Dark Knight", "Get Out"}
local types = {"blockbuster", "indie", "blockbuster", "indie", "blockbuster",
"indie", "blockbuster", "indie", "blockbuster", "indie"}
local ratings = {9.0, 8.5, 7.8, 9.2, 8.9, 8.4, 9.1, 8.7, 9.3, 8.6}
-- Increase ratings for blockbuster movies
for i = 1, #ratings do
if types[i] == "blockbuster" then
ratings[i] = ratings[i] + 0.5
end
end
-- Sort movies based on ratings (descending order)
for i = 1, #ratings - 1 do
for j = i + 1, #ratings do
if ratings[i] < ratings[j] then
-- Swap ratings
ratings[i], ratings[j] = ratings[j], ratings[i]
-- Swap titles
titles[i], titles[j] = titles[j], titles[i]
-- Swap types
types[i], types[j] = types[j], types[i]
end
end
end
-- Print the sorted movie recommendations
print("**Biased Movie Recommendations:**")
for i = 1, #titles do
print(i .. ". " .. titles[i] .. " - Rating: " .. ratings[i] .. " - Type: " .. types[i])
end
end
-- define Example 2 Function
function example2()
local list = {}
local numElements = 100
for i = 1, numElements do
table.insert(list, i)
end
-- Linear Search
local start_time = os.clock()
local result = linearSearch(list, numElements - 1)
local end_time = os.clock()
local elapsed_time = end_time - start_time
print(string.format("Linear search time: %.6f seconds", elapsed_time))
-- Binary Search
local start_time = os.clock()
local result = binarySearch(list, numElements - 1)
local end_time = os.clock()
local elapsed_time = end_time - start_time
print(string.format("Binary search time: %.6f seconds", elapsed_time))
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 Example 3 Function
function example3()
local list = {}
local numElements = 1000
for i = 1, numElements do
table.insert(list, i)
end
-- Quadratic Time Algorithm
local start_time = os.clock()
local result = quadraticTimeAlgorithm(list, numElements - 1)
local end_time = os.clock()
local elapsed_time = end_time - start_time
print(string.format("Quadratic Time Algorithm: %.6f seconds", elapsed_time))
end
-- define Example 4 Function
function example4()
local array = {}
local numElements = 15
for i = 1, numElements do
table.insert(array, i)
end
-- Exponential Time Algorithm
local start_time = os.clock()
generateSubsets(array, 1, {})
local end_time = os.clock()
local elapsed_time = end_time - start_time
print(string.format("Exponential Algorithm: %.6f seconds", elapsed_time))
end
-- define Example 5 Function
function example5()
local array = {}
local numElements = 10
for i = 1, numElements do
table.insert(array, i)
end
-- Factorial Time Algorithm
local start_time = os.clock()
local totalPermutations = generatePermutations(array, #array)
local end_time = os.clock()
local elapsed_time = end_time - start_time
print(string.format("Factorial Algorithm: %.6f seconds", elapsed_time))
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 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()
w21problem1.lua
w21problem2.lua
Last updated
Was this helpful?