We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Hi gussy, as documentation says, Python 3 no longer supports comparer functions for classes, it uses a tool function to map between old comparer (that i think HackerRank is used to put in their excercises) and Python 3 comparers:
"functools.cmp_to_key(func)
Transform an old-style comparison function to a key function. Used with tools that accept key functions (such as sorted(), min(), max(), heapq.nlargest(), heapq.nsmallest(), itertools.groupby()). This function is primarily used as a transition tool for programs being converted to Python 3 where comparison functions are no longer supported.
A comparison function is any callable that accept two arguments, compares them, and returns a negative number for less-than, zero for equality, or a positive number for greater-than. A key function is a callable that accepts one argument and returns another value to be used as the sort key."
So it takes a func and turns that into something that Python 3 understands.
Sorting: Comparator
You are viewing a single comment's thread. Return to all comments →
Hi gussy, as documentation says, Python 3 no longer supports comparer functions for classes, it uses a tool function to map between old comparer (that i think HackerRank is used to put in their excercises) and Python 3 comparers:
"functools.cmp_to_key(func) Transform an old-style comparison function to a key function. Used with tools that accept key functions (such as sorted(), min(), max(), heapq.nlargest(), heapq.nsmallest(), itertools.groupby()). This function is primarily used as a transition tool for programs being converted to Python 3 where comparison functions are no longer supported.
A comparison function is any callable that accept two arguments, compares them, and returns a negative number for less-than, zero for equality, or a positive number for greater-than. A key function is a callable that accepts one argument and returns another value to be used as the sort key."
So it takes a func and turns that into something that Python 3 understands.
Source: link