Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions challenge_9/python/slansford/README.md
Original file line number Diff line number Diff line change
@@ -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
25 changes: 25 additions & 0 deletions challenge_9/python/slansford/src/challenge_9.py
Original file line number Diff line number Diff line change
@@ -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
28 changes: 28 additions & 0 deletions challenge_9/python/slansford/src/test.py
Original file line number Diff line number Diff line change
@@ -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()