-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
Added strand sort #1981
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
Added strand sort #1981
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
201ca25
Added Burkes dithering algorithm
l3str4nge fa79687
Added unit tests for burkes algorithm
l3str4nge a157b47
Fix burkes algorithm
l3str4nge b92ccd3
Added some additional information
l3str4nge 0f2bbde
Fixed CI tests
l3str4nge 611e113
Update digital_image_processing/dithering/burkes.py
l3str4nge c872fc6
Update digital_image_processing/dithering/burkes.py
l3str4nge 8757600
Update digital_image_processing/dithering/burkes.py
l3str4nge 6b8e069
Propogate the += and add a doctest
cclauss f33f4ca
Fix doctest
l3str4nge 4cd6aa7
@staticmethod --> @ classmethod to ease testing
cclauss bea2a4d
def test_burkes(file_path):
cclauss 114ee8d
Fix for mypy checks
l3str4nge 66190e4
Fix variable order in get_greyscale
l3str4nge 823a98f
Fix get_greyscale method
l3str4nge b827c55
Fix get_greyscale method
l3str4nge 6ce1e16
3.753
cclauss 9ac0666
Added Nearest Neighbour algorithm
l3str4nge 5e10f48
technique -> techniques
l3str4nge 5bb46d3
Added Nearest neighbour algorithm
l3str4nge dc2e794
Fix conflicts
l3str4nge 940548a
Added strand sort
l3str4nge c0421c3
Fix conflicts after merge with upstream
l3str4nge 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,60 @@ | ||
from functools import partial | ||
import operator | ||
|
||
|
||
def strand_sort(arr: list, solution: list, _operator: callable): | ||
""" | ||
Strand sort implementation | ||
source: https://en.wikipedia.org/wiki/Strand_sort | ||
|
||
:param arr: Unordered input list | ||
:param solution: Ordered output list | ||
:param _operator: Operator lt/gt to specify ascent/descent version | ||
|
||
Examples: | ||
>>> solution_1 = [] | ||
>>> strand_sort([4, 2, 5, 3, 0, 1], solution_1, operator.gt) | ||
>>> solution_1 | ||
[0, 1, 2, 3, 4, 5] | ||
|
||
>>> solution_2 = [] | ||
>>> strand_sort([4, 2, 5, 3, 0, 1], solution_2, operator.lt) | ||
>>> solution_2 | ||
[5, 4, 3, 2, 1, 0] | ||
""" | ||
if not arr: | ||
return | ||
|
||
sublist = [arr.pop(0)] | ||
for i, item in enumerate(arr): | ||
if _operator(item, sublist[-1]): | ||
sublist.append(item) | ||
arr.pop(i) | ||
|
||
# merging sublist into solution list | ||
if not solution: | ||
solution.extend(sublist) | ||
else: | ||
while sublist: | ||
item = sublist.pop(0) | ||
for i, xx in enumerate(solution): | ||
if not _operator(item, xx): | ||
solution.insert(i, item) | ||
break | ||
else: | ||
solution.append(item) | ||
|
||
strand_sort(arr, solution, _operator) | ||
|
||
|
||
strand_asc = partial(strand_sort, _operator=operator.gt) | ||
strand_desc = partial(strand_sort, _operator=operator.lt) | ||
|
||
if __name__ == "__main__": | ||
solution = [] | ||
strand_asc([4, 3, 5, 1, 2], solution) | ||
assert solution == [1, 2, 3, 4, 5] | ||
|
||
solution = [] | ||
strand_desc([4, 3, 5, 1, 2], solution) | ||
assert solution == [5, 4, 3, 2, 1] |
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.
This sort would be easier to use (and test) if it returned a list instead of requiring the caller to allocate and pass in a
solution
list.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.
It would also be cool if
_operator
was an optional parameter with the default set tooperator.gt
.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.
@cclauss Could we move review to reopened PR: #1982? This PR was made by mistake.