Additional Exercises
Last updated on 2023-09-19 | Edit this page
A collection of exercises that have been either removed from or not (yet) added to the main lesson.
Both examples exchange the values of left
and
right
:
PYTHON
print(left, right)
OUTPUT
R L
In the first case we used a temporary variable temp
to
keep the value of left
before we overwrite it with the
value of right
. In the second case, right
and
left
are packed into a tuple and then unpacked into
left
and right
.
PYTHON
= []
my_list for char in "hello":
my_list.append(char)print(my_list)
PYTHON
= ''
newstring = 'Newton'
oldstring for char in oldstring:
= char + newstring
newstring print(newstring)
PYTHON
def range_overlap(ranges):
'''Return common overlap among a set of [left, right] ranges.'''
if not ranges:
# ranges is None or an empty list
return None
= ranges[0]
max_left, min_right for (left, right) in ranges[1:]:
= max(max_left, left)
max_left = min(min_right, right)
min_right if max_left >= min_right: # no overlap
return None
return (max_left, min_right)