Working with lists and tuples

Date: 11/11/2020
Time: 09:30-11:30

Tuples

Similar to lists, but their values can’t be modified
numbers = (10,30,50,70)
print(numbers[2])

#ERROR: assignment is not allowed
numbers[2] = 55

Loops: "while" and "for"

  • Loops are used in order to repeat similar actions.
  • We can't loop forever this will make our program crash
  • Our loops need a rule/condition that defines when they need to stop
  • The "while" statement

  • While the condition is true execute the inner code.
  • We use it when we don't know the number of times/repetitions we need to execute our code until a specific condition is met
  • a = 0
    while a < 10 :
        print(a)
        a += 1

    The "for" statement

  • Repeat the inner code for a number of times
  • We use it when we already know how many times/repetitions we need to execute our code
  • In python a for loop is done its execution when it finishes iterating a given sequence
  • for a in range(0,10):
        print(a)

    Exercises

    (check the exercises on the github repository)

    1st Exercise

    Let's pretend we have a list containing the titles of all the Computational Thinking lessons. The list is ordered following the established schedule. Below is the list:

    l_schedule = ["Introduction to Computational Thinking","Algorithms","Computability","Programming Languages","Laboratory","Organising information: ordered structures","Laboratory","Brute-force algorithms","Organising information: unordered structures","Laboratory","Recursion","Laboratory","Divide and conquer algorithms","Dynamic programming algorithms","Laboratory","Organising information: trees","Laboratory","Backtracking algorithms","Laboratory","Organising information: graphs","Laboratory","Project: specification","Greedy algorithms"]

    a) Define a function named lab_lessons() which takes l_schedule as a parameter and returns the total number of "Laboratory" lessons.

    Mark the box to see the solution
    def lab_lessons(a_list):
        count = 0
        for title in a_list:
            if title == "Laboratory":
                count += 1
        return count

    b) Define a function named all_before_lab() which takes l_schedule as a parameter and returns a list containing all the lessons scheduled before the first "Laboratory" lesson.

    Mark the box to see the solution
    def all_before_lab(a_list):
        result = []

        i = 0
        title = a_list[i]
        while title != "Laboratory":
            result.append(title)
            i += 1
            title = a_list[i]

        return result

    c) Define a function named all_before_lab_n() which takes l_schedule and a number n as parameters, and returns a list containing all the lessons scheduled before the nth laboratory lesson. (the returned list should also include the laboratory lessons)

    Mark the box to see the solution
    def all_before_lab_n(a_list,n):
        result = []

        i = 0
        count_lab = 0
        while count_lab < n:
            title = a_list[i]
            result.append(title)
            if title == "Laboratory":
                count_lab += 1
            i += 1

        return result

    d) Let's pretend we have a new list representing an extended version of the l_schedule, such that it embeds information about the date and the duration (in hours) of the each lesson. We call the new list l_schedule_extended and each of its elements is represented as a tuple: ([DATE],[HOURS],[TITLE]). For instance, the second lesson "Algorithms" will have the corresponding tuple: ("16/10/20",2,"Algorithms"). Here we have the entire l_schedule_extended:

    l_schedule_extended = [ ("14/10/20",2,"Introduction to Computational Thinking"), ("16/10/20",2,"Algorithms"), ("19/10/20",2,"Computability"), ("21/10/20",2,"Programming Languages"), ("23/10/20",2,"Laboratory"), ("26/10/20",2,"Organising information: ordered structures"), ("28/10/20",2,"Laboratory"), ("30/10/20",2,"Brute-force algorithms"), ("09/11/20",2,"Organising information: unordered structures"), ("11/11/20",2,"Laboratory"), ("13/11/20",2,"Recursion"), ("16/11/20",2,"Laboratory"), ("20/11/20",2,"Divide and conquer algorithms"), ("23/11/20",2,"Dynamic programming algorithms"), ("25/11/20",2,"Laboratory"), ("27/11/20",2,"Organising information: trees"), ("30/11/20",2,"Laboratory"), ("02/12/20",2,"Backtracking algorithms"), ("04/12/20",2,"Laboratory"), ("09/12/20",2,"Organising information: graphs"), ("11/12/20",2,"Laboratory"), ("14/12/20",2,"Project: specification"), ("16/12/20",2,"Greedy algorithms") ]

    Define a function max_lessons_hours() which takes l_schedule_extended and a number max_hours as parameters, and returns a list containing only the titles of all the lessons which could be attended with a maximum number of hours = max_hours, starting from the first lesson of the year.

    Mark the box to see the solution
    def max_lessons_hours(a_list, max_hours):
        result = []
        tot_hours = 0
        i = 0
        while tot_hours < max_hours:
            title = a_list[i][2]
            result.append(title)
            tot_hours += a_list[i][1]
            i += 1

        return result