-
-
Notifications
You must be signed in to change notification settings - Fork 50.2k
added algo of Max and Min elements #13344
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
02f2a78
added rotate_array.py
jenyyy4 ded9c6d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 7096968
fixed issues
jenyyy4 302988d
merged branch
jenyyy4 bd4e433
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 925cb2d
fixed reverse issue
jenyyy4 85eb704
Merge branch 'anuska' of https://github.com/jenyyy4/Python into anuska
jenyyy4 7043856
added doctests
jenyyy4 889aa02
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 90b332b
changed k to steps for a descriptive name
jenyyy4 8aa6469
Merge branch 'anuska' of https://github.com/jenyyy4/Python into anuska
jenyyy4 ec22c92
Merge branch 'master' into anuska
jenyyy4 5a1cf10
fixed non-pep
jenyyy4 79dc1ad
Merge branch 'anuska' of https://github.com/jenyyy4/Python into anuska
jenyyy4 350cdd3
added max and min element algo
jenyyy4 be84370
fixed to satisfy checklist
jenyyy4 152810d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| """ | ||
| Find the maximum and minimum elements in a list. | ||
| Source: https://en.wikipedia.org/wiki/Maximum_and_minimum | ||
|
|
||
| >>> find_max_min([4, 2, 9, 1, 7]) | ||
| (9, 1) | ||
| >>> find_max_min([-5, -10, 0, 5]) | ||
| (5, -10) | ||
| >>> find_max_min([42]) | ||
| (42, 42) | ||
| >>> find_max_min([]) | ||
| """ | ||
|
|
||
|
|
||
| def find_max_min(arr): | ||
| """ | ||
| Returns the maximum and minimum elements of a list. | ||
|
|
||
| Parameters: | ||
| arr (list): The list of numbers. | ||
|
|
||
| Returns: | ||
| tuple: (maximum, minimum) | ||
|
|
||
| Raises: | ||
| ValueError: If the list is empty. | ||
| """ | ||
|
|
||
| if not arr: | ||
| raise ValueError("find_max_min() arg is an empty list") | ||
|
|
||
| maximum = max(arr) | ||
| minimum = min(arr) | ||
| return maximum, minimum | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| examples = [ | ||
| [4, 2, 9, 1, 7], | ||
| [-5, -10, 0, 5], | ||
| [42], | ||
| ] | ||
|
|
||
| for arr in examples: | ||
| max_val, min_val = find_max_min(arr) | ||
| print(f"For list {arr}, maximum: {max_val}, minimum: {min_val}") | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| def rotate_array(arr: list[int], steps: int) -> list[int]: | ||
| """ | ||
| Rotates a list to the right by steps positions. | ||
|
|
||
| Parameters: | ||
| arr (List[int]): The list of integers to rotate. | ||
| steps (int): Number of positions to rotate. Can be negative for left rotation. | ||
|
|
||
| Returns: | ||
| List[int]: Rotated list. | ||
|
|
||
| Examples: | ||
| >>> rotate_array([1, 2, 3, 4, 5], 2) | ||
| [4, 5, 1, 2, 3] | ||
| >>> rotate_array([1, 2, 3, 4, 5], -2) | ||
| [3, 4, 5, 1, 2] | ||
| >>> rotate_array([1, 2, 3, 4, 5], 7) | ||
| [4, 5, 1, 2, 3] | ||
| >>> rotate_array([], 3) | ||
| [] | ||
| """ | ||
|
|
||
| n = len(arr) | ||
| if n == 0: | ||
| return arr | ||
|
|
||
| steps = steps % n | ||
|
|
||
| if steps < 0: | ||
| steps += n | ||
|
|
||
| def reverse(start: int, end: int) -> None: | ||
| """ | ||
| Reverses a portion of the list in place from index start to end. | ||
|
|
||
| Parameters: | ||
| start (int): Starting index of the portion to reverse. | ||
| end (int): Ending index of the portion to reverse. | ||
|
|
||
| Returns: | ||
| None | ||
|
|
||
| Examples: | ||
| >>> example = [1, 2, 3, 4, 5] | ||
| >>> def reverse_test(arr, start, end): | ||
| ... while start < end: | ||
| ... arr[start], arr[end] = arr[end], arr[start] | ||
| ... start += 1 | ||
| ... end -= 1 | ||
| >>> reverse_test(example, 0, 2) | ||
| >>> example | ||
| [3, 2, 1, 4, 5] | ||
| >>> reverse_test(example, 2, 4) | ||
| >>> example | ||
| [3, 2, 5, 4, 1] | ||
| """ | ||
|
|
||
| while start < end: | ||
| arr[start], arr[end] = arr[end], arr[start] | ||
| start += 1 | ||
| end -= 1 | ||
|
|
||
| reverse(0, n - 1) | ||
| reverse(0, steps - 1) | ||
| reverse(steps, n - 1) | ||
|
|
||
| return arr | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| examples = [ | ||
| ([1, 2, 3, 4, 5], 2), | ||
| ([1, 2, 3, 4, 5], -2), | ||
| ([1, 2, 3, 4, 5], 7), | ||
| ([], 3), | ||
| ] | ||
|
|
||
| for arr, steps in examples: | ||
| rotated = rotate_array(arr.copy(), steps) | ||
| print(f"Rotate {arr} by {steps}: {rotated}") |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please provide return type hint for the function:
find_max_min. If the function does not return a value, please provide the type hint as:def function() -> None:Please provide type hint for the parameter:
arr