Skip to content

Instantly share code, notes, and snippets.

@winny-
Created July 24, 2016 17:36
Show Gist options
  • Save winny-/c57f808fa29afcabf000cb622039d49a to your computer and use it in GitHub Desktop.
Save winny-/c57f808fa29afcabf000cb622039d49a to your computer and use it in GitHub Desktop.
import unittest
def insertion_sort(iterable):
"""Given `iterable`, sort it into a list using insertion sort."""
L = []
iterable = iter(iterable)
while True:
try:
i = next(iterable)
except StopIteration:
return L
for idx, value in enumerate(L):
if i < value:
L.insert(idx, i)
break
else:
L.append(i)
class InsertionSortTests(unittest.TestCase):
data = [([0, 1, 2, 3], [0, 1, 2, 3]),
([4, 3, 2, 1], [1, 2, 3, 4]),
([8, 5, 7, 9], [5, 7, 8, 9])]
def test_is(self):
for test in self.data:
self.assertEqual(insertion_sort(test[0]), test[1], '{0} != {1}'.format(test[0], test[1]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment