-
Notifications
You must be signed in to change notification settings - Fork 126
Counting the number of inversions added. #133
base: master
Are you sure you want to change the base?
Conversation
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 format your code according to PEP8 guidelines.
pydsa/count_inversions.py
Outdated
merged , ans=_merge(L,R) | ||
return merged , left+right+ans | ||
return a , 0 | ||
def count_inversions(a): |
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 use 4 spaces for indentation, and follow PEP8
pydsa/tests/test_count_inversions.py
Outdated
@@ -0,0 +1,4 @@ | |||
from pydsa import count_inversions | |||
def test_count_inversions(): |
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.
Leave 2 lines before defining function
@CuriousLearner please review now. Apologies for the poor formatting initially. |
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 don't rewrite the functions already present in the library.
pydsa/count_inversions.py
Outdated
Counts the number of inversions in an array. | ||
Divide and conquer algorithm is used for same. | ||
>>> from pydsa import count_inversions | ||
>>> a = [1,20,6,4,5] |
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.
space after comma
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.
@aktech the function there returns a sorted array while here it's supposed to return a count.
pydsa/count_inversions.py
Outdated
# Complexity : O(nlog(n)) | ||
|
||
|
||
def _merge(L, R): |
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 function is already present here : https://github.com/pydsa/pydsa/blob/master/pydsa/merge_sort.py#L5
return m, cnt | ||
|
||
|
||
def merge_sort(a): |
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 one is also present here: https://github.com/pydsa/pydsa/blob/master/pydsa/merge_sort.py#L21
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.
@aktech The function there returns the sorted array and not the count of inversions.
@aktech @CuriousLearner Could you please review? |
I added the counting inversions using merge sort algorithm. Please review.