Skip to content

Instantly share code, notes, and snippets.

@dudepare
Created October 21, 2015 09:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dudepare/5021637bf10c62e7bcba to your computer and use it in GitHub Desktop.
Save dudepare/5021637bf10c62e7bcba to your computer and use it in GitHub Desktop.
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