0

Had a similar problem where the solution was a while loop that never met its end condition, but this time I'm using a nested for loop in another for loop, and don't see whether or not it is an infinite loop. If anybody could take a look and see what I'm missing. I have a screenshot of the terminal, you can see there's only a blank line indicating that it's still running. Here's the code:

import stdarray
import stdio


# Entry point (DO NOT EDIT).
def main():
    a = stdarray.readFloat2D()
    c = _transpose(a)
    for row in c:
        for v in row[:-1]:
            stdio.write(str(v) + ' ')
        stdio.writeln(row[-1])


# Returns the transpose of a.
def _transpose(a):
    # Get the dimensions of matrix a.
    m = len(a)  # number of rows in a
    n = len(a[0])  # number of columns in a

    # Create an n-by-m matrix c with all elements initialized to 0.0.
    c = stdarray.create2D(n, m, 0)

    # Fill in the elements of c such that c[i][j] = a[j][i], where 0 <= i < n and 0 <= j < m.
    for i in range(n):
        for j in range(m):
            c[i][j] = a[j][i]

    # Return c.
    return c


if __name__ == '__main__':
    main()
6
  • 1
    What is stdio? This is not part of python's standard libraries. Commented Apr 5, 2021 at 1:36
  • Can you add a print or two to see what is being held up on? Commented Apr 5, 2021 at 1:36
  • Neither is stdarray, I think. At least, pip didn't detect any matching thing to install. Commented Apr 5, 2021 at 1:37
  • how big is the return of stdarray.readFloat2D()? Commented Apr 5, 2021 at 1:37
  • 1
    Please provide the expected see MRE - Minimal, Reproducible Example. Show where the intermediate results deviate from the ones you expect. We should be able to paste a single block of your code into file, run it, and reproduce your problem. This also lets us test any suggestions in your context. We expect a minimal working example of the problem, including appropriate code to trace the internal operation. Your posted code fails to run. Commented Apr 5, 2021 at 1:38

1 Answer 1

1

I suppose this comes from https://introcs.cs.princeton.edu/python/code/ which I found googling

If you look at the code in stdarray.py you can see that when calling stdarray.readFloat2D() it's expecting 2 integers (one each time) for number of rows and number of columns from your input and then n floats to fill that matrix. So if you input 3 and 4 as row and col you will need to input 12 floats to fill the matrix

def readFloat2D():
    """
    Read from sys.stdin and return a two-dimensional array of floats.
    Two integers at the beginning of sys.stdin define the array's
    dimensions.
    """
    rowCount = stdio.readInt()
    colCount = stdio.readInt()
    a = create2D(rowCount, colCount, 0.0)
    for row in range(rowCount):
        for col in range(colCount):
            print(f'row {row} col {col}')
            a[row][col] = stdio.readFloat()
    return a

Example of running the program:

2
3
4.3
2.5
6.7
4.5
7.8
2.0
2 3
4.3 2.5 6.7 
4.5 7.8 2.0 

The 3 last lines is the output, the rest are the inputs.

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

Comments

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.