Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Writing a matrix function, but not sure how to number it – Python

Write a function to do the following

 the function is called data_array_dump.py

 the inputs for this function are matrixText, rows, columns

 the output of the function is the matrixText repeated over the rows and columns but number columns and rows

 for example for the inputs matrixText="2ac4", rows=4, columns=6 the output should look like 

         1        2       3       4       5        6

  1  2ac4 2ac4 2ac4 2ac4 2ac4 2ac4

  2  2ac4 2ac4 2ac4 2ac4 2ac4 2ac4

  3  2ac4 2ac4 2ac4 2ac4 2ac4 2ac4

  4  2ac4 2ac4 2ac4 2ac4 2ac4 2ac4

So far, I can get the grid without the numbering with:

def data_array_dump(matrixText, rows, columns):
    matrix = ''
    for i in range(rows):
        for j in range(columns):
            matrix += matrixText + " "
        matrix += '\n'
    return matrix

I keep trying to put in another string or I’ve even tried lists but I’m totally stuck. Suggestions?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Rules for this task

1\. All inputs from user are pulled into script outside of function

2\. No prints inside function except while troubleshooting function

3\. A single print should used to output the result of your function to the user from outside the function

4\. No add-in libraries

>Solution :

Try:

def data_array_dump(matrixText, rows, columns):
    l_c = max(len(matrixText), len(str(columns)))
    l_r = len(str(rows))

    s = [[" " * l_r] + [f"{c:>{l_c}}" for c in range(1, columns + 1)]]
    for r in range(1, rows + 1):
        s.append([f"{r:>{l_r}}"] + [f"{matrixText:>{l_c}}"] * columns)
    return "\n".join(" ".join(row) for row in s)


print(data_array_dump(matrixText="2ac4", rows=10, columns=10))

Prints:

      1    2    3    4    5    6    7    8    9   10
 1 2ac4 2ac4 2ac4 2ac4 2ac4 2ac4 2ac4 2ac4 2ac4 2ac4
 2 2ac4 2ac4 2ac4 2ac4 2ac4 2ac4 2ac4 2ac4 2ac4 2ac4
 3 2ac4 2ac4 2ac4 2ac4 2ac4 2ac4 2ac4 2ac4 2ac4 2ac4
 4 2ac4 2ac4 2ac4 2ac4 2ac4 2ac4 2ac4 2ac4 2ac4 2ac4
 5 2ac4 2ac4 2ac4 2ac4 2ac4 2ac4 2ac4 2ac4 2ac4 2ac4
 6 2ac4 2ac4 2ac4 2ac4 2ac4 2ac4 2ac4 2ac4 2ac4 2ac4
 7 2ac4 2ac4 2ac4 2ac4 2ac4 2ac4 2ac4 2ac4 2ac4 2ac4
 8 2ac4 2ac4 2ac4 2ac4 2ac4 2ac4 2ac4 2ac4 2ac4 2ac4
 9 2ac4 2ac4 2ac4 2ac4 2ac4 2ac4 2ac4 2ac4 2ac4 2ac4
10 2ac4 2ac4 2ac4 2ac4 2ac4 2ac4 2ac4 2ac4 2ac4 2ac4
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading