0% found this document useful (0 votes)
6 views2 pages

Python Week 5 (AceGrade - In)

The document contains Python functions for various mathematical and string operations. These include calculating the range of a list, checking if a number is perfect, computing the distance between two words, determining if a matrix is magic, and transposing a matrix. Each function is defined with specific input parameters and return values.

Uploaded by

Moni
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

Python Week 5 (AceGrade - In)

The document contains Python functions for various mathematical and string operations. These include calculating the range of a list, checking if a number is perfect, computing the distance between two words, determining if a matrix is magic, and transposing a matrix. Each function is defined with specific input parameters and return values.

Uploaded by

Moni
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

# *************************************************************

# Week5 ,GrPA1

def get_range(L):

max =0.0
min=99999999999.0

for i in range(len(L)):
if(L[i]> max):
max = L[i]
if(L[i]< min):
min = L[i]

return(max-min)
# *************************************************************
# Week5 ,GrPA2
def is_perfect(n):

sum = 0

for i in range(1, n):


if (n % i == 0):
sum = sum + i

if (n == sum):
return True
else:
return False
# *************************************************************
# Week5 ,GrPA3
def distance(word_1, word_2):
if(len(word_1)!=len(word_2)):
return -1

distance = 0
for i in range(len(word_1)):
chr1 = ord(word_1[i])
chr2 = ord(word_2[i])
temp = abs(chr1-chr2)
distance += temp

return distance

# *************************************************************
# Week5 ,GrPA4
def is_magic(mat):
R = len(mat)

sum = 0
magic = True
diag1sum = 0
diag2sum = 0

for i in range(R): # A for loop for row entries


rowsum = 0
colsum = 0

for j in range(R):
if (i == 0):
sum += mat[i][j]
rowsum += mat[i][j]
colsum += mat[j][i]
if(j==i):
diag1sum += mat[i][j]
if(j+i == R-1):
diag2sum += mat[i][j]

if (rowsum != sum or colsum != sum):


magic = False
break

if (diag1sum != sum or diag2sum != sum):


magic = False

if (magic):
return "YES"
else:
return "NO"

# *************************************************************
# Week5 ,GrPA5
def transpose(mat):
mat_trans = []
Rnum = len(mat)
Colnum = len(mat[0])

for i in range(Colnum): # A for loop for row entries


L = []
for j in range(Rnum):
L.append(mat[j][i])
mat_trans.append(L)

return mat_trans

"""
compute the transpose of the matrix

Argument:
mat: list of lists
Return:
mat_trans: list of lists
"""

# *************************************************************

You might also like