BYU logo Computer Science

To start this lab, download this zip file.

Lab 17 - Indexing

IMPORTANT

Before doing Unit 4 assignments, please update byu-pytest-utils (again).

conda activate cs110
pip install -U byu-pytest-utils

You should see that byu-pytest-utils version 0.3.3 is now installed.

Version 0.3.2 was required for Unit 3. For Unit 4 you’ll need version 0.3.3.

Activities

squares.py

Write a function squares(length) that creates a list of length where each position of the list is equal to its index squared.

For example:

squares(5)

Would return:

[0, 1, 4, 9, 16]

Remember: Use double asterisks ** to perform the exponential function (eg. 5**2 = 25)


factorial.py

Write a function factorial(x) that computes x!.

Remember: the factorial is defined as x!=x(x1)(x2)1x! = x \cdot (x - 1) \cdot (x - 2) \cdot \ldots \cdot 1

For example:

factorial(4)

Would return:

24

Since 4! = 4 * 3 * 2 * 1 = 24


odd_indices.py

Write a function odd_indices(list_of_numbers) that returns a list of the indices in list_of_numbers that contain an odd number.

For example:

odd_indices([2,1,8,7,9])

Would return: [1,3,4]

Since 1,7,9 are odd and in positions 1,3,4 respectively.

Remember that indexing starts at zero


wrap_list.py

Write a function wrap_list(list, length) that “wraps” or repeats the items in list over and over until it’s length is length.

For example:

wrap_list([1, 2, 3, 4], 11)

Would return:

[1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3]

even_positions.py

Write a function even_positions(list) that takes the items in the even positions of list and returns them in a new list.

For example:

even_positions(["cat", "dog", "goat", "cow", "horse"])

Would return:

["cat", "goat", "horse"]

Note: position 0 counts as even


zip_around.py

Write a function zip_around(list1, list2) that functions like zip(list1, list2) but instead of truncating the longer list to the length of the shorter list, wrap the shorter list around to the beginning.

For example:

zip_around([1, 2, 3], [1, 2, 3, 4, 5])

Would return:

[(1, 1), (2, 2), (3, 3), (1, 4), (2, 5)]

Grading

ActivityPoints
squares.py6
factorial.py6
odd_indices.py6
wrap_list.py6
even_positions.py6
zip_around.py6