# MT2 Review Pt 1

Tips for navigating the slides:
• Press O or Escape for overview mode.
• Visit this link for a nice printable version
• Press the copy icon on the upper right of code blocks to copy the code

• Lists
• Objects

### Exercise: Is it ordered?

Is a linked list ordered from least to greatest?


def ordered(s):

True
False
False
"""


### Exercise: Is it ordered? (Solution)

Is a linked list ordered from least to greatest?


def ordered(s, key=lambda x: x):

True
False
False
"""
return True
elif s.first > s.rest.first:
return False
else:
return ordered(s.rest)


### Exercise: Is it ordered? Part 2

Is it ordered when a key function is applied, like abs?


def ordered(s, key=lambda x: x):

True
False
False
True
True
False
"""


### Exercise: Is it ordered? Part 2 (Solution)

Is it ordered when a key function is applied, like abs?


def ordered(s, key=lambda x: x):

True
False
False
True
True
False
"""
return True
elif key(s.first) > key(s.rest.first):
return False
else:
return ordered(s.rest, key)


### Exercise: Sorted merged list

Create a sorted Link containing all the elements of two sorted Links.

+ =

def merge(s, t):
"""Return a sorted Link containing the elements of sorted s & t.

>>> merge(a, b)
>>> a
>>> b
"""


### Exercise: Sorted merged list (Solution)

Create a sorted Link containing all the elements of two sorted Links.

+ =

def merge(s, t):
"""Return a sorted Link containing the elements of sorted s & t.

>>> merge(a, b)
>>> a
>>> b
"""
return t
return s
elif s.first <= t.first:
else:


### Exercise: Sorted merged list II

This time, do it without creating any new Link objects.

+ =

def merge_in_place(s, t):
"""Return a sorted Link containing the elements of sorted s & t.

>>> merge_in_place(a, b)
>>> a
>>> b
"""


### Exercise: Sorted merged list II (Solution)

This time, do it without creating any new Link objects.

+ =

def merge_in_place(s, t):
"""Return a sorted Link containing the elements of sorted s & t.

>>> merge_in_place(a, b)
>>> a
>>> b
"""
return t
return s
elif s.first <= t.first:
s.rest = merge_in_place(s.rest, t)
return s
else:
t.rest = merge_in_place(s, t.rest)
return t


## Iterables & Iterators

### Exercise: Find indices

What are the indices of all elements in a list that have the smallest absolute value?


[-4, -3, -2,  3,  2,  4]  → [2, 4]
0   1   2   3   4   5


[ 1,  2,  3,  4,  5,  6]  → 
0   1   2   3   4   5


def min_abs_indices(s):
"""Indices of all elements in list s that have the smallest absolute value.

>>> min_abs_indices([-4, -3, -2, 3, 2, 4])
[2, 4]
>>> min_abs_indices([1, 2, 3, 4, 5])

"""


### Exercise: Find indices (Solution)

What are the indices of all elements in a list that have the smallest absolute value?


[-4, -3, -2,  3,  2,  4]  → [2, 4]
0   1   2   3   4   5


[ 1,  2,  3,  4,  5,  6]  → 
0   1   2   3   4   5


def min_abs_indices(s):
"""Indices of all elements in list s that have the smallest absolute value.

>>> min_abs_indices([-4, -3, -2, 3, 2, 4])
[2, 4]
>>> min_abs_indices([1, 2, 3, 4, 5])

"""
min_abs = min(map(abs, s))
return list(filter(lambda i: abs(s[i]) == min_abs, range(len(s))))
# OR
return [i for i in range(len(s)) if abs(s[i]) == min_abs]


### Exercise: Largest sum

What's the largest sum of two adjacent elements in a list? (Assume length > 1)


[-4,  -3,  -2,   3,   2,   4]  → 6
-7    -5    1    5    6


[-4,   3,  -2,  -3,   2,   -4]  → 1
-1    1    -5   -1   -2


"""Largest sum of two adjacent elements in a list s.

>>> largest_adj_sum([-4, -3, -2, 3, 2, 4])
6
>>> largest_adj_sum([-4, 3, -2, -3, 2, -4])
1
"""


### Exercise: Largest sum (Solution)

What's the largest sum of two adjacent elements in a list? (Assume length > 1)


[-4,  -3,  -2,   3,   2,   4]  → 6
-7    -5    1    5    6


[-4,   3,  -2,  -3,   2,   -4]  → 1
-1    1    -5   -1   -2