> For the complete documentation index, see [llms.txt](https://www.csprinciples.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.csprinciples.com/25-26/session-20.md).

# Session 20

## Source Code

```lua
-- 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
            example1()
        elseif choice == "E2" then
            example2()
        elseif choice == "E3" then
            example3()
        elseif choice == "E4" then
            example4()
        elseif choice == "E5" then
            example5()
        elseif choice == "E6" then
            example6()
        elseif choice == "E7" then
            example7()
        elseif choice == "E8" then
            example8()
        elseif choice == "E9" then
            example9()
        elseif choice == "Q" then
            done = true
        else
            print("Invalid Choice")
        end
    end
end

-- define Example 1 Function
function example1()
    -- double quotes

    -- single quotes

    -- \n is a newline

    -- Lua multi-line string using double brackets


    -- length



    -- .upper()


    -- .lower()


end

-- define Example 2 Function
function example2()
    local major = "Computer Science"
    print(major)

    -- .sub()



    -- .find()



    -- loop and print every character


end

-- define Example 3 Function
function example3()
    -- names list


    -- ages list



    -- print the names and corresponding ages



    -- add a new name and age



    -- print the last student's name and age



end

-- define Example 4 Function
function example4()
    -- Student grade tracker: three parallel lists
    

    -- The RELATIONSHIP: names[i], scores[i], and letters[i]
    -- all describe the SAME student.

    -- Print a formatted report of all students
   

    -- Add a new student to all three lists
    

    -- Verify alignment
    

    -- Remove the student at index 2 (Bob) from all lists
    

    -- After removal, Carlos is now at index 2
    


end

-- define Example 5 Function
function example5()
    -- 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 6 Function
function example6()
    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 7 Function
function example7()
    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 8 Function
function example8()
    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 9 Function
function example9()
    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()

```

## s20problem1.lua

```lua
-- function definition
function problem1()

end

-- function definition
function main()
    -- function call

end

-- call to main function
main()
```

## s20problem2.lua

```lua
-- function definition
function problem2()

end

-- function definition
function main()
    -- function call

end

-- call to main function
main()
```

## s20problem3.lua

```lua
-- function definition
function problem3()

end

-- function definition
function main()
    -- function call

end

-- call to main function
main()
```

## s20problem4.lua

```lua
-- function definition
function problem4()

end

-- function definition
function main()
    -- function call

end

-- call to main function
main()
```

## s20problem5.lua

```lua
function s20problem5()
    -- 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
s20problem5()

```

## s20problem6.lua

```lua
function s20problem6()
    -- 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
s20problem6()
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://www.csprinciples.com/25-26/session-20.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
