Skip to content

Instantly share code, notes, and snippets.

@elyssonmr
Last active December 23, 2015 20:19
Show Gist options
  • Save elyssonmr/6689242 to your computer and use it in GitHub Desktop.
Save elyssonmr/6689242 to your computer and use it in GitHub Desktop.
Comparador de "Googlon" do Curso Python para Zumbis. OBS: Código para Python3
class GooglonComparador:
def __init__(self, obj, *args):
self.obj = obj
self.ordem_dict = {}
for i, letra in enumerate('zmbtshjpnwlrcxkqvdgf'):
self.ordem_dict[letra] = i
# Menor que = Less than
def __lt__(self, other):
return self._comparar(self.obj, other.obj) < 0
# Maior que = Greater than
def __gt__(self, other):
return self._comparar(self.obj, other.obj) > 0
# Igual = Equals to
def __eq__(self, other):
return self._comparar(self.obj, other.obj) == 0
# Menor ou igual a = Less or equal to
def __le__(self, other):
return self._comparar(self.obj, other.obj) <= 0
# Maior ou igual a = Greater or equal to
def __ge__(self, other):
return self._comparar(self.obj, other.obj) >= 0
# Diferente de = Not equal to
def __ne__(self, other):
return self._comparar(self.obj, other.obj) != 0
def _comparar(self, word1, word2):
if word1 == word2:
return 0
if len(word1) < len(word2):
for i in range(len(word1)):
if self.ordem_dict[word1[i]] < self.ordem_dict[word2[i]]:
return -1
if self.ordem_dict[word1[i]] > self.ordem_dict[word2[i]]:
return 1
return -1
else:
for i in range(len(word2)):
if self.ordem_dict[word1[i]] < self.ordem_dict[word2[i]]:
return -1
if self.ordem_dict[word1[i]] > self.ordem_dict[word2[i]]:
return 1
return 1
list_sort = sorted(txtB, key=GooglonComparador)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment