Skip to content

Commit d1f02b0

Browse files
author
Steven Landau
committed
adding challenge 9
1 parent 0c8f894 commit d1f02b0

File tree

3 files changed

+136
-0
lines changed

3 files changed

+136
-0
lines changed

challenge_9/README.md

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
Squares
2+
=======
3+
Idea
4+
-----
5+
You are given an array of integers. The array will be sorted and may contain negative values. The array will go from least to greatest. Your job is to square everything in the array, then resort it. No problem right? Just a simple for loop to square every element, if that and call .sort or something right? Nope. The array must be resorted in O(N) time.
6+
7+
Example: [-2,-1,0,1,2] should be returned from your function as [0,1,1,4,4].
8+
9+
[0,1,2] -> [0,1,4]
10+
11+
[-5,-4,-3,-2] -> [4,9,16,25]
12+
13+
[1,2] -> [1,4]
14+
15+
Notes
16+
* The array will always be sorted before being passed into your function
17+
* The array may or may not contain negative values
18+
* The array may or may not contain 0
19+
* The array may or may not contain positive values
20+
* The array will not be empty
21+
22+
Hint: There is no limit to the space complexity for this challenge.
23+
24+
Testing
25+
-----
26+
Testing is rather straightforward. Simply create an array with/without negatives, 0 and positives, then feed it into your function and verify it by hand.

challenge_9/python/slandau3/square.py

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#!/usr/bin/env python3
2+
# @author slandau3
3+
4+
5+
def reorder(input: list) -> list:
6+
neg_index, pos_index = find_neg_and_pos_index(input)
7+
8+
# Square the list. Can also be done with a list comprehension (probably)
9+
for i in range(len(input)):
10+
input[i] **= 2
11+
12+
re_ordered = []
13+
14+
# This is the part where we go through the square list and essentially re order it.
15+
# We kept track of the index where the least negative integer was so we can re order this in O(N) time
16+
# Think about it this way [-3, 0, 2]. neg_index would be 0 and pos_index would be 1.
17+
# The squared list would be [9, 0, 4]
18+
# We would now compare 9 and 0, we see that 0 is smaller so that gets appended to re_ordered first.
19+
# We then increment pos_index so it is now 2 (which equals 4 in this case)
20+
# we now compare 4 and 9 and see that 4 is smaller so that gets appended to re_ordered
21+
# Go around again and append 9
22+
while len(re_ordered) != len(input):
23+
if input[neg_index] < input[pos_index]:
24+
# if the input at negative index is smaller, append input[neg_index] to the new list
25+
re_ordered.append(input[neg_index])
26+
neg_index -= 1
27+
elif input[neg_index] >= input[pos_index]:
28+
# if the input at both indexes is the same or pos_index's value is smaller
29+
# append input[pos_index] to re_ordered
30+
re_ordered.append(input[pos_index])
31+
if pos_index != len(input)-1:
32+
# We don't want to get an index out of bounds error so don't do anything if we hit
33+
# the last value
34+
pos_index += 1
35+
36+
return re_ordered
37+
38+
39+
def find_neg_and_pos_index(sorted_list: list):
40+
"""
41+
Finds the least negative, negative value and the least positive, positive value (including 0).
42+
:param sorted_list: The (un-squared) input list
43+
:return: The negative index and positive index
44+
"""
45+
neg_index = 0
46+
pos_index = 0
47+
pos_evaluated = False
48+
for i in range(len(sorted_list)):
49+
if sorted_list[i] < 0: # find the least negative, negative number
50+
neg_index = i
51+
if sorted_list[i] >= 0 and not pos_evaluated: # Find the least positive positive number
52+
# This includes 0
53+
pos_index = i
54+
pos_evaluated = True
55+
56+
if sorted_list[neg_index] == 0:
57+
neg_index = len(sorted_list)
58+
59+
return neg_index, pos_index
60+
61+
if __name__ == '__main__':
62+
print(reorder([-100,-50,-2,-1,80,900,9001]))
63+

challenge_9/python/slandau3/test.py

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/usr/bin/env python3
2+
# @author slandau3
3+
4+
import unittest
5+
from square import reorder
6+
7+
8+
class Tests(unittest.TestCase):
9+
10+
def test1(self):
11+
"""
12+
Test to make sure the program can handle a simple, ordinary input with negatives, 0 and positives
13+
:return:
14+
"""
15+
self.assertEqual(reorder([-2,-1,0,1,2]), [0,1,1,4,4])
16+
17+
def test2(self):
18+
"""
19+
Test to make sure hte program can handle only negatives
20+
:return:
21+
"""
22+
self.assertEqual(reorder([-3,-2,-1]), [1,4,9])
23+
24+
def test3(self):
25+
"""
26+
Test to make sure the program can handle only positives
27+
:return:
28+
"""
29+
self.assertEqual(reorder([0,1,2,3]), [0,1,4,9])
30+
31+
def test4(self):
32+
"""
33+
Test to make sure the program can handle an input that does not contain zero
34+
:return:
35+
"""
36+
self.assertEqual(reorder([-2,-1,1,2]), [1,1,4,4])
37+
38+
def test5(self):
39+
"""
40+
Test to make sure the program can handle a list with a bunch of different lengths between each value
41+
:return:
42+
"""
43+
self.assertEqual(reorder([-100,-50,-2,-1,80,900,9001]), [1, 4, 2500, 6400, 10000, 810000, 81018001])
44+
45+
46+
if __name__ == '__main__':
47+
unittest.main()

0 commit comments

Comments
 (0)