简体   繁体   中英

How do I call my printArray function in both my flipHorizontal and flipVertical functions in order to flip my code each time it is run?

I am currently taking a computer science course called Intro to Computer Science where I am learning Python (Python 3) and I doing an assignment having to do with flipping 2D arrays. I have written some code for my assignment already and I already have it running sequentially as it tells me to in the instructions but I am having some problems flipping it each time. What I want to know is: How do I call my printArray function in both my flipHorizontal and flipVertical functions in order to flip my code each time it is run? I have the assignment instructions and an instructor's comment on my assignment.

Here are the instructions:

在此处输入图像描述

在此处输入图像描述

Here is 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)
    return Array

def flipVertical(N):
    newArray = []
    for i in range(0,len(N)):
        newArray.append(N[len(N)-i-1])
    return newArray

Array1 = [[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]]
printArray(Array1)
flipedHor = flipHorizontal(Array1)
printArray(flipedHor)
flipedVer=flipVertical(Array1)
printArray(flipedVer)

Here is the comment:

在此处输入图像描述

If I am reading correct, you just need to add print statement in your defined functions.

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)
    printArray(Array)
    return Array

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

Array1 = [[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]]
printArray(Array1)
flipedHor = flipHorizontal(Array1)
flipedVer=flipVertical(Array1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM