From e6c2be5af2222613967600ef154dce4fb62aa9d8 Mon Sep 17 00:00:00 2001 From: neha3423 Date: Thu, 19 Oct 2023 12:31:16 +0530 Subject: [PATCH 01/17] neha3423 --- data_structures/arrays/kth_largest_element.py | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 data_structures/arrays/kth_largest_element.py diff --git a/data_structures/arrays/kth_largest_element.py b/data_structures/arrays/kth_largest_element.py new file mode 100644 index 000000000000..0a411e26e5fa --- /dev/null +++ b/data_structures/arrays/kth_largest_element.py @@ -0,0 +1,63 @@ +def partition(arr,low,high): + """ + Partitions list based on the pivot element + Args: + arr: The list to be partitioned + low: The lower index of the list + high: The higher index of the list + Returns: + int: The index of pivot element after partitioning + + Examples: + >>> partition([3,1,4,5,9,2,6,5,3,5],0,9) + 4 + >>> partition([7,1,4,5,9,2,6,5,8],0,8) + 1 + """ + pivot=arr[high] + i=low-1 + for j in range(low,high): + if arr[j]>= pivot: + i+=1 + arr[i], arr[j]=arr[j],arr[i] + arr[i+1],arr[high]=arr[high],arr[i+1] + return i+1 +def kth_largest_element(arr,k): + """ + Finds the kth largest element in a list. + + Args: + nums : The list of numbers. + k : The position of the desired kth largest element. + + Returns: + int: The kth largest element. + + Examples: + >>> kth_largest_element([3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5], 3) + 5 + >>> kth_largest_element([2, 5, 6, 1, 9, 3, 8, 4, 7, 3, 5], 1) + 9 + >>> kth_largest_element([9, 1, 3, 6, 7, 9, 8, 4, 2, 4, 9], 11) + 1 + >>> kth_largest_element([1, 2, 4, 3, 5, 9, 7, 6, 5, 9, 3], 0) + 'Invalid value of k' + """ + if not 1 <= k <= len(arr): + return "Invalid value of k" + low,high=0,len(arr)-1 + while low<=high: + if low>len(arr)-1 or high<0: + return "Invalid value of k" + pivot_index=partition(arr,low,high) + if pivot_index==k-1: + return arr[pivot_index] + elif pivot_index>k-1: + high=pivot_index-1 + else: + low=pivot_index+1 + return "Kth largest element not found" +if __name__=="__main__": + import doctest + doctest.testmod() + \ No newline at end of file From 93f2d3e2bdb9005dc3bf42b837cc248b53e8ea58 Mon Sep 17 00:00:00 2001 From: neha3423 Date: Thu, 19 Oct 2023 12:36:15 +0530 Subject: [PATCH 02/17] neha3423 --- data_structures/arrays/kth_largest_element.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/data_structures/arrays/kth_largest_element.py b/data_structures/arrays/kth_largest_element.py index 0a411e26e5fa..e8b091de96e8 100644 --- a/data_structures/arrays/kth_largest_element.py +++ b/data_structures/arrays/kth_largest_element.py @@ -59,5 +59,4 @@ def kth_largest_element(arr,k): return "Kth largest element not found" if __name__=="__main__": import doctest - doctest.testmod() - \ No newline at end of file + doctest.testmod() \ No newline at end of file From 53fb6870265b1362a4ef50ce8d942bfe6b5ca115 Mon Sep 17 00:00:00 2001 From: neha3423 Date: Thu, 19 Oct 2023 12:47:57 +0530 Subject: [PATCH 03/17] neha3423 --- data_structures/arrays/kth_largest_element.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/data_structures/arrays/kth_largest_element.py b/data_structures/arrays/kth_largest_element.py index e8b091de96e8..558c8e9d69a3 100644 --- a/data_structures/arrays/kth_largest_element.py +++ b/data_structures/arrays/kth_largest_element.py @@ -1,3 +1,9 @@ +""" +Given an array of integers and an integer k, find the kth largest element in the array + +https://stackoverflow.com/questions/251781/how-to-find-the-kth-largest-element-in-an-unsorted-array-of-length-n-in-on +""" + def partition(arr,low,high): """ Partitions list based on the pivot element From d78fa1109022f16cecf1509c923328b58bdb207c Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 19 Oct 2023 07:30:47 +0000 Subject: [PATCH 04/17] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/arrays/kth_largest_element.py | 46 +++++++++++-------- 1 file changed, 26 insertions(+), 20 deletions(-) diff --git a/data_structures/arrays/kth_largest_element.py b/data_structures/arrays/kth_largest_element.py index 558c8e9d69a3..07ec8aa4f68f 100644 --- a/data_structures/arrays/kth_largest_element.py +++ b/data_structures/arrays/kth_largest_element.py @@ -4,7 +4,8 @@ https://stackoverflow.com/questions/251781/how-to-find-the-kth-largest-element-in-an-unsorted-array-of-length-n-in-on """ -def partition(arr,low,high): + +def partition(arr, low, high): """ Partitions list based on the pivot element Args: @@ -20,15 +21,17 @@ def partition(arr,low,high): >>> partition([7,1,4,5,9,2,6,5,8],0,8) 1 """ - pivot=arr[high] - i=low-1 - for j in range(low,high): - if arr[j]>= pivot: - i+=1 - arr[i], arr[j]=arr[j],arr[i] - arr[i+1],arr[high]=arr[high],arr[i+1] - return i+1 -def kth_largest_element(arr,k): + pivot = arr[high] + i = low - 1 + for j in range(low, high): + if arr[j] >= pivot: + i += 1 + arr[i], arr[j] = arr[j], arr[i] + arr[i + 1], arr[high] = arr[high], arr[i + 1] + return i + 1 + + +def kth_largest_element(arr, k): """ Finds the kth largest element in a list. @@ -51,18 +54,21 @@ def kth_largest_element(arr,k): """ if not 1 <= k <= len(arr): return "Invalid value of k" - low,high=0,len(arr)-1 - while low<=high: - if low>len(arr)-1 or high<0: + low, high = 0, len(arr) - 1 + while low <= high: + if low > len(arr) - 1 or high < 0: return "Invalid value of k" - pivot_index=partition(arr,low,high) - if pivot_index==k-1: + pivot_index = partition(arr, low, high) + if pivot_index == k - 1: return arr[pivot_index] - elif pivot_index>k-1: - high=pivot_index-1 + elif pivot_index > k - 1: + high = pivot_index - 1 else: - low=pivot_index+1 + low = pivot_index + 1 return "Kth largest element not found" -if __name__=="__main__": + + +if __name__ == "__main__": import doctest - doctest.testmod() \ No newline at end of file + + doctest.testmod() From bd6d47c19790b56a54e9171d6ec6bbf5797f0191 Mon Sep 17 00:00:00 2001 From: neha3423 Date: Mon, 23 Oct 2023 19:32:50 +0530 Subject: [PATCH 05/17] neha3423 --- data_structures/arrays/kth_largest_element.py | 49 ++++++++++++------- 1 file changed, 32 insertions(+), 17 deletions(-) diff --git a/data_structures/arrays/kth_largest_element.py b/data_structures/arrays/kth_largest_element.py index 558c8e9d69a3..48b9159317ce 100644 --- a/data_structures/arrays/kth_largest_element.py +++ b/data_structures/arrays/kth_largest_element.py @@ -1,12 +1,17 @@ """ Given an array of integers and an integer k, find the kth largest element in the array -https://stackoverflow.com/questions/251781/how-to-find-the-kth-largest-element-in-an-unsorted-array-of-length-n-in-on +https://stackoverflow.com/questions/251781 """ -def partition(arr,low,high): +def partition(elements: list[int],low: int,high: int) -> int: """ - Partitions list based on the pivot element + Partitions list based on the pivot element. + + This function rearranges the elements in the input list 'arr' such that + all elements greater than or equal to the chosen pivot are on the right side + of the pivot, and all elements smaller than the pivot are on the left side. + Args: arr: The list to be partitioned low: The lower index of the list @@ -19,16 +24,20 @@ def partition(arr,low,high): 4 >>> partition([7,1,4,5,9,2,6,5,8],0,8) 1 + >>> partition(['apple', 'cherry', 'date','banana'], 0, 3) + 2 + >>> partition([3.1, 1.2, 5.6, 4.7], 0, 3) + 1 """ - pivot=arr[high] + pivot=elements[high] i=low-1 for j in range(low,high): - if arr[j]>= pivot: + if elements[j]>= pivot: i+=1 - arr[i], arr[j]=arr[j],arr[i] - arr[i+1],arr[high]=arr[high],arr[i+1] + elements[i], elements[j]=elements[j],elements[i] + elements[i+1],elements[high]=elements[high],elements[i+1] return i+1 -def kth_largest_element(arr,k): +def kth_largest_element(elements: list[int], k: int) -> int: """ Finds the kth largest element in a list. @@ -47,22 +56,28 @@ def kth_largest_element(arr,k): >>> kth_largest_element([9, 1, 3, 6, 7, 9, 8, 4, 2, 4, 9], 11) 1 >>> kth_largest_element([1, 2, 4, 3, 5, 9, 7, 6, 5, 9, 3], 0) - 'Invalid value of k' + -1 + >>> kth_largest_element(['apple', 'cherry', 'date','banana'], 2) + 'cherry' + >>> kth_largest_element([3.1, 1.2, 5.6, 4.7,7.9,5,0], 2) + 5.6 + >>> kth_largest_element([3.1, 1.2, 5.6, 4.7,7.9,5,0], 1.5) + -1 """ - if not 1 <= k <= len(arr): - return "Invalid value of k" - low,high=0,len(arr)-1 + if not 1 <= k <= len(elements): + return -1 + low,high=0,len(elements)-1 while low<=high: - if low>len(arr)-1 or high<0: - return "Invalid value of k" - pivot_index=partition(arr,low,high) + if low>len(elements)-1 or high<0: + return -1 + pivot_index=partition(elements,low,high) if pivot_index==k-1: - return arr[pivot_index] + return elements[pivot_index] elif pivot_index>k-1: high=pivot_index-1 else: low=pivot_index+1 - return "Kth largest element not found" + return -1 if __name__=="__main__": import doctest doctest.testmod() \ No newline at end of file From 2ce0e9cca9551d6984b2c3125832aae091ec010c Mon Sep 17 00:00:00 2001 From: neha3423 Date: Mon, 23 Oct 2023 19:34:34 +0530 Subject: [PATCH 06/17] neha3423 --- data_structures/arrays/kth_largest_element.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/data_structures/arrays/kth_largest_element.py b/data_structures/arrays/kth_largest_element.py index 48b9159317ce..7d9e7e30adae 100644 --- a/data_structures/arrays/kth_largest_element.py +++ b/data_structures/arrays/kth_largest_element.py @@ -51,8 +51,12 @@ def kth_largest_element(elements: list[int], k: int) -> int: Examples: >>> kth_largest_element([3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5], 3) 5 + >>> kth_largest_element([3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5], 2.5) + -1 >>> kth_largest_element([2, 5, 6, 1, 9, 3, 8, 4, 7, 3, 5], 1) 9 + >>> kth_largest_element([2, 5, 6, 1, 9, 3, 8, 4, 7, 3, 5], -2) + -1 >>> kth_largest_element([9, 1, 3, 6, 7, 9, 8, 4, 2, 4, 9], 11) 1 >>> kth_largest_element([1, 2, 4, 3, 5, 9, 7, 6, 5, 9, 3], 0) From 0b19aae7dde604e5c438120b69fa66b01ba7f2c2 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 23 Oct 2023 14:09:39 +0000 Subject: [PATCH 07/17] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/arrays/kth_largest_element.py | 43 +++++++++++-------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/data_structures/arrays/kth_largest_element.py b/data_structures/arrays/kth_largest_element.py index ffdb01c114c4..83143fc64174 100644 --- a/data_structures/arrays/kth_largest_element.py +++ b/data_structures/arrays/kth_largest_element.py @@ -4,7 +4,8 @@ https://stackoverflow.com/questions/251781 """ -def partition(arr,low,high): + +def partition(arr, low, high): """ Partitions list based on the pivot element Args: @@ -24,15 +25,17 @@ def partition(arr,low,high): >>> partition([3.1, 1.2, 5.6, 4.7], 0, 3) 1 """ - pivot=arr[high] - i=low-1 - for j in range(low,high): - if arr[j]>= pivot: - i+=1 - arr[i], arr[j]=arr[j],arr[i] - arr[i+1],arr[high]=arr[high],arr[i+1] - return i+1 -def kth_largest_element(arr,k): + pivot = arr[high] + i = low - 1 + for j in range(low, high): + if arr[j] >= pivot: + i += 1 + arr[i], arr[j] = arr[j], arr[i] + arr[i + 1], arr[high] = arr[high], arr[i + 1] + return i + 1 + + +def kth_largest_element(arr, k): """ Finds the kth largest element in a list. @@ -65,19 +68,21 @@ def kth_largest_element(arr,k): """ if not 1 <= k <= len(arr): return "Invalid value of k" - low,high=0,len(arr)-1 - while low<=high: - if low>len(arr)-1 or high<0: + low, high = 0, len(arr) - 1 + while low <= high: + if low > len(arr) - 1 or high < 0: return "Invalid value of k" - pivot_index=partition(arr,low,high) - if pivot_index==k-1: + pivot_index = partition(arr, low, high) + if pivot_index == k - 1: return arr[pivot_index] - elif pivot_index>k-1: - high=pivot_index-1 + elif pivot_index > k - 1: + high = pivot_index - 1 else: - low=pivot_index+1 + low = pivot_index + 1 return "Kth largest element not found" -if __name__=="__main__": + + +if __name__ == "__main__": import doctest doctest.testmod() From 9e312fa859dbefbb19e5903b8a0ecdca8f4d1f0d Mon Sep 17 00:00:00 2001 From: neha3423 Date: Mon, 23 Oct 2023 19:43:04 +0530 Subject: [PATCH 08/17] neha323 --- data_structures/arrays/kth_largest_element.py | 23 ++++++++++++------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/data_structures/arrays/kth_largest_element.py b/data_structures/arrays/kth_largest_element.py index ffdb01c114c4..8e8adf563dd9 100644 --- a/data_structures/arrays/kth_largest_element.py +++ b/data_structures/arrays/kth_largest_element.py @@ -1,16 +1,22 @@ """ -Given an array of integers and an integer k, find the kth largest element in the array +Given an array of integers and an integer k, find the kth largest element in the array. https://stackoverflow.com/questions/251781 """ -def partition(arr,low,high): +def partition(arr: list[int],low: int,high: int) -> int: """ - Partitions list based on the pivot element + Partitions list based on the pivot element. + + This function rearranges the elements in the input list 'elements' such that + all elements greater than or equal to the chosen pivot are on the right side + of the pivot, and all elements smaller than the pivot are on the left side. + Args: arr: The list to be partitioned low: The lower index of the list high: The higher index of the list + Returns: int: The index of pivot element after partitioning @@ -32,7 +38,9 @@ def partition(arr,low,high): arr[i], arr[j]=arr[j],arr[i] arr[i+1],arr[high]=arr[high],arr[i+1] return i+1 -def kth_largest_element(arr,k): + + +def kth_largest_element(arr: list[int],k: int) -> int: """ Finds the kth largest element in a list. @@ -64,11 +72,11 @@ def kth_largest_element(arr,k): -1 """ if not 1 <= k <= len(arr): - return "Invalid value of k" + return -1 low,high=0,len(arr)-1 while low<=high: if low>len(arr)-1 or high<0: - return "Invalid value of k" + return -1 pivot_index=partition(arr,low,high) if pivot_index==k-1: return arr[pivot_index] @@ -76,8 +84,7 @@ def kth_largest_element(arr,k): high=pivot_index-1 else: low=pivot_index+1 - return "Kth largest element not found" + return -1 if __name__=="__main__": import doctest - doctest.testmod() From 0cb88d454e08d4f75671b4d9d099c27a70d5984f Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 23 Oct 2023 14:16:44 +0000 Subject: [PATCH 09/17] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/arrays/kth_largest_element.py | 38 ++++++++++--------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/data_structures/arrays/kth_largest_element.py b/data_structures/arrays/kth_largest_element.py index 7b13722a7828..a4825ea7b53f 100644 --- a/data_structures/arrays/kth_largest_element.py +++ b/data_structures/arrays/kth_largest_element.py @@ -4,7 +4,8 @@ https://stackoverflow.com/questions/251781 """ -def partition(arr: list[int],low: int,high: int) -> int: + +def partition(arr: list[int], low: int, high: int) -> int: """ Partitions list based on the pivot element. @@ -30,17 +31,17 @@ def partition(arr: list[int],low: int,high: int) -> int: >>> partition([3.1, 1.2, 5.6, 4.7], 0, 3) 1 """ - pivot=arr[high] - i=low-1 - for j in range(low,high): - if arr[j]>= pivot: - i+=1 - arr[i], arr[j]=arr[j],arr[i] - arr[i+1],arr[high]=arr[high],arr[i+1] - return i+1 + pivot = arr[high] + i = low - 1 + for j in range(low, high): + if arr[j] >= pivot: + i += 1 + arr[i], arr[j] = arr[j], arr[i] + arr[i + 1], arr[high] = arr[high], arr[i + 1] + return i + 1 -def kth_largest_element(arr: list[int],k: int) -> int: +def kth_largest_element(arr: list[int], k: int) -> int: """ Finds the kth largest element in a list. @@ -73,18 +74,21 @@ def kth_largest_element(arr: list[int],k: int) -> int: """ if not 1 <= k <= len(arr): return -1 - low,high=0,len(arr)-1 - while low<=high: - if low>len(arr)-1 or high<0: + low, high = 0, len(arr) - 1 + while low <= high: + if low > len(arr) - 1 or high < 0: return -1 - pivot_index=partition(arr,low,high) - if pivot_index==k-1: + pivot_index = partition(arr, low, high) + if pivot_index == k - 1: return arr[pivot_index] elif pivot_index > k - 1: high = pivot_index - 1 else: - low=pivot_index+1 + low = pivot_index + 1 return -1 -if __name__=="__main__": + + +if __name__ == "__main__": import doctest + doctest.testmod() From b76a792659aa771dfa78191adca295953e670f83 Mon Sep 17 00:00:00 2001 From: neha3423 Date: Mon, 23 Oct 2023 20:16:46 +0530 Subject: [PATCH 10/17] neha3423 --- data_structures/arrays/kth_largest_element.py | 24 ++++++++++++++----- 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/data_structures/arrays/kth_largest_element.py b/data_structures/arrays/kth_largest_element.py index 7b13722a7828..6e02fd73c39c 100644 --- a/data_structures/arrays/kth_largest_element.py +++ b/data_structures/arrays/kth_largest_element.py @@ -54,25 +54,37 @@ def kth_largest_element(arr: list[int],k: int) -> int: Examples: >>> kth_largest_element([3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5], 3) 5 - >>> kth_largest_element([3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5], 2.5) - -1 >>> kth_largest_element([2, 5, 6, 1, 9, 3, 8, 4, 7, 3, 5], 1) 9 >>> kth_largest_element([2, 5, 6, 1, 9, 3, 8, 4, 7, 3, 5], -2) - -1 + "Traceback (most recent call last): + ... + ValueError: Invalid value of 'k'" >>> kth_largest_element([9, 1, 3, 6, 7, 9, 8, 4, 2, 4, 9], 11) 1 >>> kth_largest_element([1, 2, 4, 3, 5, 9, 7, 6, 5, 9, 3], 0) - -1 + "Traceback (most recent call last): + ... + ValueError: Invalid value of 'k'" >>> kth_largest_element(['apple', 'cherry', 'date','banana'], 2) 'cherry' >>> kth_largest_element([3.1, 1.2, 5.6, 4.7,7.9,5,0], 2) 5.6 - >>> kth_largest_element([3.1, 1.2, 5.6, 4.7,7.9,5,0], 1.5) + >>> kth_largest_element([-2,-5,-4,-1],1) -1 + >>> kth_largest_element([], 1) + -1 + >>> kth_largest_element([3.1, 1.2, 5.6, 4.7,7.9,5,0], 1.5) + "Traceback (most recent call last): + ... + ValueError: k should be an integer" """ - if not 1 <= k <= len(arr): + if not arr: return -1 + if not isinstance(k,int): + raise ValueError("'k' should be an integer") + if not 1 <= k <= len(arr): + raise ValueError("Invalid value of 'k'") low,high=0,len(arr)-1 while low<=high: if low>len(arr)-1 or high<0: From 3bdaa1fb9259156cc06f0db6a2bd5a55615d2826 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 23 Oct 2023 14:50:33 +0000 Subject: [PATCH 11/17] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/arrays/kth_largest_element.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/data_structures/arrays/kth_largest_element.py b/data_structures/arrays/kth_largest_element.py index 696e6c60a86a..e00dc6d96506 100644 --- a/data_structures/arrays/kth_largest_element.py +++ b/data_structures/arrays/kth_largest_element.py @@ -82,13 +82,13 @@ def kth_largest_element(arr: list[int], k: int) -> int: """ if not arr: return -1 - if not isinstance(k,int): + if not isinstance(k, int): raise ValueError("'k' should be an integer") if not 1 <= k <= len(arr): raise ValueError("Invalid value of 'k'") - low,high=0,len(arr)-1 - while low<=high: - if low>len(arr)-1 or high<0: + low, high = 0, len(arr) - 1 + while low <= high: + if low > len(arr) - 1 or high < 0: return -1 pivot_index = partition(arr, low, high) if pivot_index == k - 1: From bb6a1a89c367db0e12ae69817be35427d88b5ea8 Mon Sep 17 00:00:00 2001 From: neha3423 Date: Mon, 23 Oct 2023 20:21:45 +0530 Subject: [PATCH 12/17] neha3423 --- data_structures/arrays/kth_largest_element.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/data_structures/arrays/kth_largest_element.py b/data_structures/arrays/kth_largest_element.py index 696e6c60a86a..bb4c4e0eae5e 100644 --- a/data_structures/arrays/kth_largest_element.py +++ b/data_structures/arrays/kth_largest_element.py @@ -61,8 +61,10 @@ def kth_largest_element(arr: list[int], k: int) -> int: "Traceback (most recent call last): ... ValueError: Invalid value of 'k'" - >>> kth_largest_element([9, 1, 3, 6, 7, 9, 8, 4, 2, 4, 9], 11) - 1 + >>> kth_largest_element([9, 1, 3, 6, 7, 9, 8, 4, 2, 4, 9], 110) + "Traceback (most recent call last): + ... + ValueError: Invalid value of 'k'" >>> kth_largest_element([1, 2, 4, 3, 5, 9, 7, 6, 5, 9, 3], 0) "Traceback (most recent call last): ... From fb5c506e10af815eb99c345ab9be5527061eb55e Mon Sep 17 00:00:00 2001 From: neha3423 Date: Mon, 23 Oct 2023 20:23:52 +0530 Subject: [PATCH 13/17] neha3423 --- data_structures/arrays/kth_largest_element.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/data_structures/arrays/kth_largest_element.py b/data_structures/arrays/kth_largest_element.py index 5d9ade8161ca..4741e1d75c3c 100644 --- a/data_structures/arrays/kth_largest_element.py +++ b/data_structures/arrays/kth_largest_element.py @@ -41,7 +41,7 @@ def partition(arr: list[int], low: int, high: int) -> int: return i + 1 -def kth_largest_element(arr: list[int], k: int) -> int: +def kth_largest_element(arr: list[int], position: int) -> int: """ Finds the kth largest element in a list. @@ -84,18 +84,18 @@ def kth_largest_element(arr: list[int], k: int) -> int: """ if not arr: return -1 - if not isinstance(k, int): - raise ValueError("'k' should be an integer") - if not 1 <= k <= len(arr): - raise ValueError("Invalid value of 'k'") + if not isinstance(position, int): + raise ValueError("The position should be an integer") + if not 1 <= position <= len(arr): + raise ValueError("Invalid value of 'position'") low, high = 0, len(arr) - 1 while low <= high: if low > len(arr) - 1 or high < 0: return -1 pivot_index = partition(arr, low, high) - if pivot_index == k - 1: + if pivot_index == position - 1: return arr[pivot_index] - elif pivot_index > k - 1: + elif pivot_index > position - 1: high = pivot_index - 1 else: low = pivot_index + 1 From 2adea82422b6e84cfa83886b21383afb9a05860f Mon Sep 17 00:00:00 2001 From: neha3423 Date: Mon, 23 Oct 2023 20:45:46 +0530 Subject: [PATCH 14/17] neha3423 --- data_structures/arrays/kth_largest_element.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/data_structures/arrays/kth_largest_element.py b/data_structures/arrays/kth_largest_element.py index 4741e1d75c3c..25c214551af8 100644 --- a/data_structures/arrays/kth_largest_element.py +++ b/data_structures/arrays/kth_largest_element.py @@ -58,17 +58,18 @@ def kth_largest_element(arr: list[int], position: int) -> int: >>> kth_largest_element([2, 5, 6, 1, 9, 3, 8, 4, 7, 3, 5], 1) 9 >>> kth_largest_element([2, 5, 6, 1, 9, 3, 8, 4, 7, 3, 5], -2) - "Traceback (most recent call last): + "Exception raised: + Traceback (most recent call last): ... - ValueError: Invalid value of 'k'" + ValueError: Invalid value of 'position'" >>> kth_largest_element([9, 1, 3, 6, 7, 9, 8, 4, 2, 4, 9], 110) "Traceback (most recent call last): ... - ValueError: Invalid value of 'k'" + ValueError: Invalid value of 'position'" >>> kth_largest_element([1, 2, 4, 3, 5, 9, 7, 6, 5, 9, 3], 0) "Traceback (most recent call last): ... - ValueError: Invalid value of 'k'" + ValueError: Invalid value of 'position'" >>> kth_largest_element(['apple', 'cherry', 'date','banana'], 2) 'cherry' >>> kth_largest_element([3.1, 1.2, 5.6, 4.7,7.9,5,0], 2) @@ -80,7 +81,7 @@ def kth_largest_element(arr: list[int], position: int) -> int: >>> kth_largest_element([3.1, 1.2, 5.6, 4.7,7.9,5,0], 1.5) "Traceback (most recent call last): ... - ValueError: k should be an integer" + ValueError: The position should be an integer" """ if not arr: return -1 From f750a7ec2b415125bcbcae69172b113e04ffbd00 Mon Sep 17 00:00:00 2001 From: neha3423 Date: Mon, 23 Oct 2023 20:50:38 +0530 Subject: [PATCH 15/17] neha3423 --- data_structures/arrays/kth_largest_element.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/data_structures/arrays/kth_largest_element.py b/data_structures/arrays/kth_largest_element.py index 25c214551af8..2751a3f3997d 100644 --- a/data_structures/arrays/kth_largest_element.py +++ b/data_structures/arrays/kth_largest_element.py @@ -58,18 +58,17 @@ def kth_largest_element(arr: list[int], position: int) -> int: >>> kth_largest_element([2, 5, 6, 1, 9, 3, 8, 4, 7, 3, 5], 1) 9 >>> kth_largest_element([2, 5, 6, 1, 9, 3, 8, 4, 7, 3, 5], -2) - "Exception raised: Traceback (most recent call last): ... - ValueError: Invalid value of 'position'" + ValueError: Invalid value of 'position' >>> kth_largest_element([9, 1, 3, 6, 7, 9, 8, 4, 2, 4, 9], 110) - "Traceback (most recent call last): + Traceback (most recent call last): ... - ValueError: Invalid value of 'position'" + ValueError: Invalid value of 'position' >>> kth_largest_element([1, 2, 4, 3, 5, 9, 7, 6, 5, 9, 3], 0) - "Traceback (most recent call last): + Traceback (most recent call last): ... - ValueError: Invalid value of 'position'" + ValueError: Invalid value of 'position' >>> kth_largest_element(['apple', 'cherry', 'date','banana'], 2) 'cherry' >>> kth_largest_element([3.1, 1.2, 5.6, 4.7,7.9,5,0], 2) @@ -79,9 +78,9 @@ def kth_largest_element(arr: list[int], position: int) -> int: >>> kth_largest_element([], 1) -1 >>> kth_largest_element([3.1, 1.2, 5.6, 4.7,7.9,5,0], 1.5) - "Traceback (most recent call last): + Traceback (most recent call last): ... - ValueError: The position should be an integer" + ValueError: The position should be an integer """ if not arr: return -1 From 49fdc869f400674da8dcb021ca31db0ae6d92e94 Mon Sep 17 00:00:00 2001 From: neha3423 Date: Mon, 23 Oct 2023 21:10:22 +0530 Subject: [PATCH 16/17] Added test case for tuple --- data_structures/arrays/kth_largest_element.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/data_structures/arrays/kth_largest_element.py b/data_structures/arrays/kth_largest_element.py index 2751a3f3997d..96f26e4edd98 100644 --- a/data_structures/arrays/kth_largest_element.py +++ b/data_structures/arrays/kth_largest_element.py @@ -81,6 +81,10 @@ def kth_largest_element(arr: list[int], position: int) -> int: Traceback (most recent call last): ... ValueError: The position should be an integer + >>> kth_largest_element((4,6,1,2),4) + Traceback (most recent call last): + ... + TypeError: 'tuple' object does not support item assignment """ if not arr: return -1 From 7d35911ba276357fef71b2de894c9e169d5fed62 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Thu, 26 Oct 2023 10:48:39 +0200 Subject: [PATCH 17/17] Update kth_largest_element.py --- data_structures/arrays/kth_largest_element.py | 23 +++++++++++-------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/data_structures/arrays/kth_largest_element.py b/data_structures/arrays/kth_largest_element.py index 96f26e4edd98..f25cc68e9b72 100644 --- a/data_structures/arrays/kth_largest_element.py +++ b/data_structures/arrays/kth_largest_element.py @@ -22,11 +22,11 @@ def partition(arr: list[int], low: int, high: int) -> int: int: The index of pivot element after partitioning Examples: - >>> partition([3,1,4,5,9,2,6,5,3,5],0,9) + >>> partition([3, 1, 4, 5, 9, 2, 6, 5, 3, 5], 0, 9) 4 - >>> partition([7,1,4,5,9,2,6,5,8],0,8) + >>> partition([7, 1, 4, 5, 9, 2, 6, 5, 8], 0, 8) 1 - >>> partition(['apple', 'cherry', 'date','banana'], 0, 3) + >>> partition(['apple', 'cherry', 'date', 'banana'], 0, 3) 2 >>> partition([3.1, 1.2, 5.6, 4.7], 0, 3) 1 @@ -44,10 +44,15 @@ def partition(arr: list[int], low: int, high: int) -> int: def kth_largest_element(arr: list[int], position: int) -> int: """ Finds the kth largest element in a list. + Should deliver similar results to: + ```python + def kth_largest_element(arr, position): + return sorted(arr)[-position] + ``` Args: - nums : The list of numbers. - k : The position of the desired kth largest element. + nums: The list of numbers. + k: The position of the desired kth largest element. Returns: int: The kth largest element. @@ -69,19 +74,19 @@ def kth_largest_element(arr: list[int], position: int) -> int: Traceback (most recent call last): ... ValueError: Invalid value of 'position' - >>> kth_largest_element(['apple', 'cherry', 'date','banana'], 2) + >>> kth_largest_element(['apple', 'cherry', 'date', 'banana'], 2) 'cherry' >>> kth_largest_element([3.1, 1.2, 5.6, 4.7,7.9,5,0], 2) 5.6 - >>> kth_largest_element([-2,-5,-4,-1],1) + >>> kth_largest_element([-2, -5, -4, -1], 1) -1 >>> kth_largest_element([], 1) -1 - >>> kth_largest_element([3.1, 1.2, 5.6, 4.7,7.9,5,0], 1.5) + >>> kth_largest_element([3.1, 1.2, 5.6, 4.7, 7.9, 5, 0], 1.5) Traceback (most recent call last): ... ValueError: The position should be an integer - >>> kth_largest_element((4,6,1,2),4) + >>> kth_largest_element((4, 6, 1, 2), 4) Traceback (most recent call last): ... TypeError: 'tuple' object does not support item assignment