0

How can I write a Python function that given an array A of numbers returns an array B that has the elements of A in reverse order? WITHOUT using the reverse function in python, and instead use a for loop?

This is what I have so far and then I call the function main() in the shell and it gives me some errors and one of them is 'B' is not defined.

def ReverseArray(A):
    n = len(A)
    for i in range(0, n-1):
        B[n-i-1] = A[i]
    return (B)

def main():
    A = [13, 21, 15, 38, 49]  # Test case for ReverseArray function
    B = ReverseArray(A)
    print(B)

Where did I go wrong?

1
  • This is what it gave me Traceback (most recent call last): File "<pyshell#9>", line 1, in <module> main() File "C:\.....", line 12, in main B = ReverseArray(A) File "C:\.....", line 7, in ReverseArray B[n-i-1] = A[i] NameError: global name 'B' is not defined Commented Mar 19, 2013 at 23:23

2 Answers 2

2

Your first problem, as you say, is that you haven't defined B.

The obvious answer is to define B. What's the starting value when you're accumulating a list? Presumably any empty list, right? So:

def ReverseArray(A):
    B = []
    n = len(A)
    for i in range(0, n-1):
        B[n-i-1] = A[i]
    return (B)

The next problem you'll run into is an IndexError from that B[n-i-1] = A[i]. That's because you're trying to modify B's items in-place, so it has to have n items to start with. In other words, you want something with as many items as A. How about a copy of A?

B = list(A)

Or, if you understand list comprehensions, this might be better:

B = [None for _ in A]

Finally, your algorithm is not actually correct. For example, given your A, n will be 5, so range(0, n-1) will be [0, 1, 2, 3]. This means you never set B[0] to anything.

You may not have realized that the Python range function returns a range that excludes the stop parameter. Just range(0, n) (or, more simply, range(n)) is what you want.

Sign up to request clarification or add additional context in comments.

1 Comment

Did you read beyond the first code sample, to the part where it says "The next problem you'll run into"? Because it also tells you how to solve that problem.
2

You can iterate over a list backwards like this, then add each element to the list B.

def ReverseArray(A):
    B = list()
    for i in xrange(len(A)-1,-1,-1):
        B.append(A[i])
    return B

4 Comments

On a side note, you can reverse a list with myList[::-1].
I think this is a better solution than the OP's, and also something the prof would likely accept. But it really doesn't answer his question of "why doesn't my code work". (That being said, SO isn't really meant for "why doesn't my code work" questions, so that may not be a problem.)
I see that. His code doesn't work because he hasn't declared B in the function. And even if he instantiated it as an empty list, he's trying to access elements of B using B[n-i-1] which is impossible because nothing is in B :)
Yeah, I explained how to fix his code in my answer. As I said, I think your answer is easier to read, and harder to get wrong, and doesn't seem to use anything he shouldn't have learned yet, so definitely +1. (I thought I'd already upvoted it, but I guess not. So, +1 now.) It's worth having both answers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.