Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import unittest
def even_elements(l):
"""
Given a list, this function returns a new list
with the with items found in the even indexes only
"""
if len(l) == 0:
return l
n = len(l)//2
results = []
for i in range(n):
results.append(l[i*2])
return results
class TestEvenElements(unittest.TestCase):
def test_empty(self):
self.assertEqual(even_elements([]), [])
def test_one(self):
self.assertEqual(even_elements([0]), [])
def test_two(self):
self.assertEqual(even_elements([0, 1]), [0])
def test_seven(self):
self.assertEqual(even_elements([0, 1, 2, 3, 4, 5, 6]), [0, 2, 4])
def test_ten(self):
self.assertEqual(even_elements([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]),
[0, 2, 4, 6, 8])
if __name__ == '__main__':
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment