✒️
Computer Science Principles
  • Introduction
  • Overview
  • Course at a Glance
  • Course Exam Description
  • Create Performance Task
  • Reference Sheet
  • Resources
  • Big Idea 1
    • 1.1 Collaboration
    • 1.2 Program Function and Purpose
    • 1.3 Program Design and Development
    • 1.4 Identifying and Correcting Errors
  • Big Idea 2
    • 2.1 Binary Numbers
    • 2.2 Data Compression
    • 2.3 Extracting Information from Data
    • 2.4 Using Programs with Data
  • Big Idea 3
    • 3.1 Variables and Assignments
    • 3.2 Data Abstraction
    • 3.3 Mathematical Expressions
    • 3.4 Strings
    • 3.5 Boolean Expression
    • 3.6 Conditionals
    • 3.7 Nested Conditionals
    • 3.8 Iteration
    • 3.9 Developing Algorithms
    • 3.10 Lists
    • 3.11 Binary Search
    • 3.12 Calling Procedures
    • 3.13 Developing Procedures
    • 3.14 Libraries
    • 3.15 Random Values
    • 3.16 Simulations
    • 3.17 Algorithmic Efficiency
    • 3.18 Undecidable Problems
  • Big Idea 4
    • 4.1 The Internet
    • 4.2 Fault Tolerant
    • 4.3 Parallel and Distributed Computing
  • Big Idea 5
    • 5.1 Beneficial and Harmful Effects
    • 5.2 Digital Divide
    • 5.3 Computing Bias
    • 5.4 Crowdsourcing
    • 5.5 Legal and Ethical Concerns
    • 5.6 Safe Computing
  • Code
    • Week 10
    • Week 11
    • Week 12
    • Week 13
    • Week 14
    • Week 15
    • Week 16
    • Week 17
    • Week 18
    • Week 19
    • Week 20
    • Week 21
    • Week 22
Powered by GitBook
On this page
  • Source Code
  • w18problem1.lua
  • w18problem2.lua
  • w18problem3.lua
  • w18problem4.lua
  • w18problem5.lua
  • w18problem6.lua
  • playlist.lua

Was this helpful?

Export as PDF
  1. Code

Week 18

Source Code

local myName = ""
print("Name: " .. myName)

-- define main function
function main()
    print("Hello Main Function")
    print(myName)
    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
            -- Example 1 Function call
            example1()

        elseif choice == "E2" then
            -- Example 2 Function call
            example2()

        elseif choice == "E3" then
            -- Example 3 Function call
            example3()

        elseif choice == "E4" then
            -- Example 4 Function call
            example4()

        elseif choice == "Q" then
            done = true
        else
            print("Invalid Choice")
        end
    end
end

-- define Example 1 Function
function example1()
    print("Example 1")
    -- define friends list
    

    -- print the number of elements
   

    -- print the element one by one
    

    -- using a for loop, print the index and elements
    

    -- assign value to first friend
    

    -- assign value to the last friend
    

    -- print the friends
    

end

-- define Example 2 Function
function example2()
    -- create an empty list
    

    -- add an element to the end of the list (append)
    

    -- add an element to the end of the list (append)
    

    -- add an element to the beginning of the list (insert)
    

    -- print using for in loop
    

    -- print using for in loop with index
    

    -- delete a fruit
    

    -- print using for in loop
    

end

-- define Example 3 Function
function example3()
    
end

-- define Example 4 Function
function example4()
    
end

-- call to main function, do not delete!
main()

w18problem1.lua

-- function definition
function problem1()

end

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

end

-- call to main function
main()

w18problem2.lua

-- function definition
function problem2()

end

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

end

-- call to main function
main()

w18problem3.lua

-- function definition
function problem3()

end

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

end

-- call to main function
main()

w18problem4.lua

-- function definition
function problem4()

end

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

end

-- call to main function
main()

w18problem5.lua

-- function definition
function problem5()

end

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

end

-- call to main function
main()

w18problem6.lua

-- function definition
function problem6()

end

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

end

-- call to main function
main()

playlist.lua

-- Main function
function main()
    -- Initialize playlist with 10 songs
    local playlist = {}

    local done = false
    while not done do
        print("My Playlist Manager")
        print("1. View playlist")
        print("2. View playlist with index positions")
        print("3. Add a song")
        print("4. Remove a song")
        print("5. Select a random song")
        print("6. Sort playlist")
        print("7. Shuffle playlist")
        print("8. Reorder playlist")
        print("9. Clear playlist")
        print("10. Exit")
        io.write("Choose an option (1-10): ")
        local choice = io.read("*n")
        io.read()
        if choice == 1 then
            print("My current playlist")


        elseif choice == 2 then
            print("My current playlist with index positions")
            
            
        elseif choice == 3 then
           print("Add a song")


        elseif choice == 4 then
            print("Remove a song")


        elseif choice == 5 then
            print("Here's a random song!")
            

        elseif choice == 6 then
            print("Sort my playlist alphabetically")
            
            
        elseif choice == 7 then
            print("Shuffle playlist")
            -- Loop through the list from the last index to the second index. 

                -- Generate a random index between 1 and i
                
                -- Swap elements at index i and j
                


        elseif choice == 8 then
            print("Reorder playlist")
            

        elseif choice == 9 then
            print("Playlist cleared.")
            

        elseif choice == 10 then
            print("Goodbye!")
            done = true
        else
            print("Invalid choice. Please try again.")
        end
    end
end

-- Call the main function
main()
PreviousWeek 17NextWeek 19

Last updated 3 months ago

Was this helpful?