Skip to content

Instantly share code, notes, and snippets.

@Pentusha
Created June 15, 2017 11:58
Show Gist options
  • Save Pentusha/d6fb0138a16db443514b10e0b563ef63 to your computer and use it in GitHub Desktop.
Save Pentusha/d6fb0138a16db443514b10e0b563ef63 to your computer and use it in GitHub Desktop.
cycle groups and sorting
class Cmp:
def __init__(self, value):
self._value = value
def __eq__(self, other: 'Cmp'):
return self._value == other._value
def __ne__(self, other: 'Cmp'):
return self._value != other._value
def __gt__(self, other: 'Cmp'):
return (
self._value == 'A' and other._value == 'B'
or self._value == 'B' and other._value == 'C'
or self._value == 'C' and other._value == 'A'
)
def __ge__(self, other: 'Cmp'):
return self == other or self > other
def __lt__(self, other: 'Cmp'):
return (
self._value == 'A' and other._value == 'C'
or self._value == 'B' and other._value == 'A'
or self._value == 'C' and other._value == 'B'
)
def __le__(self, other):
return self == other or self < other
def __str__(self):
return self._value
def __repr__(self):
return self._value
x1 = [Cmp('A'), Cmp('B'), Cmp('C')]
x2 = [Cmp('A'), Cmp('C'), Cmp('C')]
x3 = [Cmp('B'), Cmp('A'), Cmp('B')]
x1.sort()
x2.sort()
x3.sort()
print(x1) # [C, B, A]
print(x2) # [A, C, C]
print(x3) # [B, B, A]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment