
Warning
This page shows last year’s version. We might still make small changes, but you’re welcome to take a look. We’ll remove this notice once the page is final.
Week 6: Closure#
Syllabus#
Lists, a list is a sequence of values
Understanding sequences, elements, and indexes.
Creating lists: enclosing the elements in square brackets
List indexing
List slicing
Lists are mutable
The keyword
isas identity operator vs. equality operator==List methods:
append(),extend(),pop(),index(),sort(),count(),copy()List operators:
+(concatenate) and*(repeat)Difference between modification and reassignment
Empty lists
Deleting list elements
Pure functions and modifier functions.
Functions
min(),max(),sum(),len()and understanding functions that accept different data typesTraversing lists
Understanding that modifying the indexing variable does not affect the list
The constructor
list()i.e.list('hej'),list(range(4))Solving problems that involve custom searches within lists.
Checkpoints#
Checkpoint 6.1: Special Occurrence #
Given a sequence of positive integers, we want to find what we call a special occurrence. A special occurrence is when the number 5 is followed by two numbers where exactly one is 7. Thus the occurrence …5, 3, 7… is a special occurrence, and so is the occurrence …5, 7, 8…, while …5, 7, 7… is not a special occurrence.
Write a function that takes as input a list of positive integers. The function should return the index of the number 5 in the first special occurrence. If no such occurrence exists, the function should return -1.
As an example, consider the sequence [2, 8, 11, 3, 12, 5, 7, 7, 11, 3, 12, 5, 2, 7, 5, 7, 2, 6]. The number 5 occurs three times in the sequence, at positions with index 5, 11, and 14. The first occurrence of the number 5 is not a special occurrence as it is followed by two 7. The second occurrence is a special occurrence as it is followed by 2 and 7. The third occurrence is a special occurrence, but it occurs later than the second occurrence. Therefore, the function should return 11, as shown below.
>>> special_occurrence([2, 8, 11, 3, 12, 5, 7, 7, 11, 3, 12, 5, 2, 7, 5, 7, 2, 6])
11
The filename and requirements are in the box below:
special_occurrence.pyspecial_occurrence(sequence)
Return the index of the first special occurrence.
Parameters:
|
|
A list with 0 or more elements. |
Returns:
|
The index of the first 5 followed by two numbers where exactly one is 7. |
Use the following script to check your function test_special_occurrence.py. If your function fails the test in this script, it will also fail when you hand it in.
Checkpoint 6.2: First Double Peak #
Given a list of numbers, we want to locate the first peak. Usually, a peak is a number that is strictly larger than its first neighbors (the number just before and the number just after). However, in this task, we want to locate a double peak, which is a number that is strictly larger than both its first and its second neighbors (the two numbers before and the two numbers after).
Write a function that takes as input a list of floats. The function should return the index of the first double peak. If there is no double peak, the function should return -1.
As an example, consider the list [1.2, 2.4, 3.1, 2.9, 3.6, 2.3, 1.9, 2.4]. The numbers from the list are also in the figure below, where the x-axis represents the index of the numbers and the y-axis represents the values of the numbers.
Considering all numbers in order, the first two values should be ignored, as they have no two neighbors before. The value 3.1 a not strictly larger than its second neighbor with the value 3.6. The value 2.9 is not a peak either, as it is not strictly larger than 3.1. The value 3.6 (red) is a double peak as it is larger than both 3.1, 2.9, 2.3 and 1.9 (gray). The function should therefore return the index of the value 3.6. which is 4, as shown in the code cell below.
>>> first_double_peak([1.2, 2.4, 3.1, 2.9, 3.6, 2.3, 1.9, 2.4])
4
The filename and requirements are in the box below:
first_double_peak.pyfirst_double_peak(sequence)
Return first number strictly larger than its first and second neighbors.
Parameters:
|
|
A list of numbers. |
Returns:
|
The index of the first peak. |
Use the following script to check your function test_first_double_peak.py. If your function fails the test in this script, it will also fail when you hand it in.