From d4e274dc776ccd13bb68b7cfa592bc5998efceba Mon Sep 17 00:00:00 2001 From: vguo Date: Sat, 31 Oct 2020 11:37:09 -0400 Subject: [PATCH 1/4] 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/4] 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/4] 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 91a9f7e2fce9b92d33a3ddf0935711bcd6a6a098 Mon Sep 17 00:00:00 2001 From: vguo Date: Thu, 5 Nov 2020 18:22:24 -0500 Subject: [PATCH 4/4] implement 1st year/09 string operations& 10 bubble sort python solution --- 1st Year/09. String Operations/string_fun.py | 48 ++++++++++++++++++++ 1st Year/10. Bubble Sort/bubble_sort.py | 40 ++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 1st Year/09. String Operations/string_fun.py create mode 100644 1st Year/10. Bubble Sort/bubble_sort.py diff --git a/1st Year/09. String Operations/string_fun.py b/1st Year/09. String Operations/string_fun.py new file mode 100644 index 0000000..cc74cc3 --- /dev/null +++ b/1st Year/09. String Operations/string_fun.py @@ -0,0 +1,48 @@ +## To implement user-defined function for string functions +def slen(str): + count = 0 + for item in str: + count += 1 + return count + + +def scomp(str_first, str_second): + if slen(str_first) != slen(str_second): + return False + + for i in range(slen(str_first)): + if str_first[i] != str_second[i]: + return False + + return True + + +def scat(str_first, str_second): + return str_first + str_second + + +if __name__ == "__main__": + str1 = input("Enter String 1: ") + str2 = input("Enter string 2: ") + + print("Lenght of String 1:", slen(str1)) + print("Length of String 2:", slen(str2)) + + if scomp(str1, str2): + print("The strings {}, {} are equal.".format(str1, str2)) + else: + print("The strings {}, {} are not equal.".format(str1, str2)) + + print("The concatenated string is: {}".format(scat(str1, str2))) + +""" +Sample output +Enter String 1: ads +Enter string 2: asdfsdf + +Lenght of String 1: 3 +Length of String 2: 7 +The strings ads, asdfsdf are not equal. +The concatenated string is: adsasdfsdf + +""" diff --git a/1st Year/10. Bubble Sort/bubble_sort.py b/1st Year/10. Bubble Sort/bubble_sort.py new file mode 100644 index 0000000..1f962cb --- /dev/null +++ b/1st Year/10. Bubble Sort/bubble_sort.py @@ -0,0 +1,40 @@ +## Program to implement bubble sort + +def bubble_sort(arr): + length = len(arr) + for i in range(length - 1): + for j in range(0, length - i - 1): + if arr[j] > arr[j + 1]: + ## Swapping the right adjacent number + t = arr[j] + arr[j] = arr[j + 1] + arr[j + 1] = t + return arr + + +if __name__ == "__main__": + input_arr = [] + num = int(input("Enter the number of numbers: ")) + print("Enter the array: ") + + for item in range(0, num): + input_arr.append(int(input())) + + print("The given array is : {}".format(input_arr)) + + print("Array after bubble sorting is: {}".format(bubble_sort(input_arr))) + exit(0) + +''' +Sample output +Enter the number of numbers: 5 +Enter the array: +3 +1 +6 +4 +8 +The given array is : [3, 1, 6, 4, 8] +Array after bubble sorting is: [1, 3, 4, 6, 8] + +''' \ No newline at end of file