From 707d7375a7c2eb0d2727f54ae179a3ae2d4bcdb2 Mon Sep 17 00:00:00 2001 From: Prathima Kadari Date: Wed, 12 May 2021 22:45:16 +0530 Subject: [PATCH 1/7] Created Sum_Of_Digits.py --- SimpleAddition/Sum_Of_Digits.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 SimpleAddition/Sum_Of_Digits.py diff --git a/SimpleAddition/Sum_Of_Digits.py b/SimpleAddition/Sum_Of_Digits.py new file mode 100644 index 0000000..80a18a4 --- /dev/null +++ b/SimpleAddition/Sum_Of_Digits.py @@ -0,0 +1,20 @@ + def SumLoop(a): + a = a.split(' ') + + return int(a[0])*int(a[1]) + int(a[2]) + +def SumDig(a): + sum = 0 + i = 0 + while i < len(a): + sum = sum + int(a[i]) + i+= 1 + return str(sum) + +b = input() +c = [] +for i in range(b): + a = raw_input() + c.append(SumDig(str(SumLoop(a)))) +x = ' '.join(c) +print x From d6c3332164e245d8d0af8b58a7817676343923b3 Mon Sep 17 00:00:00 2001 From: Prathima Kadari Date: Sat, 15 May 2021 20:54:54 +0530 Subject: [PATCH 2/7] Added quick sort program --- SortingAlgorithms/quick_sort.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 SortingAlgorithms/quick_sort.py diff --git a/SortingAlgorithms/quick_sort.py b/SortingAlgorithms/quick_sort.py new file mode 100644 index 0000000..56cf57f --- /dev/null +++ b/SortingAlgorithms/quick_sort.py @@ -0,0 +1,32 @@ +# Randomize Pivot to avoid worst case, currently pivot is last element +def quickSort(A, si, ei): + if si < ei: + pi=partition(A,si,ei) + quickSort(A,si,pi-1) + quickSort(A,pi+1,ei) + + +# Utility function for partitioning the array(used in quick sort) +def partition(A, si, ei): + x = A[ei] # x is pivot, last element + i = (si-1) + for j in range(si,ei): + if A[j] <= x: + i += 1 + A[i], A[j] = A[j], A[i] + + A[i+1], A[ei] = A[ei], A[i+1] + + return i+1 + +'''===Alternate Implementation===''' +# Warning --> As this function returns the array itself, assign it to some +# variable while calling. Example--> sorted_arr=quicksort(arr) +def quicksort(arr): + if len(arr) <= 1: + return arr + pivot = arr[len(arr) / 2] + left = [x for x in arr if x < pivot] + middle = [x for x in arr if x == pivot] + right = [x for x in arr if x > pivot] + return quicksort(left) + middle + quicksort(right) From 9f16bcc4181af50739fe0c846f78fa8fd7ba76b2 Mon Sep 17 00:00:00 2001 From: Prathima Kadari Date: Sat, 15 May 2021 20:58:41 +0530 Subject: [PATCH 3/7] Delete Sum_Of_Digits.py --- SimpleAddition/Sum_Of_Digits.py | 20 -------------------- 1 file changed, 20 deletions(-) delete mode 100644 SimpleAddition/Sum_Of_Digits.py diff --git a/SimpleAddition/Sum_Of_Digits.py b/SimpleAddition/Sum_Of_Digits.py deleted file mode 100644 index 80a18a4..0000000 --- a/SimpleAddition/Sum_Of_Digits.py +++ /dev/null @@ -1,20 +0,0 @@ - def SumLoop(a): - a = a.split(' ') - - return int(a[0])*int(a[1]) + int(a[2]) - -def SumDig(a): - sum = 0 - i = 0 - while i < len(a): - sum = sum + int(a[i]) - i+= 1 - return str(sum) - -b = input() -c = [] -for i in range(b): - a = raw_input() - c.append(SumDig(str(SumLoop(a)))) -x = ' '.join(c) -print x From 6e4563a84f59f763dbb68c40383ec9312db6feb0 Mon Sep 17 00:00:00 2001 From: Prathima Kadari Date: Sat, 15 May 2021 21:07:16 +0530 Subject: [PATCH 4/7] Updated the quick sort program --- SortingAlgorithms/quick_sort.py | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/SortingAlgorithms/quick_sort.py b/SortingAlgorithms/quick_sort.py index 56cf57f..47de7fc 100644 --- a/SortingAlgorithms/quick_sort.py +++ b/SortingAlgorithms/quick_sort.py @@ -1,27 +1,31 @@ +# Quick Sort # Randomize Pivot to avoid worst case, currently pivot is last element def quickSort(A, si, ei): if si < ei: - pi=partition(A,si,ei) - quickSort(A,si,pi-1) - quickSort(A,pi+1,ei) + pi = partition(A, si, ei) + quickSort(A, si, pi-1) + quickSort(A, pi+1, ei) +# Partition # Utility function for partitioning the array(used in quick sort) def partition(A, si, ei): - x = A[ei] # x is pivot, last element + x = A[ei] # x is pivot, last element i = (si-1) - for j in range(si,ei): + for j in range(si, ei): if A[j] <= x: i += 1 A[i], A[j] = A[j], A[i] - - A[i+1], A[ei] = A[ei], A[i+1] - + A[i+1], A[ei] = A[ei], A[i+1] return i+1 - -'''===Alternate Implementation===''' + + +#'Alternate Implementation' # Warning --> As this function returns the array itself, assign it to some -# variable while calling. Example--> sorted_arr=quicksort(arr) +# variable while calling. Example--> sorted_arr=quicksort(arr) +# Quick sort + + def quicksort(arr): if len(arr) <= 1: return arr From f9de6237fede484b0d7e53ac2c438614c6085f5a Mon Sep 17 00:00:00 2001 From: Prathima Kadari Date: Sun, 23 May 2021 17:42:38 +0530 Subject: [PATCH 5/7] Updated quick sort program --- SortingAlgorithms/quick_sort.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/SortingAlgorithms/quick_sort.py b/SortingAlgorithms/quick_sort.py index 47de7fc..0e05348 100644 --- a/SortingAlgorithms/quick_sort.py +++ b/SortingAlgorithms/quick_sort.py @@ -1,5 +1,7 @@ -# Quick Sort +# 'Quick Sort' # Randomize Pivot to avoid worst case, currently pivot is last element + + def quickSort(A, si, ei): if si < ei: pi = partition(A, si, ei) @@ -7,7 +9,7 @@ def quickSort(A, si, ei): quickSort(A, pi+1, ei) -# Partition +# 'Partition' # Utility function for partitioning the array(used in quick sort) def partition(A, si, ei): x = A[ei] # x is pivot, last element @@ -18,12 +20,12 @@ def partition(A, si, ei): A[i], A[j] = A[j], A[i] A[i+1], A[ei] = A[ei], A[i+1] return i+1 - - -#'Alternate Implementation' + + +# Alternate Implementation # Warning --> As this function returns the array itself, assign it to some # variable while calling. Example--> sorted_arr=quicksort(arr) -# Quick sort +# 'Quick sort' def quicksort(arr): From 20e657fc78a8c829365f467791e39a0fba0f6e57 Mon Sep 17 00:00:00 2001 From: Prathima Kadari Date: Sun, 23 May 2021 17:48:04 +0530 Subject: [PATCH 6/7] Updated quicksort program --- SortingAlgorithms/quick_sort.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/SortingAlgorithms/quick_sort.py b/SortingAlgorithms/quick_sort.py index 0e05348..7b08c74 100644 --- a/SortingAlgorithms/quick_sort.py +++ b/SortingAlgorithms/quick_sort.py @@ -18,13 +18,13 @@ def partition(A, si, ei): if A[j] <= x: i += 1 A[i], A[j] = A[j], A[i] - A[i+1], A[ei] = A[ei], A[i+1] + A[i+1], A[ei] = A[ei], A[i+1] return i+1 # Alternate Implementation # Warning --> As this function returns the array itself, assign it to some -# variable while calling. Example--> sorted_arr=quicksort(arr) +# Variable while calling. Example--> sorted_arr=quicksort(arr) # 'Quick sort' @@ -36,3 +36,7 @@ def quicksort(arr): middle = [x for x in arr if x == pivot] right = [x for x in arr if x > pivot] return quicksort(left) + middle + quicksort(right) + +# pylint: disable=missing-module-docstring +# pylint: disable=missing-class-docstring +# pylint: disable=missing-function-docstring From e1fe2f3702ac8ed946e341f6982abe6b0a073213 Mon Sep 17 00:00:00 2001 From: Prathima Kadari Date: Sun, 23 May 2021 17:50:15 +0530 Subject: [PATCH 7/7] Updated quick sort --- SortingAlgorithms/quick_sort.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/SortingAlgorithms/quick_sort.py b/SortingAlgorithms/quick_sort.py index 7b08c74..6e11299 100644 --- a/SortingAlgorithms/quick_sort.py +++ b/SortingAlgorithms/quick_sort.py @@ -37,6 +37,3 @@ def quicksort(arr): right = [x for x in arr if x > pivot] return quicksort(left) + middle + quicksort(right) -# pylint: disable=missing-module-docstring -# pylint: disable=missing-class-docstring -# pylint: disable=missing-function-docstring