Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
e6c2be5
neha3423
neha3423 Oct 19, 2023
93f2d3e
neha3423
neha3423 Oct 19, 2023
53fb687
neha3423
neha3423 Oct 19, 2023
a50bd08
Merge branch 'TheAlgorithms:master' into master
neha3423 Oct 19, 2023
d78fa11
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 19, 2023
bd6d47c
neha3423
neha3423 Oct 23, 2023
2ce0e9c
neha3423
neha3423 Oct 23, 2023
41312a1
neha3423
neha3423 Oct 23, 2023
b4e58a8
Merge branch 'TheAlgorithms:master' into master
neha3423 Oct 23, 2023
add638b
Merge branch 'master' of https://github.com/neha3423/Python
neha3423 Oct 23, 2023
0b19aae
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 23, 2023
9e312fa
neha323
neha3423 Oct 23, 2023
36490ef
Merge branch 'master' of https://github.com/neha3423/Python
neha3423 Oct 23, 2023
0cb88d4
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 23, 2023
b76a792
neha3423
neha3423 Oct 23, 2023
ebd88b9
Merge branch 'master' of https://github.com/neha3423/Python
neha3423 Oct 23, 2023
3bdaa1f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 23, 2023
bb6a1a8
neha3423
neha3423 Oct 23, 2023
7bb2f6b
Merge branch 'master' of https://github.com/neha3423/Python
neha3423 Oct 23, 2023
fb5c506
neha3423
neha3423 Oct 23, 2023
2adea82
neha3423
neha3423 Oct 23, 2023
f750a7e
neha3423
neha3423 Oct 23, 2023
49fdc86
Added test case for tuple
neha3423 Oct 23, 2023
7d35911
Update kth_largest_element.py
cclauss Oct 26, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Update kth_largest_element.py
  • Loading branch information
cclauss authored Oct 26, 2023
commit 7d35911ba276357fef71b2de894c9e169d5fed62
23 changes: 14 additions & 9 deletions data_structures/arrays/kth_largest_element.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand All @@ -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
Expand Down