diff --git a/Calculator/Calcy.py b/Calculator/Calcy.py index a1c0b5183f..6f51c2676b 100644 --- a/Calculator/Calcy.py +++ b/Calculator/Calcy.py @@ -13,28 +13,24 @@ previous = 0 run = True - def performMath(): global run global previous - equation = "" - if previous == 0: - equation = input("Enter Equation:") - else: - equation = input(str(previous)) + equation = input("Enter Equation: ") if equation == 'quit': - print("GoodBye, Human..!") + print("Goodbye, Human..!") run = False - else: equation = re.sub('[a-zA-Z,:()"{}"]', '', equation) - if previous == 0: - previous = eval(equation) - else: - previous = eval(str(equation) + equation) - + try: + if previous == 0: + previous = eval(equation) + else: + previous = eval(str(previous) + equation) + except Exception as e: + print("Invalid equation. Please try again.") while run: performMath() diff --git a/DSA-Python/Algorithms/QuickSort.py b/DSA-Python/Algorithms/QuickSort.py new file mode 100644 index 0000000000..675a9970aa --- /dev/null +++ b/DSA-Python/Algorithms/QuickSort.py @@ -0,0 +1,54 @@ +# Quick sort in Python + +# function to find the partition position +def partition(array, low, high): + + # choose the rightmost element as pivot + pivot = array[high] + + # pointer for greater element + i = low - 1 + + # traverse through all elements + # compare each element with pivot + for j in range(low, high): + if array[j] <= pivot: + # if element smaller than pivot is found + # swap it with the greater element pointed by i + i = i + 1 + + # swapping element at i with element at j + (array[i], array[j]) = (array[j], array[i]) + + # swap the pivot element with the greater element specified by i + (array[i + 1], array[high]) = (array[high], array[i + 1]) + + # return the position from where partition is done + return i + 1 + +# function to perform quicksort +def quickSort(array, low, high): + if low < high: + + # find pivot element such that + # element smaller than pivot are on the left + # element greater than pivot are on the right + pi = partition(array, low, high) + + # recursive call on the left of pivot + quickSort(array, low, pi - 1) + + # recursive call on the right of pivot + quickSort(array, pi + 1, high) + + +data = [8, 7, 2, 1, 0, 9, 6] +print("Unsorted Array") +print(data) + +size = len(data) + +quickSort(data, 0, size - 1) + +print('Sorted Array in Ascending Order:') +print(data) \ No newline at end of file