Reputation: 47
I am trying to set up modules, and I am fairly new to modules. I have searched a bit online and can't find an answer to this!
I am trying to create a module with some code I made for QuickSort, however I am unsure how to get an input from other files.
My code in the module is:
# An algorithm to sort a list of random numbers using QuickSort
def mainSortBody(arrayToSort, begin, end):
compareVarI = arrayToSort[begin]
lower = begin + 1
upper = end
while True:
while lower <= upper and arrayToSort[upper] >= compareVarI:
upper = upper - 1
while lower <= upper and arrayToSort[lower] <= compareVarI:
lower = lower + 1
if lower <= upper:
arrayToSort[lower], arrayToSort[upper] = arrayToSort[upper], arrayToSort[lower]
else:
break
arrayToSort[begin], arrayToSort[upper] = arrayToSort[upper], arrayToSort[begin]
return upper
def speedSort(arrayToSort, begin, end):
if begin >= end:
return
p = mainSortBody(arrayToSort, begin, end)
speedSort(arrayToSort, begin, p-1)
speedSort(arrayToSort, p+1, end)
def sort(arrayToSort):
speedSort(arrayToSort, 0, len(arrayToSort) - 1)
arrayToSort = []
sort(arrayToSort)
print(arrayToSort)
My code in the other file
import main
array = [21,31,348,283,128,348,394,586,378,1923,1381782432,12348,13284]
main.sort(array)
The Output
[]
Process finished with exit code 0
Any help would be appreciated, thank you.
Upvotes: 0
Views: 134
Reputation: 51
I am assuming the tester file; the file you would run is test.py: the test.py should look like this:
from main import sort # importing the sort function from main.py
array = [21,31,348,283,128,348,394,586,378,1923,1381782432,12348,13284]
sort(array)
print(array) # printing the array as the function returns None
and the main.py:
# An algorithm to sort a list of random numbers using QuickSort
def mainSortBody(arrayToSort, begin, end):
compareVarI = arrayToSort[begin]
lower = begin + 1
upper = end
while True:
while lower <= upper and arrayToSort[upper] >= compareVarI:
upper = upper - 1
while lower <= upper and arrayToSort[lower] <= compareVarI:
lower = lower + 1
if lower <= upper:
arrayToSort[lower], arrayToSort[upper] = arrayToSort[upper], arrayToSort[lower]
else:
break
arrayToSort[begin], arrayToSort[upper] = arrayToSort[upper], arrayToSort[begin]
return upper
def speedSort(arrayToSort, begin, end):
if begin >= end:
return
p = mainSortBody(arrayToSort, begin, end)
speedSort(arrayToSort, begin, p-1)
speedSort(arrayToSort, p+1, end)
def sort(arrayToSort):
speedSort(arrayToSort, 0, len(arrayToSort) - 1)
# only keep the functions in this file; so removed the redundant parts or else they would also be executed.
I hope this solves your issue.
Upvotes: 1
Reputation: 2118
Correct me if I'm wrong but I believe you have it a bit backwards. main.py
should not be the module that is imported, as it should be the, well, main part of the program. You import what you need in main.py
and run it there.
Here's how you could do it:
def mainSortBody(arrayToSort, begin, end):
compareVarI = arrayToSort[begin]
lower = begin + 1
upper = end
while True:
while lower <= upper and arrayToSort[upper] >= compareVarI:
upper = upper - 1
while lower <= upper and arrayToSort[lower] <= compareVarI:
lower = lower + 1
if lower <= upper:
arrayToSort[lower], arrayToSort[upper] = (
arrayToSort[upper],
arrayToSort[lower],
)
else:
break
arrayToSort[begin], arrayToSort[upper] = arrayToSort[upper], arrayToSort[begin]
return upper
def speedSort(arrayToSort, begin, end):
if begin >= end:
return
p = mainSortBody(arrayToSort, begin, end)
speedSort(arrayToSort, begin, p - 1)
speedSort(arrayToSort, p + 1, end)
def sort(arrayToSort):
speedSort(arrayToSort, 0, len(arrayToSort) - 1)
from quicksort import sort
if __name__ == "__main__":
array = [21, 31, 348, 283, 128, 348, 394, 586, 378, 1923, 1381782432, 12348, 13284]
sort(array)
print(array)
Upvotes: 1