Skip to content

Commit

Permalink
Challenge 9
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbotello committed Jan 11, 2017
1 parent 0c33e17 commit 6fa1f65
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 0 deletions.
30 changes: 30 additions & 0 deletions challenge_9/python/alexbotello/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Squares
###### Language Version (Python 3.6.0)


First, we need to find and separate the positive and negative values from the original array.
Next we reverse the array holding our negative values.
This is done to ensure order will be preserved once array is squared.
Without the reversal our array becomes unsorted [-5, -4] -> [25, 16]
Iterate to compare negative and positive arrays and append the smallest value.
Continues the iteration until our resultant array matches the length of our original array.

### Compile

```
python test.py
```

```
python square.py
```

### How To Use

Run test.py to check different test cases

Run python square.py to test your own implementation
First input line is the number of elements in your array
Subsequent input lines are the array values

Unit Test Written By: slandau3
41 changes: 41 additions & 0 deletions challenge_9/python/alexbotello/src/square.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
def square_soft(input_list):
"""
Returns a list with all values squared and sorted
"""
neg = []
pos = []
squared = []

# Seperate negative and positive values
# Reverse the negative array to preserve order
# ex: [-3, -2, -1] -> [-1, -2, -3] -> [1, 4, 9]
neg = [x**2 for x in input_list if x < 0]
pos = [x**2 for x in input_list if x >= 0]
neg.reverse()

# While loop will exit when squared takes in every value from input_list
# pop() each value after appending as to avoid using a counter variable
# and possibily getting an IndexError
while len(squared) != len(input_list):
# Add the rest of pos array if neg is empty
if not neg:
squared.append(pos.pop(0))
# Add the rest of neg array if pos is empty
elif not pos:
squared.append(neg.pop(0))

elif pos[0] >= neg[0]:
squared.append(neg.pop(0))

elif neg[0] >= pos[0]:
squared.append(pos.pop(0))

return squared

if __name__ == "__main__":

# Test your own implementation
n = int(input()) # Number of elements in list
array = [int(input()) for _ in range(n)]

print(square_soft(array))
47 changes: 47 additions & 0 deletions challenge_9/python/alexbotello/src/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/usr/bin/env python3
# @author slandau3

import unittest
from square import square_soft


class Tests(unittest.TestCase):

def test1(self):
"""
Test to make sure the program can handle a simple, ordinary input with negatives, 0 and positives
:return:
"""
self.assertEqual(square_soft([-2,-1,0,1,2]), [0,1,1,4,4])

def test2(self):
"""
Test to make sure hte program can handle only negatives
:return:
"""
self.assertEqual(square_soft([-3,-2,-1]), [1,4,9])

def test3(self):
"""
Test to make sure the program can handle only positives
:return:
"""
self.assertEqual(square_soft([0,1,2,3]), [0,1,4,9])

def test4(self):
"""
Test to make sure the program can handle an input that does not contain zero
:return:
"""
self.assertEqual(square_soft([-2,-1,1,2]), [1,1,4,4])

def test5(self):
"""
Test to make sure the program can handle a list with a bunch of different lengths between each value
:return:
"""
self.assertEqual(square_soft([-100,-50,-2,-1,80,900,9001]), [1, 4, 2500, 6400, 10000, 810000, 81018001])


if __name__ == '__main__':
unittest.main()

0 comments on commit 6fa1f65

Please sign in to comment.