From d4e274dc776ccd13bb68b7cfa592bc5998efceba Mon Sep 17 00:00:00 2001 From: vguo Date: Sat, 31 Oct 2020 11:37:09 -0400 Subject: [PATCH 1/5] implement 1st year/02 Roots of quadratic equation python solution --- .../quadratic.py | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 1st Year/02. Roots of a quadratic equation/quadratic.py diff --git a/1st Year/02. Roots of a quadratic equation/quadratic.py b/1st Year/02. Roots of a quadratic equation/quadratic.py new file mode 100644 index 0000000..b86e8d9 --- /dev/null +++ b/1st Year/02. Roots of a quadratic equation/quadratic.py @@ -0,0 +1,54 @@ +## To compute roots of quadratic equation +import math + +def equationroots(a, b, c): + # It's not possible to find roots when a=0 & b=0 + if a == 0 and b == 0: + print("Roots cannot be determined!") + return + + # If a is 0, then equation is not quadratic, but Liner + if a == 0: + print("Liner Equation:") + print("Root is: {}".format(-c/b)) + return + + # calculating discriminate using formula + dis = b * b - 4 * a * c + sql_val = math.sqrt(abs(dis)) + + if dis > 0: + r1 = (-b + sql_val)/(2 * a) + r2 = (-b - sql_val)/(2 * a) + print("Roots are real and distinct:") + print("Root 1: {} Root 2: {}".format(r1, r2)) + ## root are real + elif dis == 0: + r1 = r2 = -b / (2 * a) + print("The roots are real and equal:") + print("Root 1: {} Root 2: {}".format(r1, r2)) + # when discriminant is less than 0 + else: + r = -b/(2 * a) + print("The roots are imaginary:") + print("Roo1: {} + i{}".format(r, sql_val)) + print("Roo1: {} - i{}".format(r, sql_val)) + return + + +if __name__ == "__main__": + + a, b, c = [int(x) for x in input("Enter the coefficients of x:").split()] + + equationroots(a, b, c) + + ## three test case + print("some test cases:") + list = [(1, 1, 1), (1, 7, 12), (1, -2, 1)] + for item in list: + print("a={} b={} c={}".format(item[0], item[1], item[2])) + equationroots(item[0], item[1], item[2]) + + + + From 86736e7aa6eb3186905a9e9cc28540c4212e2738 Mon Sep 17 00:00:00 2001 From: vguo Date: Sat, 31 Oct 2020 15:14:31 -0400 Subject: [PATCH 2/5] fix format issue with code review --- .../quadratic.py | 21 +++++-------------- 1 file changed, 5 insertions(+), 16 deletions(-) diff --git a/1st Year/02. Roots of a quadratic equation/quadratic.py b/1st Year/02. Roots of a quadratic equation/quadratic.py index b86e8d9..a4e19ab 100644 --- a/1st Year/02. Roots of a quadratic equation/quadratic.py +++ b/1st Year/02. Roots of a quadratic equation/quadratic.py @@ -10,7 +10,7 @@ def equationroots(a, b, c): # If a is 0, then equation is not quadratic, but Liner if a == 0: print("Liner Equation:") - print("Root is: {}".format(-c/b)) + print("Root is: {}".format(- c / b)) return # calculating discriminate using formula @@ -18,18 +18,18 @@ def equationroots(a, b, c): sql_val = math.sqrt(abs(dis)) if dis > 0: - r1 = (-b + sql_val)/(2 * a) - r2 = (-b - sql_val)/(2 * a) + r1 = (- b + sql_val) / (2 * a) + r2 = (- b - sql_val) / (2 * a) print("Roots are real and distinct:") print("Root 1: {} Root 2: {}".format(r1, r2)) ## root are real elif dis == 0: - r1 = r2 = -b / (2 * a) + r1 = r2 = - b / (2 * a) print("The roots are real and equal:") print("Root 1: {} Root 2: {}".format(r1, r2)) # when discriminant is less than 0 else: - r = -b/(2 * a) + r = - b / (2 * a) print("The roots are imaginary:") print("Roo1: {} + i{}".format(r, sql_val)) print("Roo1: {} - i{}".format(r, sql_val)) @@ -41,14 +41,3 @@ def equationroots(a, b, c): a, b, c = [int(x) for x in input("Enter the coefficients of x:").split()] equationroots(a, b, c) - - ## three test case - print("some test cases:") - list = [(1, 1, 1), (1, 7, 12), (1, -2, 1)] - for item in list: - print("a={} b={} c={}".format(item[0], item[1], item[2])) - equationroots(item[0], item[1], item[2]) - - - - From 9f951a937fbc97533543379bc188cdc81531389a Mon Sep 17 00:00:00 2001 From: vguo Date: Sat, 31 Oct 2020 15:17:54 -0400 Subject: [PATCH 3/5] remove last line break --- 1st Year/02. Roots of a quadratic equation/quadratic.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/1st Year/02. Roots of a quadratic equation/quadratic.py b/1st Year/02. Roots of a quadratic equation/quadratic.py index a4e19ab..a647594 100644 --- a/1st Year/02. Roots of a quadratic equation/quadratic.py +++ b/1st Year/02. Roots of a quadratic equation/quadratic.py @@ -40,4 +40,4 @@ def equationroots(a, b, c): a, b, c = [int(x) for x in input("Enter the coefficients of x:").split()] - equationroots(a, b, c) + equationroots(a, b, c) \ No newline at end of file From 33a911c0f5c49d453191fbb5b583b2f7b2a94df1 Mon Sep 17 00:00:00 2001 From: vguo Date: Mon, 2 Nov 2020 17:37:31 -0500 Subject: [PATCH 4/5] implement 1st year/07 matrix multipulication & 08 compute sinx python solution --- .../matrix_multiplication.py | 75 +++++++++++++++++++ 1st Year/08. Compute sin(x)/compute_sinx.py | 28 +++++++ 2 files changed, 103 insertions(+) create mode 100644 1st Year/07. Matrix Multiplication/matrix_multiplication.py create mode 100644 1st Year/08. Compute sin(x)/compute_sinx.py diff --git a/1st Year/07. Matrix Multiplication/matrix_multiplication.py b/1st Year/07. Matrix Multiplication/matrix_multiplication.py new file mode 100644 index 0000000..67780a2 --- /dev/null +++ b/1st Year/07. Matrix Multiplication/matrix_multiplication.py @@ -0,0 +1,75 @@ +# To compute product of two matrices by 2D Array manipulation + +# accept user input to create 2D matrix +def input_matrix(): + row, column = [int(x) for x in input("Enter rows and columns of Matrix: ").split()] + matrix = [] + print("Enter the elements of this matrix:") + for i in range(0, row): + temp = [] + for j in range(0, column): + temp.append(int(input())) + matrix.append(temp) + return matrix + + +# Print 2D matrix with correct format +def print_matrix(matrix): + for i in range(len(matrix)): + for j in range(len(matrix[0])): + print(matrix[i][j], end=" ") + print() + return + + +if __name__ == "__main__": + matrix1 = [] + matrix2 = [] + prduct_matrix = [] + # input two matrics + matrix1 = input_matrix() + print("Matrix 1 are: ") + print_matrix(matrix1) + + matrix2 = input_matrix() + print("Matrix 2 are: ") + print_matrix(matrix2) + + if len(matrix1[0]) != len(matrix2): + print("Matrices are incompatible!, try again.") + exit(0) + + # product matrix rows and cols + rows, cols = (len(matrix1), len(matrix2[0])) + + # init product matrix value + prduct_matrix = [[0 for x in range(cols)] for y in range(rows)] + + # iterate through rows of first matrix + for i in range(rows): + # iterate through columns of second matrix + for j in range(cols): + # iterate through rows of second matrix + for k in range(len(matrix2)): + prduct_matrix[i][j] += matrix1[i][k] * matrix2[k][j] + + print("Product of matrices are: ") + print_matrix(prduct_matrix) + + """ + Sample output + + Matrix 1 are: + 12 7 3 + 4 5 6 + 7 8 9 + Matrix 2 are: + 5 8 1 2 + 6 7 3 0 + 4 5 9 1 + + Product of matrices are: + 114 160 60 27 + 74 97 73 14 + 119 157 112 23 + """ \ No newline at end of file diff --git a/1st Year/08. Compute sin(x)/compute_sinx.py b/1st Year/08. Compute sin(x)/compute_sinx.py new file mode 100644 index 0000000..3d05427 --- /dev/null +++ b/1st Year/08. Compute sin(x)/compute_sinx.py @@ -0,0 +1,28 @@ +## To compute sin x using taylor series approximation +import math + +if __name__ == "__main__": + x = float(input("Enter the value in degrees: ")) + num = int(input("Enter the number of terms in Taylor series expansion: ")) + + # Convert to radians + x = (x * 3.151592) / 180 + term = sum = x + + for i in range(1, 10): + # Term closest to approximation. + term = (- term * x * x) / (2 * i * (2 * i + 1)) + sum += term + + print("Calculated =", sum) + print("Builtin function =", math.sin(x)) + +''' +sample output + +Enter the value in degrees: 90 +Enter the number of terms in Taylor series expansion: 2 +Calculated = 0.9999875016599555 +Builtin function = 0.9999875016599559 + +''' \ No newline at end of file From fa65b7b0e9a77058f61430754b751488468398ad Mon Sep 17 00:00:00 2001 From: Vicky Guo Date: Mon, 2 Nov 2020 17:39:36 -0500 Subject: [PATCH 5/5] Delete quadratic.py --- .../quadratic.py | 43 ------------------- 1 file changed, 43 deletions(-) delete mode 100644 1st Year/02. Roots of a quadratic equation/quadratic.py diff --git a/1st Year/02. Roots of a quadratic equation/quadratic.py b/1st Year/02. Roots of a quadratic equation/quadratic.py deleted file mode 100644 index a647594..0000000 --- a/1st Year/02. Roots of a quadratic equation/quadratic.py +++ /dev/null @@ -1,43 +0,0 @@ -## To compute roots of quadratic equation -import math - -def equationroots(a, b, c): - # It's not possible to find roots when a=0 & b=0 - if a == 0 and b == 0: - print("Roots cannot be determined!") - return - - # If a is 0, then equation is not quadratic, but Liner - if a == 0: - print("Liner Equation:") - print("Root is: {}".format(- c / b)) - return - - # calculating discriminate using formula - dis = b * b - 4 * a * c - sql_val = math.sqrt(abs(dis)) - - if dis > 0: - r1 = (- b + sql_val) / (2 * a) - r2 = (- b - sql_val) / (2 * a) - print("Roots are real and distinct:") - print("Root 1: {} Root 2: {}".format(r1, r2)) - ## root are real - elif dis == 0: - r1 = r2 = - b / (2 * a) - print("The roots are real and equal:") - print("Root 1: {} Root 2: {}".format(r1, r2)) - # when discriminant is less than 0 - else: - r = - b / (2 * a) - print("The roots are imaginary:") - print("Roo1: {} + i{}".format(r, sql_val)) - print("Roo1: {} - i{}".format(r, sql_val)) - return - - -if __name__ == "__main__": - - a, b, c = [int(x) for x in input("Enter the coefficients of x:").split()] - - equationroots(a, b, c) \ No newline at end of file