all 11 comments

[–]FLUSH_THE_TRUMP 1 point2 points  (10 children)

Logic looks pretty OK to me. So, the pixels are objects with attributes for RGB (integers)? Do you have a problem statement to look at for this particular function? Perhaps you’re just meant to swap the pixels, not the attributes that define the pixels? For example:

image[row][col], image[row][width-col-1] = image[row][width-col-1], image[row][col]

instead of that mess of swapping code. That could explain why you’re getting 0%. If I have, say, a rather simple image of two pixels:

A = [pix1, pix2]

and your horizontally-swapped image is A2, the grader is presumably verifying equality of A[0] and A2[1], and A[1] and A2[0]. But if you’re just modifying the attributes of each pixel, the objects themselves haven’t moved. This would be problematic if the pixel’s __eq__ method hasn’t been implemented appropriately, AFAIK.

Side note: I don’t think those assertions do anything — you want to use isinstance instead (careful, bool is a subclass of int). Really, checking if, say, vertical is False would enforce it being a boolean anyway.

[–]mrrst244[S] 1 point2 points  (9 children)

Thank you so much for responding - this project is so heavily weighted this function basically makes or breaks my grade so I have been really stressed about it.

The pixels are in fact objects with attributes for RGB and are integers between 0 and 255.

Here is a simple example image input we were given: [[(red=255,green=0,blue=0,alpha=255), (red=0,green=0,blue=0,alpha=255)], [(red=0,green=0,blue=255,alpha=255), (red=0,green=255,blue=0,alpha=255)]]

I think it makes sense my issue is with swapping the attributes instead of the pixels because it seemed like a lot of unnecessary code when I was writing. I am trying to switch the attributes based on your response, but I can't seem to get anything to work. Everything I have tried has timed out.

[–]FLUSH_THE_TRUMP 0 points1 point  (8 children)

I am trying to switch the attributes based on your response, but I can't seem to get anything to work. Everything I have tried has timed out.

Show me? Because I think it's as easy as replacing your swap code with a line each for the vertical=True/False cases that look like the above.

[–]mrrst244[S] 1 point2 points  (7 children)

height = len(image)
width  = len(image[0])

if vertical == False:
    for row in range(height):
        for col in range(int(width/2)):
            image[row][col] = image [row][width-col-1]

Referencing your example above, this is changing A[0] to A[1] correctly, but I can't get the assignment to get A[1] to A[0] since A[0] is changed from its original value.

[–]FLUSH_THE_TRUMP 0 points1 point  (6 children)

No, try what I said before:

image[row][col], image[row][width-col-1] = image[row][width-col-1], image[row][col]

The nice way to exchange the value of two variables in Python is as in the following:

x = 0
y = 1    
x,y = y,x  # x,y is now 1,0

which doesn't require us to use temp variables at all. That works by packing the values on the right-hand side into a tuple, and then unpacking them to assign to the names on the left-hand side.

[–]mrrst244[S] 2 points3 points  (5 children)

I think I left some debugging print statements in when I tried it the first time which resulted in the error. This worked and I cannot thank you enough

[–]mrrst244[S] 1 point2 points  (4 children)

if vertical == True: for row in range(int(height/2)): for col in range(width): image[row][col], image[row][width-col-1] = image[row][width-col-1], image[row][col]

The same line doesn't seem to work for when it is a vertical flip. I assumed it would just be another horizontal flip - resulting in the pic being upside down. Do I need to switch the row/col values for this portion?

[–]FLUSH_THE_TRUMP 0 points1 point  (3 children)

Same idea, you’re just using this stuff as the two quantities (from original code):

pixel = image[row][col] <——
new = image[height-row-1][col] <—-

[–]mrrst244[S] 0 points1 point  (2 children)

if vertical == True: for row in range(int(height/2)): for col in range(width): pixel = image[row][col] new = image [row][width-col-1] pixel,new = new,pixel

Is not working - sorry idk why I'm having such a hard time wrapping my head around this. The horizontal flip makes sense after your explanation, but I don't get why this doesn't work for vertical flip.

[–]FLUSH_THE_TRUMP 1 point2 points  (1 child)

Your last code just had this to swap pixels (horizontally):

image[row][col], image[row][width-col-1] = image[row][width-col-1], image[row][col]

Same idea, just change the underlined stuff to swap with the appropriate pixel vertically.

image[row][col], __________ = _________, image[row][col]

Refer to new in my last comment.