Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
210-CT/Week 2.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
155 lines (95 sloc)
3.76 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Shuffle Program: O(n) | |
Trailing os program: O(n) | |
def MultiplyingMatrices(matrix1,matrix2,finalMatrix): | |
for i in range(len(matrix1)): #iterating through the rows | |
for j in range(len(matrix1)): #iterating through elements in each row | |
for k in range(len(matrix1)): #iterating through the columns | |
counter = matrix1[i][k] * matrix2[k][j] #product of the two number in matrix | |
finalMatrix[i][j] = counter + finalMatrix[i][j] #adding the product to the value in the final matrix | |
return(finalMatrix) | |
def MultiplyingMatrix(matrix1,B): | |
for i in range(len(matrix1)): #iterating through the rows | |
for j in range(len(matrix1)): #iterating through elements in each row | |
matrix1[i][j] = matrix1[i][j]*B | |
return(matrix1) | |
def AddingMatrices(matrix1,matrix2): | |
for i in range (len(matrix1)): #iterating through the rows | |
for j in range(len(matrix1)): #iterating through elements in each row | |
matrix1[i][j] = matrix1[i][j] + matrix2[i][j] | |
return(matrix1) | |
def SubtractingMatrices(matrix1,matrix2): | |
for i in range (len(matrix1)): #iterating through the rows | |
for j in range(len(matrix1)): #iterating through elements in each row | |
matrix1[i][j] = matrix1[i][j] - matrix2[i][j] | |
return(matrix1) | |
#Computing A = B*C - 2(B+C) | |
B = [[3,1],[2,4]] | |
C = [[2,10],[4,3]] | |
matrix = [[0,0],[0,0]] | |
x = MultiplyingMatrices(B,C,matrix) | |
y = AddingMatrices(B,C) | |
z = MultiplyingMatrix(y,2) | |
print("A = " + str(SubtractingMatrices(x,z))) | |
def HighestPerfectSquare(x): | |
"""Given a positive integer this function will return the highest perfect square that is either equal or less than the number""" | |
n = 1 #number to test | |
perfectSquare = 1 #current highest perfect square | |
while n**2 <= x : | |
perfectSquare = n**2 | |
n = n+1 | |
return("The Highest perfect square is >> " + str(perfectSquare)) | |
Pseudocode for finding the highest perfect square given a number | |
HighestPerfectSquare(x) | |
n <- 1 | |
perfectSquare <- 1 | |
WHILE n*n <= x | |
perfectSquare <- n*n | |
n <- n+1 | |
RETURN(perfcetSquare) | |
Task 2: | |
Shuffle Algorithm: O(n) | |
Factoral Algorithm: O(n) | |
Task 3: | |
Pseudocode for functions that add, subtract and multiple matrices. | |
These functions work by accepting two matrices which are arrays where each row in the matrix is an array within the array. | |
example: | |
matrix = [[1,2,3], | |
[4,5,6], | |
[7,8,9]] | |
AddingMatrices(matrix1,matrix2) | |
for i <- 0 to length[matrix1] | |
for j <- 0 length[matrix1] | |
matrix1[i][j] <- matrix1[i][j] + matrix2[i][j] | |
RETURN(matrix1) | |
Big O notation for this algorithm = O(n^2) | |
SubtractingMatrices(matrix1,matrix2) | |
for i <- 0 to length[matrix1] | |
for j <- 0 length[matrix1] | |
matrix1[i][j] <- matrix1[i][j] - matrix2[i][j] | |
RETURN(matrix1) | |
Big O notation for this algorithm = O(n^2) | |
when you want to multiply a matrix by and integer or a float | |
MultiplyingMatrix(matrix,x) | |
for i <- 0 to length[matrix] | |
for j <- 0 to length[matrix] | |
matrix[i][j] <- matrix[i][j] * x | |
RETURN(matrix) | |
Big O notation for this algorithm = O(n^2) | |
when you want to multiply a matrix by another matrix | |
This fuction only works if you provide a Final matrix full of 0s | |
for example | |
[[1,2],[3,4]] * [[3,5],[2,4]] | |
The resulting matrix would be a 2 by 2 matrix so the final matrix would be entered as [[0,0],[0,0]] | |
MultiplyingMatrices(matrix1,matrix2,finalMatrix) | |
for i <- 0 to length[matrix1] | |
for j <- 0 to length[matrix1] | |
for k <- 0 to length[matrix1] | |
counter <- matrix1[i][k] * matrix2[k][j] | |
finalMatrix[i][j] <- counter + finalMatrix[i][j] | |
RETURN(finalMatrix) | |
Big O notation for this algorithm = O(n^3) | |
Method of caculating A = B*C - 2*(B+C) | |
x <- MultiplyingMatrix(B,C,finalMatrix) | |
y <- AddingMatrices(B.C) | |
z <- MultiplyingMatrix(y,2) | |
finalResult <- SubtractingMatrices(x,z) | |