Lists Worksheet

Back to Week 8 Notes

Problem 1

Write a function int_sum that takes an argument that is a list of numbers. The function should return a the original list with the sum of all ints added to the end.

Examples: int_sum([1, 2, 3.0]) returns: [1, 2, 3.0, 3]

# Your code here
assert int_sum([1, 2, 3.0]) == [1, 2, 3.0, 3]
assert int_sum([]) == [0]
assert int_sum([0.0]) == [0.0, 0]
assert int_sum([-1, 1]) == [-1, 1, 0]
assert int_sum(4, "hello", 4.5) == [4, "hello", 4.5, 4]

Problem 2

Write a function list_multiplier that takes an argument, a list of integers, and returns a list of the integers multiplied by the total number of integers.

Example: list_multiplier([1, 2]) returns [2, 4]

# Your code here
assert list_multiplier([]) == []
assert list_multiplier([1]) == [1]
assert list_multiplier([1, 2, 3]) == [3, 6, 9]
assert list_multiplier([0, 3]) == [0, 6]

Problem 3

Write a function min_and_max that takes an argument, a list of numbers, and returns a new list with two entries: the original list’s minimum and maximum values.

Example: min_and_max([10, 5, 23, -5]) returns: [-5, 23]

# Your code here
assert min_and_max([0, 0, 0]) == [0, 0]
assert min_and_max([10, -5]) == [-5, 10]
assert min_and_max([10, 5, 0, -10]) == [-10, 10]
assert min_and_max([-10, 0, 5, 10]) == [-10, 10]

Problem 4

Write a function rearranger that takes one argument, a list. The list will contain ints, strings, and floats.

Return a new list consisting of the same elements, in the same relative order, with all ints first, then all strings, then all floats.

# your code here
assert rearranger([1, 2, "two", 3.0, "three"]) == [1, 2, "two", "three", 3.0]
assert rearranger([1, 2.0, "two", 3.0, "three"]) == [1, "two", "three", 2.0, 3.0]
assert rearranger([1.0, 2, "two", 3.0, "three", 4]) == [2, 4, "two", "three", 1.0, 3.0]
assert rearranger([1, 2, "two", 3.0, 3, "three", 0.0, 1]) == [1, 2, 3, 1, "two", "three", 3.0, 0.0]

Problem 5

Write a function fibonacci_list that takes one argument, an integer, and returns a list of the fibonacci sequence value that are less than the int.

*Note: The fibonacci sequence starts with 1 and 1; each subsequent number in the sequence is the sum of the previous two numbers.

# your code here
assert fibonacci_list(10) == [1, 1, 2, 3, 5, 8]
assert fibonacci_list(200) == [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144]
assert fibonacci_list(20000)[15] == 987
assert fibonacci_list(20000)[18] == 4181
assert fibonacci_list(20000)[-1] == 17711

Problem 6

Write a function substrings that takes as argument a string, and returns a list of all substrings of the string. Note that the empty string '' is a substring of every string. Note also that every string is its own substring.

Your substring list doesn’t need to be in any particular order.

Examples:

  • substrings("ab") returns list ['', 'a', 'b', 'ab'] (or any list containing these same elements)
  • substrings("abc") returns list ['', 'a', 'b', 'c', 'ab', 'bc', 'abc'] (or any list containing these same elements)
# your code here
assert "CD" in substrings("ABCD") and type(substrings("ABCD")) == list
assert "BCD" in substrings("ABCD") and type(substrings("ABCD")) == list
assert "" in substrings("xok") and type(substrings("xok")) == list
assert "ok" in substrings("xok") and type(substrings("xok")) == list
assert "o" in substrings("oooo") and type(substrings("oooo")) == list
assert "oo" in substrings("oooo") and type(substrings("oooo")) == list
assert "ooo" in substrings("oooo") and type(substrings("oooo")) == list
assert "oooo" in substrings("oooo") and type(substrings("oooo")) == list

Back to Week 8 Notes