This exercise combines indexing, list building, and a conditional.
bookends with one parameter, items (a list).items.items is empty, return an empty list.bookends([3, 7, 2, 9]) returns [3, 9]. bookends(["solo"]) returns ["solo", "solo"]. bookends([]) returns [].
Run your code to see the output, then press Submit.
import unittest
class TestBookends(unittest.TestCase):
def test_four_numbers(self):
self.assertEqual(bookends([3, 7, 2, 9]), [3, 9])
def test_one_element_appears_twice(self):
self.assertEqual(bookends(["solo"]), ["solo", "solo"])
def test_empty_list_gives_empty_list(self):
self.assertEqual(bookends([]), [])
def test_two_elements(self):
self.assertEqual(bookends(["a", "b"]), ["a", "b"])
def bookends(items):
if len(items) == 0:
return []
return [items[0], items[-1]]
print(bookends([3, 7, 2, 9]))
print(bookends(["solo"]))
print(bookends([]))
Sign in to join the discussion.
No comments yet. Be the first to ask a question.
Press Run to execute your code, or Submit to test and complete this problem.