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.
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))
Write a function, remove_odds
, which deletes all the odd numbers from a list of numbers.
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))
Write a function, is_palindrome
, which determines whether or not a list is a palindrome.
True
if the input list is a palindrome, False
otherwiseRecall 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)
Write a function strip_n
that chops a certain number of elements off the front and back of a list.
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