Skip to content

Instantly share code, notes, and snippets.

@ialexpovad
Last active June 16, 2021 17:30
Show Gist options
  • Save ialexpovad/781720f26ad997aa37052af7ace18f06 to your computer and use it in GitHub Desktop.
Save ialexpovad/781720f26ad997aa37052af7ace18f06 to your computer and use it in GitHub Desktop.
The function return result additional two value (matricies, number, string)
def sum(A,B):
'''
The function return result
additional two matrices.
parametr A: First variable m x n
parametr B: Second variable k x p
...
return C: result additional
'''
# Ensure dimensions are valid for matrix additional
if type(A)==int and type(B)==int or type(A)==str and \
type(B)==str or type(A)==float and type(B)==float:
return A+B
else:
m,n,k,p=len(A),len(A[0]),len(B),len(B[0])
if m!=k or n!=p:
raise ArithmeticError('Not the same size.')
# Initial zeros matrix
C=[[0]*n for _ in range(n)]
# Perform element by element sum
for i in range(m):
for j in range(n):
C[i][j]=A[i][j]+B[i][j]
return C
if __name__=='__main__':
A='Жыве '
B='Беларусь!'
print(sum(A,B))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment