-
Notifications
You must be signed in to change notification settings - Fork 1
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.
Good work, but two problems:
Comparable
doesn't work with Python- it's better to centralize all the binarySearch into
ListBackedSet
, no reason to spread it around
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.
Closer!
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.
Excellent! Much improved, I think this is the last change and ArrayMap
will be done!
class BinarySearchUtil: | ||
@staticmethod | ||
def binary_search( | ||
data, item, compare_func: Optional[Callable[[Any, Any], int]] = None | ||
) -> int: |
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.
-
since we're in Python, this doesn't have to be in a class. It could just be
def _binary_search_util(data, item):
-
This function is only ever called with two parameters -
compare_func
is always None. The fix is
def _compare_normal(a, b) -> int:
if a == b:
return 0
else if a < b:
return -1
else
return 1
def _compare_string_slash_first(a: str, b : str) -> int:
return _compare_normal(a.replace("/", "\0"), b.replace("/", "\0"))
def _binary_search(data, item) -> int:
compare_func = _compare_string_slash_first if item is str else _compare_normal
low, high = 0, len(data) - 1
...
the rest of the logic, but now you know that compare_func is never None
No description provided.