Q1

In this question, you'll write some basic functions.

A

Write a function add_index that adds an amount to each element of a list equal to the index of that element in the list, plus an optional offset.

  • input: 2 arguments. Required argument is a list of numbers. Optional argument (default: 0) is the offset that is added to each element, in addition to the value of the element's index.
  • output: the same list of numbers, which each element incremented by its index value

For example, add_index([1, 2, 3]) should return [1, 3, 5]. Another example: add_index([1, 2, 3], 5) should return [6, 8, 10].

You cannot use any built-in or imported functions.


In [ ]:


In [ ]:
i1 = [1, 2, 3]
a1 = [1, 3, 5]
assert set(a1) == set(add_index(i1))

i2 = [0, 0, 0, 9, 10, 11]
o2 = 12
a2 = [12, 13, 14, 24, 26, 28]
assert set(a2) == set(add_index(i2, o2))

B

Write a function, remove_odds, which deletes all the odd numbers from a list of numbers.

  • input: 1 argument, a list of numbers
  • output: the same list of numbers, but with all the odd elements stripped out

For example, remove_odds([1, 2, 3, 4, 5]) should return [2, 4]. 0 counts as even.

You cannot use any built-in or imported functions.


In [ ]:


In [ ]:
i1 = [1, 2, 3]
a1 = [2]
assert set(a1) == set(remove_odds(i1))

i2 = [0, 0, 0, 9, 10, 11]
a2 = [0, 0, 0, 10]
assert set(a2) == set(remove_odds(i2))

C

Write a function, is_palindrome, which determines whether or not a list is a palindrome.

  • input: 1 argument, a list
  • output: a boolean, True if the input list is a palindrome, False otherwise

Recall a palindrome is typically a word or sequence of words that reads identically forwards as backwards.

For example, is_palindrome([1, 2, 1]) would return True, but is_palindrome([1, 1, 2, 1]) would return False.

You cannot use any built-in or imported functions.


In [ ]:


In [ ]:
i1 = [1, 2, 1]
assert is_palindrome(i1)

i2 = [1, 1, 2, 1]
assert not is_palindrome(i2)

i3 = [1, 2, 3, 4, 5, 4, 3, 2, 1]
assert is_palindrome(i3)

i4 = [1, 2, 3, 4, 5, 5, 4, 3, 2, 1]
assert is_palindrome(i4)

i5 = [1, 2, 3, 4, 5, 5, 4, 3, 2]
assert not is_palindrome(i5)

D

Write a function strip_n that chops a certain number of elements off the front and back of a list.

  • input: 2 arguments, a list of numbers, and the number of list elements to remove from the front and back of the list
  • output: 1 list, the modified input list with specified number of leading and tailing elements removed

If the number of elements specified to strip from the list is infeasible--for example, asking to chop 3 elements off the front and back of a 1-element list--raise a ValueError.

For example, strip_n([1, 2, 3], 1) should return [2].

You cannot use any built-in functions or additional imports.


In [ ]:


In [ ]:
i1 = [1, 2, 3]
n1 = 1
a1 = [2]
assert set(a1) == set(strip_n(i1, n1))

i2 = [1, 2, 3, 4]
n2 = 2
a2 = []
assert set(a2) == set(strip_n(i2, n2))

i3 = [1, 2, 3, 4, 5]
n3 = 3
try:
    strip_n(i3, n3)
except ValueError:
    assert True
else:
    assert False