
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 7: Closure#
Syllabus#
A string is a sequence of characters (each character itself is also a string), revisit sequences, elements, indices
String indexing
String slicing
Negative index
Methods and invoking a method
String methods:
upper(),lower(),find(),index(),count(),strip()The keyword
inas a membership operatorThe
len()function for stringsTraversing strings, keyword
infor traversalf-strings, and string formatting options
Escape sequence, e.g.
'/n'String methods with lists as return value or argument:
split()andjoin()The
repr()function and using it to display strings with escape sequences visible
Checkpoints#
Checkpoint 7.1: Arrival Times #
Given a list of scheduled train arrivals (hours and minutes) and a delay in minutes, we need to determine the expected arrival times. The scheduled times are given as a list of strings. Each time is formatted as hh:mm for a 24-hour display. Here, hh is the number of hours between 00 and 23 written using two digits, while mm is the number of minutes between 00 and 59 written using two digits. Expected arrival times need to be formatted in the same way.
Write a function that takes as input a list of scheduled arrivals and a delay in minutes. The list may contain an arbitrary number of scheduled arrivals, but the delay is the same for all arrivals. The function should return a list of strings with expected arrival times formatted as hh:mm in 24-hour time notation with two digits for both hours and minutes. Remember to handle the case when the delay causes the arrival to be postponed until the next day.
As an example, consider the list ['12:37', '08:10'] and a delay of 25 minutes. The first scheduled arrival is 12:37. However, with a 25-minute delay, the expected arrival is 13:02. The second scheduled arrival is 08:10, but with a 25-minute delay, the expected arrival is 08:35. You can see the expected output in the code cell below.
>>> arrival_times(['12:37', '08:10'], 25)
['13:02', '08:35']
The filename and requirements are in the box below:
arrival_times.pyarrival_times(schedule, delay)
Return the arrival times given scheduled times and delay.
Parameters:
|
|
The scheduled times. |
|
|
The delay (in minutes). |
Returns:
|
The arrival times. |
Use the following script to check your function test_arrival_times.py. If your function fails the test in this script, it will also fail when you hand it in.
Checkpoint 7.2: Punctuation Ratio #
We would like to collect statistics about using commas in connection with the word and. Therefore, given a text, we first want to identify all occurrences of the lower-case word and between two spaces, that is the string ' and '. Then, we want to calculate the ratio of the cases where a comma immediately precedes ' and ' against the cases without the comma (that is, any other character immediately precedes ' and '). The ratio should be given as
Write a function that takes a string with text as input. The function should return a number giving the ratio of occurrences of ' and ' preceded by a comma against the occurrences of ' and ' not preceded by a comma. You can assume that the text does not start with ' and '. If either the numerator or the denominator is zero, the function should return 0.
Consider the text with all seven occurrences of ' and ' highlighted.
The string ' and ' is preceded by a comma four times (highlighted in blue), while it is not preceded by a comma three times (highlighted in orange). The ratio we should compute is therefore 4/3, and this is what your function should return, as seen in the code box below. Note that ' And ' and ' and, ' are not counted, as we only consider the lower-case word and between two spaces.
>>> text = ("Sara and Emma like to travel, bike, and hike, and when they are " +
... "traveling they always take their bikes, hiking shoes, and sleeping bags. " +
... "Last year, Sarah and Emma traveled to Italy, France, and Spain. And that " +
... "was fun, and, according to Sara and Emma, very expensive!")
>>> punctuation_ratio(text)
1.3333333333333333
The filename and requirements are in the box below:
punctuation_ratio.pypunctuation_ratio(text)
Return punctuation ratio.
Parameters:
|
|
Some text. |
Returns:
|
Ratio of ‘ and ‘ preceded by comma against ‘ and ‘ not preceded by comma. |
Use the following script to check your function test_punctuation_ratio.py. If your function fails the test in this script, it will also fail when you hand it in.