def test_rand(self): def solution(array): result = [] step = 0 while len(array) > 0: if step > 3: step = 0 if step == 0: result.extend(array.pop(0)) elif step == 1: _results = [] for row in array: _results.append(row.pop()) result.extend(_results) elif step == 2: _results = array.pop() _results.reverse() result.extend(_results) elif step == 3: _results = [] for row in array: _results.append(row.pop(0)) _results.reverse() result.extend(_results) step += 1 return result for _ in range(100): n = rnd(1, 20) a = [[rnd(1, 1000) for _ in range(n)] for _ in range(n)] b = [x[:] for x in a] self.assertEqual(snail(a), solution(b))
def test_snail_sort_small_list(self): """ Tests that the snail sort algorithm returns a small list path sorted in winding snail order """ l = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] result = snail(l) self.assertEqual(result, [1, 2, 3, 6, 9, 8, 7, 4, 5])
def test(self): self.assertEqual(snail([[]]), []) self.assertEqual(snail([[1]]), [1]) self.assertEqual(snail([[1, 2, 3], [4, 5, 6], [7, 8, 9]]), [1, 2, 3, 6, 9, 8, 7, 4, 5]) self.assertEqual( snail([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15], [16, 17, 18, 19, 20], [21, 22, 23, 24, 25]]), [ 1, 2, 3, 4, 5, 10, 15, 20, 25, 24, 23, 22, 21, 16, 11, 6, 7, 8, 9, 14, 19, 18, 17, 12, 13 ]) self.assertEqual( snail([[1, 2, 3, 4, 5, 6], [20, 21, 22, 23, 24, 7], [19, 32, 33, 34, 25, 8], [18, 31, 36, 35, 26, 9], [17, 30, 29, 28, 27, 10], [16, 15, 14, 13, 12, 11]]), [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36 ])
def test_snail_sort_large_list(self): """ Tests that the snail sort alrogrithm returns a large list path sorted in winding snail order """ l = [[3, 78, 45, 12, 5, 3], [7, 4, 345, 76, 12, 4], [0, 0, 0, 0, 0, 0], [5456, 123, 6345, 123, 5435, 1], [1, 2, 3, 4, 5, 6], [678, 45, 12, 54, 123, 2]] result = snail(l) self.assertEqual(result, [ 3, 78, 45, 12, 5, 3, 4, 0, 1, 6, 2, 123, 54, 12, 45, 678, 1, 5456, 0, 7, 4, 345, 76, 12, 0, 5435, 5, 4, 3, 2, 123, 0, 0, 0, 123, 6345 ])
def test_handles_assorted_data_types(self): """ Tests that the snail sort alrogrithm can handle other data types than just integers """ l = [["1", {}, [], 45], ["string", None, None, { "test": "test" }], [[1, 2, 3, 4], 2, 3, 4], [4.55, 6.22, "test", ""]] result = snail(l) self.assertEqual(result, [ "1", {}, [], 45, { "test": "test" }, 4, "", "test", 6.22, 4.55, [1, 2, 3, 4], "string", None, None, 3, 2 ])