This week in section we are going to focus on 2D lists, strings, and midterm review. 2D lists will be helpful for HW4, strings will be very new when you come to section but will be on the midterm as well as on assignment 5. In addtion to section this week, there will be a practice midterm BlueBook exam and a review session. Check the announcements and handouts on the course webpage for more information on these.
2D Lists
True Grid
When working with 2-dimensional lists in Python, it can often be easier to work with a "true" grid where every row in the 2-dimensional list has the same number of elements, as opposed to a "jagged" grid where each row may have a different number of elements.
Implement a function, make_true_grid(grid) that is passed in a 2-dimensional list of integers (i.e., grid) and modifies the grid passed in so that every row has the same number of elements. When your function is done, every row in the grid passed in should have the same number of elements as the longest row (row with the most elements) in the original grid passed in. For rows that are originally shorter than the longest row in the grid, 0's should be added to the ends of those rows to make them all the same size. There should be no extra rows added to the grid.
def make_true_grid(grid):
"""
>>> grid = [[2, 3], [5], [1, 2, 3, 4, 5]]
>>> make_true_grid(grid)
[[2, 3, 0, 0, 0], [5, 0, 0, 0, 0], [1, 2, 3, 4, 5]]
"""
As a side note (although it will not explicitly impact your code), if an already true grid (all the rows are the same length) or empty grid (a grid with no rows, []) is passed to your function, that grid should not be changed by your function.
def make_true_grid(grid):
# Find maximum number of columns across all rows
# (Could be a separate function)
max_cols = 0
for i in range(len(grid)):
cols = len(grid[i])
if cols > max_cols:
max_cols = cols
# Pad each row to have extra elements (0’s), so grid is square
for i in range(len(grid)):
cols = len(grid[i])
for j in range(max_cols - cols):
grid[i].append(0)
Enumerating a Function
Implement the following function
def enumerate(lst):
"""
returns a nested list where each element is a list containing
the index of an element in the original list and the element itself.
These lists should appear in increasing order of indices.
>>> enumerate(['cs106a', 'is', 'super', 'fun'])
[[0, 'cs106a'], [1, 'is'], [2, 'super'], [3, 'fun']]
>>> enumerate(['hello'])
[[0, 'hello']]
>>> enumerate([])
[]
"""
def enumerate(lst):
enum_lsts = []
for i in range(len(lst)):
val = lst[i]
enum_lsts.append([i, val])
return enum_lsts
Strings
Double Char
Implement the following function
def double_char(s):
"""
Given string s. Return a new string that has 2 chars for every char in s.
>>> double_char('Hello')
'HHeelllloo'
"""
def double_char(s):
result = ''
for i in range(len(s)):
result = result + s[i] + s[i]
return result
Catty
Implement the following function
def catty(s):
"""
Given a string s, return a new string made of the chars from s, whenever the chars in s are exactly 'c' 'a' or 't' (not case sensitive).
>>> catty('xCtxxxTacx')
'CtTac'
"""
def catty(s):
result = ''
for i in range(len(s)):
# Decomp var: save lower form in var
low = s[i].lower()
if low == 'c' or low == 'a' or low == 't':
result += s[i] # Use original, not low
return result
Find Snps
Implement the following function
def find_snps(string_1, string_2):
"""
Given two equal length strings, return the number of indices
where the two strings have different characters.
>>> find_snps("ATGCC", "ATTCA")
2
"""
def find_snps(string_1, string_2):
num_snps = 0
for i in range(len(dna_1)):
ch_1 = dna_1[i]
ch_2 = dna_2[i]
if ch_1 != ch_2:
num_snps += 1
return num_snps
Midterm Review
If you are feeling good about 2D lists and strings, feel free to work through any of the problems from previous sections or from the practice BlueBook exam.