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
function w21problem1()
-- Parallel lists for student data
local names = {"Alice", "Bob", "Charlie", "David", "Eve", "Frank", "Grace", "Henry", "Ivy", "Jack"}
local schools = {"Oak Prep", "Public High", "Oak Prep", "Community School", "Public High",
"Oak Prep", "Community School", "Public High", "Elite Academy", "Community School"}
local scores = {85, 88, 80, 90, 87, 82, 91, 86, 84, 89}
for i = 1, #scores do
if schools[i] == "Oak Prep" then
scores[i] = scores[i] * 1.05
end
end
-- Sort students based on final scores (descending order)
for i = 1, #scores - 1 do
for j = i + 1, #scores do
if scores[i] < scores[j] then
-- Swap scores
scores[i], scores[j] = scores[j], scores[i]
-- Swap names
names[i], names[j] = names[j], names[i]
-- Swap schools
schools[i], schools[j] = schools[j], schools[i]
end
end
end
-- Print final student rankings
print("**Biased College Admissions Results:**")
for i = 1, #names do
print(i .. ". " .. names[i] .. " - School: " .. schools[i] .. " - Final Score: " .. string.format("%.2f", scores[i]))
end
end
-- Run the biased admissions system
w21problem1()
w21problem2.lua
function w21problem2()
-- Parallel lists for suspect data
local names = {"Alice", "Bob", "Charlie", "David", "Eve", "Frank", "Grace", "Henry", "Ivy", "Jack"}
local neighborhoods = {"Downtown", "Suburb", "Downtown", "Uptown", "Suburb",
"Downtown", "Uptown", "Suburb", "Downtown", "Uptown"}
local base_risk_scores = {50, 30, 55, 40, 35, 60, 42, 28, 53, 38}
local prior_convictions = {true, false, true, false, false, true, false, false, true, false}
local employment_status = {"unemployed", "employed", "employed", "unemployed", "employed",
"unemployed", "employed", "employed", "unemployed", "employed"}
local ages = {22, 40, 19, 33, 29, 25, 48, 55, 21, 37}
-- Adjust risk scores with hidden biases
for i = 1, #base_risk_scores do
local risk = base_risk_scores[i]
if neighborhoods[i] == "Downtown" then
risk = risk * 1.08
end
if prior_convictions[i] then
risk = risk * 1.12
end
if employment_status[i] == "unemployed" then
risk = risk * 1.05
end
if ages[i] >= 18 and ages[i] <= 25 then
risk = risk * 1.06
end
base_risk_scores[i] = risk -- Store adjusted risk score
end
-- Sort suspects based on final risk scores (descending order)
for i = 1, #base_risk_scores - 1 do
for j = i + 1, #base_risk_scores do
if base_risk_scores[i] < base_risk_scores[j] then
-- Swap risk scores
base_risk_scores[i], base_risk_scores[j] = base_risk_scores[j], base_risk_scores[i]
-- Swap names
names[i], names[j] = names[j], names[i]
-- Swap neighborhoods
neighborhoods[i], neighborhoods[j] = neighborhoods[j], neighborhoods[i]
-- Swap prior convictions
prior_convictions[i], prior_convictions[j] = prior_convictions[j], prior_convictions[i]
-- Swap employment status
employment_status[i], employment_status[j] = employment_status[j], employment_status[i]
-- Swap ages
ages[i], ages[j] = ages[j], ages[i]
end
end
end
-- Print formatted final suspect rankings
print("\n**Biased Criminal Risk Assessment Results:**\n")
print(string.format("%-3s %-10s %-12s %-8s %-12s %-10s %s", "#", "Name", "Neighborhood", "Age", "Conviction", "Employment", "Risk Score"))
for i = 1, #names do
print(string.format("%-3d %-10s %-12s %-8d %-12s %-10s %.2f",
i, names[i], neighborhoods[i], ages[i], prior_convictions[i], employment_status[i], base_risk_scores[i]))
end
end
-- Run the biased risk assessment
w21problem2()
Last updated
Was this helpful?