codezonediitj / pydatastructs

A python package for data structures and algorithms
https://pydatastructs.readthedocs.io/en/stable/
Other
202 stars 270 forks source link

Added a function to check ordering in ODA and DODA #344

Closed Nora2412 closed 3 years ago

Nora2412 commented 3 years ago

Appended algorithms file with is_sorted function. This will be useful to check whether the array given is sorted or not.

References to other Issues or PRs or Relevant literature

Brief description of what is fixed or changed

Other comments

codecov[bot] commented 3 years ago

Codecov Report

Merging #344 (8730d80) into master (a21d00e) will increase coverage by 0.010%. The diff coverage is 100.000%.

@@              Coverage Diff              @@
##            master      #344       +/-   ##
=============================================
+ Coverage   98.550%   98.561%   +0.010%     
=============================================
  Files           25        25               
  Lines         3243      3267       +24     
=============================================
+ Hits          3196      3220       +24     
  Misses          47        47               
Impacted Files Coverage Δ
pydatastructs/linear_data_structures/__init__.py 100.000% <ø> (ø)
pydatastructs/linear_data_structures/algorithms.py 99.613% <100.000%> (+0.015%) :arrow_up:
...ucts/miscellaneous_data_structures/disjoint_set.py 100.000% <0.000%> (ø)

Impacted file tree graph

czgdp1807 commented 3 years ago

This is a bit trivial. I mean checking if an array sorted or not in O(N) time isn't really helpful. For keeping time complexity constant we may need to maintain a state inside ODA class to track when the sort function was called on an array object. More or less it would be just like maintaining the history of the array. We can have this feature as something extra.

Smit-create commented 3 years ago

For keeping time complexity constant we may need to maintain a state inside ODA class to track when the sort function was called on an array object

What if the user has'nt call sort function and has already given an sorted array as input?

This is a bit trivial. I mean checking if an array sorted or not in O(N) time isn't really helpful.

Presently, C++, also uses linear time complexity to check sortedness: http://www.cplusplus.com/reference/algorithm/is_sorted/

An operator is defined in _comp (or something similar) in misc_util.py. Please use that here.

This is something that would be good to have instead of using operators directly

Smit-create commented 3 years ago

Taking references from https://en.cppreference.com/w/cpp/algorithm/lower_bound and https://en.cppreference.com/w/cpp/algorithm/is_sorted_until: We could do something like this:

def is_sorted(array, start, end, cmp=None):
    # cmp is a function that takes (a, b) and returns true if a is strictly less than b (a<b)
    if cmp is None:
         cmp = lambda a, b: (a < b) # we will use python defined <
    # algo
    return True/False

Similar thing can be done on #351 What do you think @czgdp1807 ?

czgdp1807 commented 3 years ago

What do you think @czgdp1807 ?

We should add is_ordered as it is more generic and allows more flexibility.

Smit-create commented 3 years ago

@Nora2412 Please use python -m pytest to check locally which test is failing (presently due to trailing space/tab on some line)

Smit-create commented 3 years ago

If you have trailing whitespace it will show errors. This one will fix unwanted spaces: $ ./bin/strip_whitespace <file>

Nora2412 commented 3 years ago

@Smit-create can you please review and let me know if any further changes are required.

czgdp1807 commented 3 years ago

Will merge once the tests pass.