I just started on a project of making different types of sorting algorithms for fun, but I just can't figure out why this function is overwriting a variable.
I have a main file:
import bubble
test = [5, 4, 3, 2, 1]
def main():
print(test) # output: [5, 4, 3, 2, 1]
print(bubble.oneCycle(test)) # output: [4, 3, 2, 1, 5]
print(test) # output: [4, 3, 2, 1, 5] || and not [5, 4, 3, 2, 1]?
if __name__ == "__main__":
main()
and I have these functions in my "bubble.py" file:
def compareTwo(a, b):
if a > b:
return [b, a]
elif a < b:
return [a, b]
else:
return False
def oneCycle(arr):
for i in range(len(arr)-1):
ans = compareTwo(arr[i], arr[i+1])
arr[i] = ans[0]
arr[i+1] = ans[1]
return arr
def fullCycle(arr):
return arr
I also have a __init__.py
file:
from .bubble import compareTwo
from .bubble import oneCycle
from .bubble import fullCycle
So you can read the comments in the main file, but when I'm calling bubble.oneCycle(test)
it overwrites my test list. Why is it doing that?