Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class TKey:
- def __init__(self, x, compare):
- self.value = x
- self.cmp = compare
- def __le__(self, other):
- return self.cmp(self.value, other.value)
- def __lt__(self, other):
- return self <= other and self.value != other.value
- def __gt__(self, other):
- return not(self <= other)
- def __ge__(self, other):
- return not(self < other)
- def main():
- straight_compare = lambda x, y: x <= y
- reverse_compare = lambda x, y: y <= x
- convert_to_key = lambda cmp: lambda x: TKey(x, cmp)
- print(sorted([1, 4, 35, 10, 8, 7, 9, 9, 12], key=convert_to_key(straight_compare))) # output: [1, 4, 7, 8, 9, 9, 10, 12, 35]
- print(sorted([1, 4, 35, 10, 8, 7, 9, 9, 12], key=convert_to_key(reverse_compare))) # output: [35, 12, 10, 9, 9, 8, 7, 4, 1]
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement