0% found this document useful (0 votes)
12 views9 pages

Matrix Multiplication and Algorithms Guide

The document contains multiple algorithms designed by Nyi Nyi Zaw Zaw, including matrix multiplication, number-to-word conversion, XML parsing, Fibonacci sequence generation, and ASCII code conversion. Each algorithm is presented with its respective Python code and includes user input prompts for interaction. The document also discusses the performance differences between recursive and iterative approaches for generating Fibonacci numbers.

Uploaded by

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

Matrix Multiplication and Algorithms Guide

The document contains multiple algorithms designed by Nyi Nyi Zaw Zaw, including matrix multiplication, number-to-word conversion, XML parsing, Fibonacci sequence generation, and ASCII code conversion. Each algorithm is presented with its respective Python code and includes user input prompts for interaction. The document also discusses the performance differences between recursive and iterative approaches for generating Fibonacci numbers.

Uploaded by

kittyangelay123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Unit 1 - Activity 8 - Designing Algorithm Worksheet By Nyi Nyi Zaw Zaw

Q1) Create an Algorithm to multiply matrixes

def matrixformat():
x=int(input("Enter number of column for matrix:
"))
y=int(input("Enter number of row for matrix:
"))
return x,y

def formmatrix(x1,y1):
matrix=[]
for i in range(y1):
temp=[]
for j in range(x1):
n=int(input("Enter number to input:"))
temp.append(n)
matrix.append(temp)
return matrix

def printmatrix(matrix):
# to print matrix without the brackets
for i in range(len(matrix)):
for j in range(len(matrix[i])):
print(' ',matrix[i][j],end=' ')
print()
def productmatrix(a, b, c, d, e):
# Initialize the result matrix with appropriate
dimensions
C = []
for i in range(len(d)):
temp = []
for j in range(len(e[0])):
temp.append(0)
C.append(temp)

# Matrix multiplication
for i in range(len(d)):
for j in range(len(e[0])):
for k in range(len(e)):
C[i][j] += d[i][k] * e[k][j]

return C

def main():
print("====================")
print("First Matrix")
print("====================")
x1,y1=matrixformat()
matrix1=formmatrix(x1,y1)
print("====================")
print("First Matrix Result:")
printmatrix(matrix1)
print("====================")
print("Second Matrix")
print("====================")
x2,y2=matrixformat()
matrix2=formmatrix(x2,y2)
print("====================")
print("Second Matrix Result:")
printmatrix(matrix2)
print("====================")
if x1==y2:
print("Product of both matrices:")

result=productmatrix(x1,x2,y2,matrix1,matrix2)
printmatrix(result)
print("====================")
else:
print("Product is not possible for this
matrice")
main()
# By Nyi Nyi Zaw Zaw
Demo Output:
Q2) Create an Algorithm which translate numbers into words

def n2w(number):
# Define dictionaries for number to word
conversion
ones = {
0: 'zero', 1: 'one', 2: 'two', 3: 'three',
4: 'four', 5: 'five', 6: 'six',
7: 'seven', 8: 'eight', 9: 'nine'
}
teens = {
10: 'ten', 11: 'eleven', 12: 'twelve', 13:
'thirteen', 14: 'fourteen',
15: 'fifteen', 16: 'sixteen', 17:
'seventeen', 18: 'eighteen', 19: 'nineteen'
}
tens = {
20: 'twenty', 30: 'thirty', 40: 'forty',
50: 'fifty', 60: 'sixty',
70: 'seventy', 80: 'eighty', 90: 'ninety'
}
if number < 10:
return ones[number]
elif number < 20:
return teens[number]
elif number < 100:
if number % 10 != 0:
return tens[number - number % 10] + '-'
+ ones[number % 10]
else:
return tens[number - number % 10]
elif number < 1000:
if number % 100 != 0:
return ones[number // 100] + ' hundred
and ' + n2w(number % 100)
else:
return ones[number // 100] + ' hundred'
else:
return None

# Test the function


try:
number = int(input("Enter number between 0-999:
"))
print(n2w(number).title())
except (ValueError,AttributeError):
print("Invalid input")

Q3) design an algorithm which perform XML parsing

Q4) write an algorithm for first 40 Fibonacci numbers

# Fibonacci Sequence

def fibonacci(n):
if n==0 or n==1:
return n
else:
return(fibonacci(n-1)+fibonacci(n-2))
limit=int(input("Enter how many Fibonacci numbers
to print: "))
for i in range(limit):
print(fibonacci(i),end=" ")

Slower due to python itself and recursive algorithm

limit=int(input("Enter how many Fibonacci numbers


to print: "))
n1,n2=0,1
print(n1,n2,end=" ")
for i in range(limit-2):
n3=n1+n2
print(n3,end=" ")
n1,n2=n2,n3

Faster but simpler way:

Around 33 times faster

Q5) create an algorithm to write ASCII code and their respective characters

# ASCII conversions
def conversion():
Flag=True
while Flag:
try:
x=int(input("Enter choice: "))
if x==1:
y=input("Enter string: ")
for i in y:
print(i,":",ord(str(i)))
Flag=False
elif x==2:
x=input("Enter ASCII code(s)
(spaces between each code): ")
y=x.split(" ")
for i in y:
print(i,":",chr(int(i)))
Flag=False
else:
print("Invalid option!")
except ValueError:
print("Invalid Input! Try Again!")
def main():
print("","==================","")
print(' ',"Options",'')
print("(1) String to ASCII code")
print("(2) ASCII code to String")
print("","==================","")
conversion()

main()
#By Nyi Nyi Zaw Zaw
Demo Output:

Demo Output 2:

You might also like