Search code examples
pythonloopsreverse

Reversing array without reverse function but loop isn't working


So I am trying to reverse an array from a .txt file without using the reverse function. here is what I have.

numbers = read() #creates numbers array out of the txt file
numbersrev = numbers #blank array for reverse
numLength = len(numbers) #measures the length of the array

print(numbers)
print("Array length of numbers: ", numLength)

i = numLength
i = i-1 #since the array starts at 0 instead of 1
k = 0
for k in range(8):
    numbersrev[k] = numbers[i]
   print ("The ", i," element of numbers is the ", k," element of numbersrev")
   i -= 1
   k += 1

print(numbersrev)

This is what I get after debugging on vscode:

[2, 4, 9, 11, 8, 3, 2, 5, 10]
Array length of numbers:  9
The  8  element of numbers is the  0  element of numbersrev
The  7  element of numbers is the  1  element of numbersrev
The  6  element of numbers is the  2  element of numbersrev
The  5  element of numbers is the  3  element of numbersrev
The  4  element of numbers is the  4  element of numbersrev
The  3  element of numbers is the  5  element of numbersrev
The  2  element of numbers is the  6  element of numbersrev
The  1  element of numbers is the  7  element of numbersrev
[10, 5, 2, 3, 8, 3, 2, 5, 10]

The top array is the original and the bottom array is the supposed reversal array

I cannot for the life of me find out why it stops changing the numbersrev array halfway through. Anybody know what the cause could be?


Solution

  • Okay, a few things...

    First for loops increment their variables automatically in python.

    so:

    for k in range(8):
        ...
        i -= 1
        k += 1
    

    should be:

    for k in range(8):
        ...
        i -= 1
    

    No need to manually increment k.

    Next lists are NOT arrays.

    Lists in python are very different from arrays in a language like C. Lists are mutable, and are passed by reference by default. so when you try to make an empty array:

    numbersrev = numbers #blank array for reverse
    

    you are actually referencing the same 'list' from both numbers AND numbersrev

    What you should have done is numbersrev = []

    Then in your for loop, simply append to numbersrev rather than assign.

    for k in range(numLength):
        numbersrev.append(numbers[i])
        print ("The ", i," element of numbers is the ", k," element of numbersrev")
        i -= 1
    

    Lastly

    you could/should reference the length of numbers rather than a hardcoded value in your for loop, but how you have it will still work (assuming you ONLY ever get 8 numbers)

    for k in range(numLength):
        ...
    

    All together

    numbers = read() #creates numbers array out of the txt file
    numbersrev = [] #blank array for reverse
    numLength = len(numbers) #measures the length of the array 
    
    print(numbers)
    
    print("Array length of numbers: ", numLength)
    
    
    i = numLength
    
    i = i-1 #since the array starts at 0 instead of 1
    
    for k in range(numLength):
        numbersrev.append(numbers[i])
        print ("The ", i," element of numbers is the ", k," element of numbersrev")
        i -= 1
    
    print(numbersrev)