Q1

Loops, loops, and more loops. And writing basic functions.

Part A

Write a function:

  • named add_one
  • takes 1 argument, a list
  • returns 1 value, a list

In this function, you should loop through each element of the argument list, add 1 to it, and then return the new list.

For example, add_one([1, 2, 3]) should return [2, 3, 4]. No imports!

HINT: I would highly recommend constructing a new list, rather than updating the old one.


In [ ]:


In [ ]:
import numpy as np
np.random.seed(7584)

list1 = np.random.randint(-100, 100, 10)
actual1 = list1 + 1
pred1 = add_one(list1.tolist())
assert set(pred1) == set(actual1.tolist())

In [ ]:
np.random.seed(68527)

list2 = np.random.randint(-100, 100, 100)
actual2 = list2 + 1
pred2 = add_one(list2.tolist())
assert set(pred2) == set(actual2.tolist())

Part B

Write a function that adds an arbitrary integer quantity to every element in a list. This is just like Part A, except instead of adding 1 to each element, you add some positive integer x to each one.

This function should:

  • be named add_to_list
  • take 2 arguments: the list, and the number
  • return a list: the new list that contains the added elements

In this function, you should loop through each element of the argument list, add the second argument to it, and then return the new list.

For example, add_to_list([1, 2, 3], 1) should return [2, 3, 4], just like in Part A. add_to_list([1, 2, 3], 5) should return [6, 7, 8]. No imports!

HINT: I would highly recommend constructing a new list, rather than updating the old one.


In [ ]:


In [ ]:
import numpy as np
np.random.seed(2846)

list1 = np.random.randint(-100, 100, 10)
num1 = 15
actual1 = list1 + num1
pred1 = add_to_list(list1.tolist(), num1)
assert set(pred1) == set(actual1.tolist())

In [ ]:
np.random.seed(68527)

list2 = np.random.randint(-100, 100, 100)
num2 = np.random.randint(0, 100)
actual2 = list2 + num2
pred2 = add_to_list(list2.tolist(), num2)
assert set(pred2) == set(actual2.tolist())

Part C

Write a function:

  • named list_of_positive_indices
  • takes 1 arguments, a list
  • returns 1 value, a list

In this function, you should loop through each element of the argument list. If the element is positive, then you should append the index number of the positive element to a new list. If not, skip that index (i.e. do nothing). Once the loop is finished, return the new list.

For example, list_of_positive_indices([1, -1, 0, 3]) should return [0, 3]. No imports!

HINT: Instead of doing a simple for element in list: loop, try using the range() function in the loop header.


In [ ]:


In [ ]:
import numpy as np
np.random.seed(986)

list1 = np.random.randint(-100, 100, 10)
indices1 = np.arange(list1.shape[0])[list1 > 0].tolist()
pred1 = list_of_positive_indices(list1.tolist())
assert set(pred1) == set(indices1)

In [ ]:
np.random.seed(578752)

list2 = np.random.randint(-100, 100, 100)
indices2 = np.arange(list2.shape[0])[list2 > 0].tolist()
pred2 = list_of_positive_indices(list2.tolist())
assert set(pred2) == set(indices2)