Last active
September 21, 2019 00:08
-
-
Save fcantor/e7a53375912adf28a1f2ce7e4726b0d9 to your computer and use it in GitHub Desktop.
Box Sorting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python2 | |
""" | |
Module Docstring | |
""" | |
import copy | |
def mergeSort(arr): | |
if len(arr) <= 1: | |
return arr | |
mid = len(arr) // 2 | |
left, right = mergeSort(arr[:mid]), mergeSort(arr[mid:]) | |
new_arr = list(arr) | |
return merge(left, right, new_arr) | |
def merge(left, right, merged): | |
left_cursor, right_cursor = 0, 0 | |
while left_cursor < len(left) and right_cursor < len(right): | |
if left[left_cursor] <= right[right_cursor]: | |
merged[left_cursor+right_cursor]=left[left_cursor] | |
left_cursor += 1 | |
else: | |
merged[left_cursor + right_cursor] = right[right_cursor] | |
right_cursor += 1 | |
for left_cursor in range(left_cursor, len(left)): | |
merged[left_cursor + right_cursor] = left[left_cursor] | |
for right_cursor in range(right_cursor, len(right)): | |
merged[left_cursor + right_cursor] = right[right_cursor] | |
return merged | |
def orderedJunctionBoxes(numberOfBoxes, boxList): | |
''' iterate through lists of lists ''' | |
new_versions = [] | |
for box in boxList: | |
wordList = box.split() | |
for word in wordList: | |
if word.isdigit(): | |
new_versions.append(box) | |
boxList.remove(box) | |
break | |
''' sort boxList ''' | |
boxList = mergeSort(boxList) | |
return boxList | |
if __name__ == "__main__": | |
""" This is executed when run from the command line """ | |
numberOfBoxes = 4 | |
boxList = ["mi2 jog mid pet", "wz3 34 54 398", "al alps cow bar", "x4 45 21 7"] | |
print(orderedJunctionBoxes(numberOfBoxes, boxList)) | |
numberOfBoxes = 6 | |
otherBoxList = ["r1 box ape bit", "br8 eat nim did", "w1 has uni gry", "b4 xi me nu", "t2 13 121 98", "f3 52, 54, 31"] | |
print(orderedJunctionBoxes(numberOfBoxes, otherBoxList)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment