diff --git a/challenge_9/python/slansford/README.md b/challenge_9/python/slansford/README.md new file mode 100644 index 000000000..6074f226e --- /dev/null +++ b/challenge_9/python/slansford/README.md @@ -0,0 +1,5 @@ +Function square_sort in challenge_9.py takes one input, a list with numbers ranging from least to greatest, and may or may not contain positive integers, negative integers, or 0, but will not be empty, and returns an output list of + +Completes unit test in an average of 0.0066 seconds. + +Uses Python 3.6.0 \ No newline at end of file diff --git a/challenge_9/python/slansford/src/challenge_9.py b/challenge_9/python/slansford/src/challenge_9.py new file mode 100644 index 000000000..a873fd3b9 --- /dev/null +++ b/challenge_9/python/slansford/src/challenge_9.py @@ -0,0 +1,25 @@ +def square_sort(inputList): + + pos = [i**2 for i in inputList if i >= 0] + neg = [i**2 for i in inputList if i < 0] + neg = neg[::-1] + + outputList = [] + + while len(outputList) != len(inputList): + + if not neg: + outputList += pos + + elif not pos: + outputList += neg + + elif pos[0] >= neg[0]: + outputList.append(neg[0]) + neg.remove(neg[0]) + + else: + outputList.append(pos[0]) + pos.remove(pos[0]) + + return outputList diff --git a/challenge_9/python/slansford/src/test.py b/challenge_9/python/slansford/src/test.py new file mode 100644 index 000000000..a0d03bddc --- /dev/null +++ b/challenge_9/python/slansford/src/test.py @@ -0,0 +1,28 @@ +import unittest +import time +from challenge_9 import square_sort + + +class Tests(unittest.TestCase): + + def test1(self): + t1 = time.time() + self.assertEqual(square_sort([-3, -2, -1, 0, 1, 2, 3]),[0, 1, 1, 4, 4, 9, 9]) + t2 = time.time() + print('Runtime test1: ' + str(t2-t1)) + + def test2(self): + t1 = time.time() + self.assertEqual(square_sort([4, 5, 6]),[16, 25, 36]) + t2 = time.time() + print('Runtime test1: ' + str(t2-t1)) + + def test3(self): + t1 = time.time() + self.assertEqual(square_sort([-11, -10, -9]),[81, 100, 121]) + t2 = time.time() + print('Runtime test1: ' + str(t2-t1)) + + +if __name__ == '__main__': + unittest.main() \ No newline at end of file