An exercise in python but I could not understand the logic

Asked

Viewed 107 times

-1

If you can help me mainly understand I’ll be grateful

  1. The functions below constitute part of a library edited into a file, named f_matrix.py and properly installed with path assigned to the list in the sys.py file.
#
# >>> A=leia_matriz(n_l,n_c) <<<
# leia_matriz(n_l,n_c) .... Realiza a leitura de uma matriz com números inteiros
# n_l ............................... número de linhas da matriz
# n_c............................... número de colunas da matriz
#
def leia_matriz(n_l,n_c):
    matriz=[]
    for i in range(0,n_l):
        matriz_c=[]
        for j in range(0,n_c):
            print(" >>> Digite o elemento [",i+1,"][",j+1,"] da Matriz",end="")
            elemento_matriz=int(input(" ==> "))
            matriz_c.append(elemento_matriz)
        matriz.append(matriz_c)
    return matriz

#
# >>>escreva_matriz(matriz)
# escreva_matriz(matriz) .........Apresenta a matriz
#
def escreva_matriz(matriz):
    n_l=len(matriz)
    n_c=len(matriz[0])
    for i in range(0,n_l):
        for j in range(0,n_c):
            print("\t\t",matriz[i][j],end="")
        print("")
    return

#
# >>>soma_matriz(matriz1,matriz2)
# atribui a uma matriz a soma da matriz1 com a matriz2
#
def soma_matriz(matriz1,matriz2):
    nl_matriz1=len(matriz1)
    nl_matriz2=len(matriz2)
    if nl_matriz1 != nl_matriz2:
        return "Matrizes incompatíveis - número de linhas diferentes )-: "
    nc_matriz1=len(matriz1[0])
    nc_matriz2=len(matriz2[0])
    if nc_matriz1 != nc_matriz2:
        return "Matrizes incompatíveis - número de colunas diferentes )-: "
    matriz_soma=[]
    linha_matriz=[]
    for i in range(0,nl_matriz1):
        for j in range(0,nc_matriz1):
            linha_matriz.append(0)
        matriz_soma.append(linha_matriz)
        linha_matriz=[]
    for i in range(0,nl_matriz1):
        for j in range(0,nc_matriz1):
            matriz_soma[i][j]=matriz1[i][j]+matriz2[i][j]
    return matriz_  ssoma

Consider designing a program edited in the Python language that makes use of this library for:Reading, Summing two matrices, and Displaying the final result of this sum. Check the alternative where the program correctly corresponds to these operations.

to)

importf_matriz
A=leia_matriz(3,5)
B=leia_matriz(5,3)
soma_matriz(A,B)
C=escreva_matriz(linhas,colunas)

b)

importf_matriz
A=f_matriz.leia_matriz(4,3)
B=f_matriz.leia_matriz(4,3)
C=f_matriz.soma_matriz(A,B)
f_matriz.escreva_matriz(C)

c)

A=leia_matriz(3,2)
B=leia_matriz(2,3)
soma_matriz(A,B)
C=escreva_matriz(linhas,colunas)

d)

importf_matriz
    A=leia_matriz(linhas,colunas)
B=leia_matriz(linhas,colunas)
soma_matriz(A,B)
C=escreva_matriz(linhas,colunas)

and)

importf_matriz
A=leia_matriz(linhas,colunas)
B=leia_matriz(linhas,colunas)
soma_matriz(A,B)
C=escreva_matriz(linhas,colunas)

Justify your answer !

  • 1

    Your question seems to have some problems and your experience here in Stack Overflow may not be the best because of it. We want you to do well here and get what you want, but for that we need you to do your part. Here are some guidelines that will help you: Stack Overflow Survival Guide in English (short version). If the solution is very simple it is still possible for someone to do so in the comments.

1 answer

0

In python you can use the files .py in two ways. Either you run them (the file will be a "script"), or you import functions, classes, etc., from that file (the file will be a module).

In the case of that file f_matriz.py it defines some functions you want to use and for that you must import first.

If a module is called m.py and defines the functions func1, func2 and func3 you can import the whole module using import m, can import specific functions using from m import func1, func3, for example. It is also possible to import everything from the module with from m import *, but it is not very advisable.

If you import the entire module when using a function it is necessary to do something like m.func1(argumentos). In the other two cases where you matter something inside the module and not the entire module you call the function without the module name before. That is, only func1(argumentos).

This is enough for you to understand what the right option is and why.


A final detail is that yesterday has importf_matriz should be import f_matriz (with a space between import and the name of the module).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.