Skip to content

Conversation

@shivansh31414
Copy link

Describe your change:

improved the docstrings with complexity notes,

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Add or change doctests? -- Note: Please avoid changing both code and tests in a single pull request.
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms include at least one URL that points to Wikipedia or another similar explanation.
  • If this pull request resolves one or more open issues then the description above includes the issue number(s) with a closing keyword: "Fixes adding better docstring explaining Strassen’s complexity: #14084

@algorithms-keeper algorithms-keeper bot added awaiting reviews This PR is ready to be reviewed tests are failing Do not merge until tests pass labels Jan 8, 2026
@harshadtech22
Copy link

def partition(arr: list, low: int, high: int) -> int:
pivot = arr[high]
i = low - 1
for j in range(low, high):
# Using >= sorts the array in descending order
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, position: int) -> int:
if not arr:
return -1 # Match your specific doctest requirement

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'")

# Create a copy if you don't want to mutate the original list
nums = list(arr) 
low, high = 0, len(nums) - 1

while low <= high:
    pivot_index = partition(nums, low, high)
    
    # Since we partition in descending order:
    # 1st largest (position 1) will be at index 0
    if pivot_index == position - 1:
        return nums[pivot_index]
    elif pivot_index > position - 1:
        high = pivot_index - 1
    else:
        low = pivot_index + 1
        
return -1

you should try this one

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting reviews This PR is ready to be reviewed tests are failing Do not merge until tests pass

Projects

None yet

Development

Successfully merging this pull request may close these issues.

adding better docstring explaining Strassen’s complexity:

3 participants