Skip to content

A1 - Introduction to Algorithms

We will be doing a group activity as a lead-up to this lesson. You may want to wait until we've done that before proceeding.

Consider the following Python code, which can be used to go through each item in a list while giving you access to both the position (aka index) and value of each item:

nums = [2, 5, 6, 8, 11, 17, 21]

for i in range(len(nums)):
    print(f"At position {i}, the value is {nums[i]}")

The important thing to recognize here is that within this loop:

  • i represents the current position in the list
  • nums[i] represents the value at the current position in the list

Tips for Completing this Assignment

  • The three problems here mimic the first three problems we did in the group activity found here.
  • See how much of this you can do without any help. If you do need help, try to get a hint from me, a friend, or ask SchoolAI by joining at this link (sign in with your SCDSB Google Account).
  • Try to avoid asking an AI for a full solution. However, if you feel that you need to do this as a last resort, look at the solution, then try to implement it yourself without looking.

When coming around to mark this, I will ask you to disclose any AI help (or other help) you received. You may want to add a comment indicating this to remind yourself. Whatever method you use, the important thing is that you've gained as deep of an understanding of these as possible. Feel free to add extension to these if you would like.

Your Task

Inside your Local folder, create a folder called Unit 4. Complete these three questions in files within that folder called a1a.py, a1b.py and a1c.py.

  1. Start with the following code:

    words = ["duck", "ball", "tree", "lake", "sun", "piano", "tree"]
    
    for i in range(len(words)):
    
    Add code that will output the positions (or indexes) in the list of that hold the word tree. You should only need two lines of code, that include an if statement.

  2. Start with the following code:

    nums = [7, 2, 8, 1, 19, 3, 15]
    highest_found = nums[0]
    
    for i in range(len(nums)):
    
    Complete the program so that the highest number in the list will be printed at the end.

  3. Start with the following code:

    nums = [7, 2, 8, 1, 19, 3, 15]
    total = 0
    
    for i in range(len(nums)):
    
    Complete the program so that the total of all numbers in the list will be printed at the end. In this program, total is an accumulator. A reminder that to increment a variable by a certain amount, we do this:

    total += some_value # increase total by some_value