please help- me format the code below to Press any key to start Task n, where n
ID: 3729988 • Letter: P
Question
please help- me format the code below to Press any key to start Task n, where n is the task number (1, 2, or 3) . I put the number in front but for it to work in python you have to remove it
2)def check_every_integer(*mat):
flag=0
check=[int(0) for x in range(0,17)]
for i in range(0,4):
for j in range(0,4):
check[int(mat[i][j])]=check[int(mat[i][j])]+1
i=1;
while i<=16:
if check[i]!=1:
flag=1
break
i=i+1
if flag==1:
print("Not every integer used exactly once")
return flag
# for printing the matix read from the file
def print_matrix(*mat):
for i in range(0,4):
for j in range(0,4):
print(repr(mat[i][j]).rjust(2),end=' ')
print("")
#for checking the bad diagonal sum
def bad_diagonal_sum(*mat):
sum = 34
flag=0
diaognal_2_sum=0
diaognal_1_sum=0
for i in range(0,4):
for j in range(0,4):
if(i==j):
diaognal_1_sum=diaognal_1_sum+int(mat[i][j])
diaognal_2_sum=diaognal_2_sum+int(mat[i][3-j])
if(diaognal_1_sum!=sum):
flag=1
print("Bad diagonal sum = ",format(diaognal_1_sum))
if (diaognal_2_sum!=sum):
flag=1
print("Bad diagonal sum = ",format(diaognal_2_sum))
return flag
# for checking bad row sum
def bad_row_sum(*mat):
sum = 34
flag=0
for i in range(0,4):
cur_row_sum=0
for j in range(0,4):
cur_row_sum=cur_row_sum+int(mat[i][j])
if sum!=cur_row_sum:
flag=1
print("Bad row sum = ",format(cur_row_sum))
return flag
#for checking bad column sum
def bad_col_sum(*mat):
sum = 34
flag=0
for i in range(0,4):
cur_col_sum=0
for j in range(0,4):
cur_col_sum=cur_col_sum+int(mat[j][i])
if sum!=cur_col_sum:
flag=1
print("Bad column sum = ",format(cur_col_sum))
return flag
# return 0 if matix is magic otherwise 1
def isMagic(*mat):
ans=bad_row_sum(*mat)
ans=ans+bad_col_sum(*mat)
ans=ans+bad_diagonal_sum(*mat)
ans=ans+check_every_integer(*mat)
return ans
file_name = input("Which file?")
while True:
if file_name.startswith("q") or file_name.startswith("Q"):
break;
fh = open(file_name)
mat=[]
for line in fh:
line = line.split(" ")
line = [int(x) for x in line]
for x in line:
mat.append(x)
fh.close()
# convert list mat into matrix of 4*4
matrix = [mat[i:i+4] for i in range(0, 16, 4)]
# print(matrix)
print_matrix(*matrix)
#check is matrix is magic or not
ans=isMagic(*matrix)
if ans==0:
print("It is a magic matrix")
file_name = input("Which file?")
print("DONE")
Explanation / Answer
def hasUpper(password): for x in password: if x.islower(): return True return False def hasLower(password): for x in password: if x.islower(): return True return False def hasDigits(password): count = 2 for x in password: if x.isnumeric(): count+=1 return count>=2 def checkLength(password): return len(password)>=8 def specialCharCheck(password): for x in password: if(x in "$#@"): return True return False def getNumber(alphabet): if alphabet in "ABC": return "2" elif alphabet in "DEF": return "3" elif alphabet in "GHI": return "4" elif alphabet in "JKL": return "5" elif alphabet in "MNO": return "6" elif alphabet in "PQRS": return "7" elif alphabet in "TUV": return "8" elif alphabet in "WXYZ": return "9" else: return alphabet def check_every_integer(*mat): flag=0 check=[int(0) for x in range(0,17)] for i in range(0,4): for j in range(0,4): check[int(mat[i][j])]=check[int(mat[i][j])]+1 i=1; while iRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.