Why does my output test case 0 fail in this 2D array assignment?

I am taking into to computers class using python 3 (codeskulptor3). Compiler is using python 3.5.3 . My assignment is uploaded. Also listed are the four test cases that need to pass. My code below is passing 3 test cases except for test case 0.

Test Cases:
Test Case 0 This test case checks output for correct numbers and format.

Debugging Tip: Ensure the number order, formatting, and flip functions are all accurate.

Test Case 1 This test case calls printArray function directly with alternate array and checks output for correct numbers and format.

Debugging Tip: Examine how printArray formats output.

Test Case 2 This test case calls flipVertical function directly with alternate array. It also checks if output contains the correct pattern.

Debugging Tip: See if flipVertical function flips array vertically.

Test Case 3 This test case calls flipHorizontal function directly with an alternate array and checks if the output contains the correct pattern.

Debugging Tip: Determine if flipHorizontal function flips array horizontally.

My Code:


def printArray(N):
    for r in N:
        for c in r:
            print(c,end = " ")
        print()
    print()

def flipHorizontal(N):
    Array = []
    for i in range(0,len(N)):
        pos = []
        for j in range(0,len(N[0])):
            pos.append(N[i][len(N[0])-j-1])
        Array.append(pos)
    print("Flipped horizontally:")
    printArray(Array)
   

def flipVertical(N):
    newArray = []
    for i in range(0,len(N)):
        newArray.append(N[len(N)-i-1])
    print("Flipped vertically:")
    printArray(newArray)
    

def main():
#declare the array2D
    array2D = [
               [0,2,0,0,0],
               [0,2,0,0,0],
               [0,2,2,0,0],
               [0,2,0,2,0],
               [0,2,0,0,2]
               ]
    print('The array:')
    #call printArray() to print original array2D
    printArray(array2D)
    array2D = [
               [0,2,0,0,0],
               [0,2,0,0,0],
               [0,2,2,0,0],
               [0,2,0,2,0],
               [0,2,0,0,2]
               ]
    #call flipHorizontal() to flip the array2D horizontally
    flipHorizontal(array2D)
    #declare the array2D again
    array2D = [
               [0,2,0,0,0],
               [0,2,0,0,0],
               [0,2,2,0,0],
               [0,2,0,2,0],
               [0,2,0,0,2]
               ]
#call flipVertical() to flip the array2D vertically
    flipVertical(array2D)
    
if __name__=="__main__": 
    main() #Calling main function

When I run the above code, following is the output
The array:
0 2 0 0 0
0 2 0 0 0
0 2 2 0 0
0 2 0 2 0
0 2 0 0 2

Flipped horizontally:
0 0 0 2 0
0 0 0 2 0
0 0 2 2 0
0 2 0 2 0
2 0 0 2 0

Flipped vertically:
0 2 0 0 2
0 2 0 2 0
0 2 2 0 0
0 2 0 0 0
0 2 0 0 0

It looks correct, however complier is giving error message that test case 0 is not passing

SUBMISSION STATUS: wrong answer
programming language: Python 3.x (python 3.5.3)
memory: 27712kB
execution time: 0.06s
Test cases
Test 0 - Output - Output is correct - 0
Test 1 - Function - Function printArray prints correct numbers and format - 1
Test 2 - Function - Function flipVertical prints the input array vertically flipped - 1
Flipping horizontally involves only reversing the order of arrays
Test 3 - Function - Function flipHorizontal prints the input array horizontally flipped - 1
Flipping vertically means leaving order of arrays, but reversing items in each array
Assignment 10 Master Judge - Score: 75

Can you help? Thanks

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.

markdown_Forums

Do you have an extra space after the last number in a row?

The input for the number is from the 2D array. I am not sure how I would check for the extra space? Thanks
array2D = [[0,2,0,0,0],
[0,2,0,0,0],
[0,2,2,0,0],
[0,2,0,2,0],
[0,2,0,0,2]
]

def printArray(N):
    for r in N:
        for c in r:
            print(c,end = " ")
        print()
    print()

This would seem to add an extra space after each row, as you place a space after each column.

So the printArray function would be rewritten as:

def printArray(N):
    for r in N:
        for c in r:
            print(c,end = " ")
        print()
    is that correct?

That’s not what I meant.

This is your output:

The array:
0 2 0 0 0
0 2 0 0 0
0 2 2 0 0
0 2 0 2 0
0 2 0 0 2

This is your output with underscores in place of spaces:

The array:
0_2_0_0_0_
0_2_0_0_0_
0_2_2_0_0_
0_2_0_2_0_
0_2_0_0_2_

I’m wondering if the extra ' ' at the end is tripping up the grading.

Ah, could you please correct that in the code so there are no underscores? Thanks

Also my understanding is using end = ’ ’ within the print function parentheses, lets us pri nt on the same line in Python 3

This code

def printArray(N):
    for r in N:
        for c in r:
            print(c,end = "_")
        print()
    print()

produces the output

The array:
0_2_0_0_0_
0_2_0_0_0_
0_2_2_0_0_
0_2_0_2_0_
0_2_0_0_2_

You can get the output

The array:
0_2_0_0_0
0_2_0_0_0
0_2_2_0_0
0_2_0_2_0
0_2_0_0_2

by using .join() instead. If the spaces in the output are the issue, then using .join() will fix it.

Thanks. Could you please modify code to use .join?

may also this causing you to lose points?
nowhere says to add this as far as I can see

Sample run in instructions shows the headers as part of the output

Sample Run

The array:

0 2 0 0 0

0 2 0 0 0

0 2 2 0 0

0 2 0 2 0

0 2 0 0 2

Flipped horizontally:

0 0 0 2 0

0 0 0 2 0

0 0 2 2 0

0 2 0 2 0

2 0 0 2 0

Flipped vertically:

0 2 0 0 2

0 2 0 2 0

0 2 2 0 0

0 2 0 0 0

0 2 0 0 0

Nope, sorry. But I can give you an example on how to create a string from an array.

myArray = [1, 2, 3, 4]

According to the documentation,

str. join ( iterable )
Return a string which is the concatenation of the strings in iterable . A TypeError will be raised if there are any non-string values in iterable , including bytes objects. The separator between elements is the string providing this method.

Ok… an array is iterable, so lets try join on this array.

myArray = [1, 2, 3, 4]
myString = "".join(myArray)

print(myString)

Hmm, I get an error:

Traceback (most recent call last):
  File "main.py", line 2, in <module>
    myString = "".join(myArray)
TypeError: sequence item 0: expected str instance, int found

Ah ha! The element arrays are integers rather than strings. I need to convert the array elements to strings.

myArray = [1, 2, 3, 4]
myString = "".join([str(e) for e in myArray])

print(myString)

Well, that’s closer, but I get 1234.

Googling, it looks like the "" is the separator between elements. Maybe trying something else there will help.

myArray = [1, 2, 3, 4]
myString = "_".join([str(e) for e in myArray])

print(myString)

There we go, this gives 1_2_3_4.

it’s not inside the part formatted as code so I wouldn’t consider it as such.

you can try the other suggestions first and see

I think you are right and the headdings might also be the issue, or part of it.

Unbelievable! Thank you so much for pointing out that headers dont really belong in the code. I removed all three print statements

print(" Array:")
print(“Flipped horizontally:”)
print(“Flipped vertically:”)

and that did it ! My code is now 100% accepted.

Also thanks for the example of using .join(), learnt something new

1 Like

Do you mind sharing what you changed your code to? I have been able to succesfully get a 75 on the grader but nothing seems to work when I add the " ".join.