Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Python] Challenge 9 (Unreviewed) #433

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
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()