# Array Flipping Homework

Hello, I am currently taking a programming course in Python and I am using Python 3 and I am having some trouble with my assignment. I have already gotten a comment on what I need to fix in my code for it to pass in my course’s grader but I don’t understand it so I was wondering if someone could explain it a little better and possibly show me how to fix it. I have posted the instructions for the assignment, my code, and a comment below. Can someone please help me comprehend what the comment means?

The Instructions:
Using the following array, write a method to swap the image vertically.

``````@         @
@ @       @
@   @     @
@     @   @
@       @ @
@         @
``````

My Code:

``````def flipIt(array):
for i in range(len(array)):
length = len(array[i])
for j in range(length // 2):
temp = array[i][j]
array[i][j] = array[i][length - 1 - j]
array[i][length - 1 - j] = temp

#testing

pic = [['@', ' ', ' ', ' ', ' ', '@'],
['@', '@', ' ', ' ', ' ', '@'],
['@', ' ', '@', ' ', ' ', '@'],
['@', ' ', ' ', '@', ' ', '@'],
['@', ' ', ' ', ' ', '@', '@'],
['@', ' ', ' ', ' ', ' ', '@']]

flipIt(pic)
for i in pic:
for j in i:
print(j,end=' ')
print()
``````

The Comment:
Your flipIt function needs to reverse the order of the arrays - yours reverses the order of the elements inside the arrays. This can be seen if you make what you’re flipping not symmetric - for example, if you set it to be just [[1,2],[3,4]], your code results in

``````2 1
4 3
``````

when it should be

``````3 4
1 2
``````

Instead of swapping elements inside of array[i], you should be swapping the elements directly in array.

We can’t see the pictures. Can you please post the text?

I have changed your post for readability, when you post screenshots don’t add line breaks in the code that appear

@JeremyLT done

1 Like

Alrighty. when you run your code, you can see that you flip the “image” horizontally (left to right) rather than vertically (up and down).

To see this I recommend using a test image without symmetry:

``````pic = [['0', ' ', ' ', ' ', ' ', '1'],
['@', '@', ' ', ' ', ' ', '@'],
['@', ' ', '@', ' ', ' ', '@'],
['@', ' ', ' ', '@', ' ', '@'],
['@', ' ', ' ', ' ', '@', '@'],
['2', ' ', ' ', ' ', ' ', '3']]
``````

Now you can see that your result is

``````1         0
@       @ @
@     @   @
@   @     @
@ @       @
3         2
``````

``````2         3
@       @ @
@     @   @
@   @     @
@ @       @
0         1
``````

To put it in other words, you are flipping the elements in each row while you were asked to exchange the rows instead.

Do you know how I could fix that?

Sure. You need to change the part of your loop that is changing the order of each row into a loop that exchanges the rows.

Do you know which part of your code changes the position of the values inside of each row?

I had to work with my teacher to write much of this so I don’t exactly understand where that is.

Ok, lets start at the beginning.

What can you tell me about how this function does work?

``````# Function to flip the array (currently horizontal flip)
def flipIt(array):
# What does this line do?
for i in range(len(array)):
length = len(array[i])
for j in range(length // 2):
# And this part is doing some flipping. How?
temp = array[i][j]
array[i][j] = array[i][length - 1 - j]
array[i][length - 1 - j] = temp
``````

From what I can understand, I am creating a function called printIt and it’s taking in a parameter called array. I am assuming that I am using the two for loops to get the image that is being flipped to go in a vertical direction on the screen. Under the line

``````for j in range(length // 2):
``````

I am assuming that I am using a temporary variable to flip the array. That is all I can tell so far.

You are using a temporary variable to flip the array, but that isn’t part of the line you highlighted.

This is the core of the flipping part.

``````            # And this part is doing some flipping. How?
temp = array[i][j]
array[i][j] = array[i][length - 1 - j]
array[i][length - 1 - j] = temp
``````

Let me write this more simply

``````# Test array
array = [1, 2, 3, 4, 5]

# View array
print(array)

# Swap values
temp = array[0]
array[0] = array[4]
array[4] = temp

# View result of swap
print(array)
``````

Does this simplified code make sense to you? I’d run it and see if it does what you expect.

It looks like you’re creating the temp variable and assigning it array[0] and then telling it that it is equal to the last place (4th place in the array) and then switching the two places by saying that array[4] is equal to the temp variable (array[0]).

Exactly! So, if we wrap this in a loop, we can flip this entire row.

``````# Test array
array = [1, 2, 3, 4, 5]

# View array
print(array)

# Swap values
length = len(array)
for i in range(length // 2):
temp = array[i]
array[i] = array[length - 1 - i]
array[length - 1 - i] = temp

# View result of swap
print(array)
``````

The indexing might look hard to follow. Can you follow what’s going on in this case?

Try thinking about it in this way. Instead of having an array of arrays, what if you had an array of fruits:

``````pic = ['apple', 'orange', 'grape', 'banana'];
``````

What are the elements of this array?

If you need to write code to reverse the order of the elements in the array what would you do?

Would you need to go in and change the order of the letters in the fruit names, or just work with the elements themselves? What your original code was doing was equivalent to manipulating the letters of the words in the fruit array.

If written generically, there should be no difference between flipping/reversing the order of the fruit elements and reversing your array where the elements of the array are arrays instead of strings.

I know that we are creating an array with elements (1, 2, 3, 4, 5) and then printing it. I’m having a little bit of trouble understanding what these four lines are doing

``````for i in range(length // 2):
temp = array[i]
array[i] = array[length - 1 - i]
array[length - 1 - i] = temp
``````

Alrighty. Do you know what `range(length)` gives us?

Try

``````length = 5

print(range(length))
print(range(length // 2))
``````

What do these functions do?

We can also test

``````length = 5

for i in print(range(length)):
print(i)

for i in print(range(length // 2)):
print(i)
``````

It looks like it’s outputting the range of the for loop (0, 5).

Right, so this code

``````for i in range(length // 2):
temp = array[i]
array[i] = array[length - 1 - i]
array[length - 1 - i] = temp
``````

starts an `i = 0` and continues to `i = length //2 = 1`, doing the swaps.

We could also write this code as

``````i = 0
temp = array[i]
array[i] = array[length - 1 - i]
array[length - 1 - i] = temp

i = 1
temp = array[i]
array[i] = array[length - 1 - i]
array[length - 1 - i] = temp

i = 2
temp = array[i]
array[i] = array[length - 1 - i]
array[length - 1 - i] = temp
``````

Does this help show what’s going on?

1 Like

Yes. Is there a way I could implement that into my code?

Now that we are at the same point about how the the exchange works, lets look at your task.

You have your image stored as
`array[row][column]`
In your function above, you are running this switch on the columns but you want to run this switch on the rows instead.

Let’s use our new understanding to update the comments in your code

``````# Function to flip the array (currently horizontal flip)
def flipIt(array):
# Loop over all rows in your image array
for i in range(len(array)):
length = len(array[i])
# Loop over the first half of the columns
for j in range(length // 2):
# Swap the jth and (length-1-j)th column
temp = array[i][j]
array[i][j] = array[i][length - 1 - j]
array[i][length - 1 - j] = temp
``````

This swaps the columns in each row rather than swapping each row. What part should we change to swap the rows?

I know it’s one of the these three lines

``````            temp = array[i][j]
array[i][j] = array[i][length - 1 - j]
array[i][length - 1 - j] = temp
``````

but I am not sure what to change about it.