Tuples Worksheet
Problem 1
Write a function tuple_cat that takes as argument a tuple and returns a new tuple with one more element: the string 'cat'.
Examples:
tuple_cat((1, 2))returns tuple(1, 2, 'cat')tuple_cat(("A", "B", "C"))returns tuple('A', 'B', 'C', 'cat')
Hint: Use tuple concatenation!
# Your code hereassert tuple_cat((1,)) == (1, 'cat')
assert tuple_cat((1, 2)) == (1, 2, 'cat')
assert tuple_cat((0, 0, 0, 0)) == (0, 0, 0, 0, 'cat')Problem 2
Write a function swap_elements that takes one argument, a list. Your function should swap the first and last elements of the list in place, returning nothing.
Hint: Use multiple assignment to swap, e.g.:
a, b = b, a# Your code hereL = [1, 2, 3]
assert swap_elements(L) is None
assert L == [3, 2, 1]
L.append(0)
assert swap_elements(L) is None
assert L == [0, 2, 1, 3]
L.append(5)
assert swap_elements(L) is None
assert L == [5, 2, 1, 3, 0]Problem 3
Write a function count_six_seven that takes one argument: a list of integers. Determine which of the integers 6 or 7 appears more frequently in the list. You can assume that one of them will appear more frequently than the other. Return a tuple: the first element of the returned tuple is the int 6 or 7, and the second element of the returned tuple is how many times it appears.
Examples:
count_six_seven([1, 6, 1, 7, 6])returns tuple(6, 2)count_six_seven([7])returns tuple(7, 1)count_six_seven([6, 6, 7, 2, 7, 7])returns tuple(7, 3)
# Your code hereassert count_six_seven([6]) == (6, 1)
assert count_six_seven([6, 6, 7]) == (6, 2)
assert count_six_seven([7]) == (7, 1)
assert count_six_seven([6 if i < 5 else 7 for i in range(100)]) == (7, 95)Problem 4
Write a function char_counter that takes as argument a string and returns a tuple. The first element of the tuple should be the most common character in the string; the second element of the tuple should be how many times that character appears. Assume that there is a single most common character.
# Your code hereassert char_counter("hello") == ('l', 2)
assert char_counter("AABA") == ('A', 3)
assert char_counter("char count") == ('c', 2)
assert char_counter("A B C D") == (' ', 3)Problem 5
Write a function find_run that takes as argument a list of integers. Your function should find a sequential run of integers within the list, and return a tuple with two elements: the first and last elements of the run. Assume there is exactly one run in the list.
A sequential run is a sequence of integers where each is one greater than the previous.
find_run([1, 2, 3, 5])returns tuple(1, 3)find_run([0, 4, 5, 6, 7, 8, 1])returns tuple(4, 8)find_run([2, 0, 1, 3, 5])returns tuple(0, 1)
Note: This is a hard problem.
# Your code hereassert find_run([1, 1, 1, 2, 3, 4, 6]) == (1, 4)
assert find_run([1, 1, 2, 2, 7, 4, 6]) == (1, 2)
assert find_run([0, 0, 3, 4, 5, 0, 0, 0]) == (3, 5)
assert find_run([5, 4, 13, 14, 15, 16, 5]) == (13, 16)