Skip to content

Instantly share code, notes, and snippets.

@kajuberdut
Last active July 27, 2017 21:02
Show Gist options
  • Save kajuberdut/e313cae5092707114f0b57207044aa06 to your computer and use it in GitHub Desktop.
Save kajuberdut/e313cae5092707114f0b57207044aa06 to your computer and use it in GitHub Desktop.
from functools import total_ordering
i = 0
weights = {}
for alpha in 'abcdefghijklmnopqrstuvwxyz':
i += 1
weights[alpha] = i
@total_ordering
class weighted_word(object):
def __init__(self, word):
self.word = word
self.alpha_weights = self.word_weights(word)
def _is_valid_operand(self, other):
return (hasattr(other, "alpha_weights") and
hasattr(other, "alpha_weights"))
def __eq__(self, other):
if not self._is_valid_operand(other):
return NotImplemented
return self.alpha_weights == other.alpha_weights
def __lt__(self, other): # a word is alphabetically less than another if it would be indexed earlier in a list alphabetically.
if not self._is_valid_operand(other):
return NotImplemented
if self.alpha_weights == other.alpha_weights: # Equality exclusive of less than
return False
i = 0
for alpha_wt in self.alpha_weights:
if alpha_wt < other.alpha_weights[i]: # self is lesser if comparing from left, self has the first lesser letter.
return True
if self.alpha_weights[i] == other.alpha_weights[i]: #Check for letter tie
if i+1 == len(other.alpha_weights): # if end of other first, other is lesser alphasort
return False
i += 1 # tie - check next letter.
return True # All ties and self has run out of letters therefore is less than
def __repr__(self):
return self.word
def word_weights(self, word):
word = word.lower()
return [weights[a] for a in word if a.isalpha()]
def weight_sentence(string):
return [weighted_word(word) for word in string.split()]
def order_wordlist(wordlist):
""" Assumes a list of weighted_word objects"""
if len(wordlist) > 1:
pivot_index = len(wordlist) // 2
smaller_items = []
larger_items = []
for i, val in enumerate(wordlist):
if i != pivot_index:
if val < wordlist[pivot_index]:
smaller_items.append(val)
else:
larger_items.append(val)
print('pivot is:', wordlist[pivot_index])
print('smaller is: ', smaller_items)
print('larger is:', larger_items)
order_wordlist(smaller_items)
order_wordlist(larger_items)
wordlist[:] = smaller_items + [wordlist[pivot_index]] + larger_items
if __name__ == "__main__":
a = weighted_word('alpha')
alph = weighted_word('alph')
aalpha = weighted_word('aalpha')
b = weighted_word('beta')
print(a, a.alpha_weights)
# alpha [1, 12, 16, 8, 1]
print(b, b.alpha_weights)
# beta [2, 5, 20, 1]
print("Alpha Greater than Beta: ", a > b)
# Alpha Greater than Beta: False
print("Alpha lesser than Beta: ", a < b)
# Alpha lesser than Beta: True
print("Alpha equal Beta: ", a == b)
# Alpha equal Beta: False
print("Alpha equal Alpha: ", a == a)
# Alpha equal Alpha: True
print("Alpha less than Alph: ", a < alph)
# Alpha less than Alph: False
print("Alpha greater than Alph: ", a > alph)
# Alpha greater than Alph: True
print("Alpha equal Alph: ", a == alph)
# Alpha equal Alph: False
print("Alpha less than Aalpha: ", a < aalpha)
# Alpha less than Aalpha: True
print("Alpha greater than Aalpha: ", a > aalpha)
# Alpha greater than Aalpha: False
print("Alpha equal Aalpha: ", a == aalpha)
# Alpha equal Aalpha: False
sentence = "The quick brown fox jumps over the lazy dog"
sentence = weight_sentence(sentence)
for word in sentence:
print(word, word.alpha_weights)
# The [20, 8, 5]
# quick [17, 21, 9, 3, 11]
# brown [2, 18, 15, 23, 14]
# fox [6, 15, 24]
# jumps [10, 21, 13, 16, 19]
# over [15, 22, 5, 18]
# the [20, 8, 5]
# lazy [12, 1, 26, 25]
# dog [4, 15, 7]
order_wordlist(sentence)
# pivot is: jumps
# smaller is: [The, quick, brown, fox, over, the, lazy, dog]
# larger is: []
# pivot is: over
# smaller is: [The, quick, brown, fox, the, lazy, dog]
# larger is: []
# pivot is: fox
# smaller is: [The, quick, brown, the, lazy, dog]
# larger is: []
# pivot is: the
# smaller is: [quick, brown, lazy, dog]
# larger is: [The]
# pivot is: lazy
# smaller is: [quick, brown, dog]
# larger is: []
# pivot is: brown
# smaller is: [quick, dog]
# larger is: []
# pivot is: dog
# smaller is: [quick]
# larger is: []
print(sentence)
# [quick, dog, brown, lazy, the, The, fox, over, jumps]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment