def test_accepts_duplicates(self): """ Test if object accepts duplicate items """ sl = SortedList() sl.add(0) sl.add(0) self.assertEqual(len(sl), 2)
def test_correct_order(self): """ Test if object puts items in correct order """ items = [5, 3, 10, 1, 15, 20, 30] sl = SortedList() for item in items: sl.add(item) self.assertEqual(sorted(items), sl.sequence)
def test1(): """Start from empty.""" from SortedList import SortedList a = SortedList() for val in range(10): a.add(val) assert a.getCountLT(5) == 5 assert a.getCountGT(5) == 4 assert a.removeLT(5) == 5 assert a.getCountLT(5) == 0
def test_getitem(self): """ Test if __getitem__ is correctly implemented """ sl = SortedList() items = [(0, 1), (0, 2), (0, 3)] for item in items: sl.add(item) # Test if indices work correctly aswell as reversed for i, ri in zip(range(len(sl)), reversed(range(len(sl)))): self.assertEqual(items[i], sl[i]) self.assertEqual(items[ri], sl[ri]) self.assertRaises(IndexError, sl.__getitem__, 10)
def test_pop(self): """ Test if the object pops correctly """ sl = SortedList() with self.assertRaises(IndexError): sl.pop() sl.pop(0) sl.add(5) self.assertEqual(sl.pop(), 5) sl.extend([6, 90, 3, 4, 9]) self.assertEqual(sl.pop(), 90) self.assertEqual(sl.pop(0), 3) self.assertEqual(sl.sequence, [4, 6, 9])
def test_specified_key(self): """ Test if object inserts values in correct order if the key is specified """ key_1 = lambda a: a[0] key_2 = lambda a: a[1] sl_key1 = SortedList(key_1) sl_key2 = SortedList(key_2) items = [(0, 1), (-5, 29), (1, 15), (-3, 29)] for item in items: sl_key1.add(item) sl_key2.add(item) self.assertEqual(sorted(items, key=key_1), sl_key1.sequence) self.assertEqual(sorted(items, key=key_2), sl_key2.sequence)
class SymbolTable: def __init__(self): self.__sortedListIdentifiers = SortedList() self.__sortedListConstants = SortedList() def addIdentifier(self, value): return self.__sortedListIdentifiers.add(value) def addConstants(self, value): return self.__sortedListConstants.add(value) def getI(self, value): return self.__sortedListIdentifiers.getId(value) def getC(self, value): return self.__sortedListConstants.getId(value) def __str__(self): return str( str(self.__sortedListIdentifiers) + "\n" + str(self.__sortedListConstants))
def test_correct_len(self): """ Test if object has correct length """ sl = SortedList() sl.add(10) self.assertEqual(len(sl), 1) sl.add(0) sl.add(15) self.assertEqual(len(sl), 3)
class SortedDict(dict): def __init__(self, dictionary = None, key = None, **kwargs): dictionary = dictionary or {} super().__init__(dictionary) if kwargs: super.update(kwargs) self.__keys = SortedList(super().keys(), key) ''' updata the dictionary ''' def updata(self, dictionary = None, **kwargs): if dictionary is None: pass elif isinstance(dictionary, dict): super().update(dictionary) else: for key, value in dictionary.items(): super().__setitem__(key, value) if kwargs: super().update(kwargs) self.__keys = SortedList(super().keys(), self.__keys.key) @classmethod def fromkeys(cls, iterable, value = None, key = None): return cls({k : value for k in iterable}, key) ''' set dict[key] = value ''' def __setitem__(self, key, value): if key not in self: self.__keys.add(key) return super().__setitem__(key, value) ''' delete the dict[key] items ''' def __delitem__(self, key): try: self.__keys.remove(key) except ValueError: raise KeyError(key) return super().__delitem__(key) ''' set the dictionary to default statue ''' def setdefault(self, key, value = None): if key not in self: self.__keys.add(key) return super().setdefault(key, value) ''' delete the lastest item dict[len(keys) - 1] from the dictionary ''' def pop(self, key, *args): if key not in self: if len(args) == 0: raise KeyError(key) return args[0] self.__keys.remove(key) return super().pop(key, args) ''' delete the lastest item from the dictionary ''' def popitem(self): item = super().popitem() self.__keys.remove(item[0]) return item ''' clear the all keys from the dictionary ''' def clear(self): super().clear() self.__keys.clear() ''' get the dict's value list ''' def values(self): for key in self.__keys: yield self[key] ''' get the dict's key list ''' def keys(self): for key in self.__keys: yield self.__key ''' get the dict's item list ''' def items(slef): for key in self.__keys: yield (key, self[key]) ''' get the dict's key list ''' def __iter__(self): return iter(self.__keys) def __repr__(self): return object.__repr__(self) ''' get the dict's string describe ''' def __str__(self): return ("{" + ",".join(["{0!r}:{1!r}".format(k, v) for k, v in self.items()]) + "}") ''' get a copy of the dictionary ''' def copy(self): d = SortedDict() super(SortedDict, d).update(self) d.__keys = self.__keys.copy() return d ''' get the value's index ''' def value_at(self, index): return self[self.__keys[index]] ''' set the value at the index ''' def set_value_at(self, index, value): self[self.__keys[index]] = value
class SortedDict(dict): def __init__(self, dictionary=None, key=None, **kwargs): dictionary = dictionary or {} super().__init__(dictionary) if kwargs: super.update(kwargs) self.__keys = SortedList(super().keys(), key) ''' updata the dictionary ''' def updata(self, dictionary=None, **kwargs): if dictionary is None: pass elif isinstance(dictionary, dict): super().update(dictionary) else: for key, value in dictionary.items(): super().__setitem__(key, value) if kwargs: super().update(kwargs) self.__keys = SortedList(super().keys(), self.__keys.key) @classmethod def fromkeys(cls, iterable, value=None, key=None): return cls({k: value for k in iterable}, key) ''' set dict[key] = value ''' def __setitem__(self, key, value): if key not in self: self.__keys.add(key) return super().__setitem__(key, value) ''' delete the dict[key] items ''' def __delitem__(self, key): try: self.__keys.remove(key) except ValueError: raise KeyError(key) return super().__delitem__(key) ''' set the dictionary to default statue ''' def setdefault(self, key, value=None): if key not in self: self.__keys.add(key) return super().setdefault(key, value) ''' delete the lastest item dict[len(keys) - 1] from the dictionary ''' def pop(self, key, *args): if key not in self: if len(args) == 0: raise KeyError(key) return args[0] self.__keys.remove(key) return super().pop(key, args) ''' delete the lastest item from the dictionary ''' def popitem(self): item = super().popitem() self.__keys.remove(item[0]) return item ''' clear the all keys from the dictionary ''' def clear(self): super().clear() self.__keys.clear() ''' get the dict's value list ''' def values(self): for key in self.__keys: yield self[key] ''' get the dict's key list ''' def keys(self): for key in self.__keys: yield self.__key ''' get the dict's item list ''' def items(slef): for key in self.__keys: yield (key, self[key]) ''' get the dict's key list ''' def __iter__(self): return iter(self.__keys) def __repr__(self): return object.__repr__(self) ''' get the dict's string describe ''' def __str__(self): return ("{" + ",".join(["{0!r}:{1!r}".format(k, v) for k, v in self.items()]) + "}") ''' get a copy of the dictionary ''' def copy(self): d = SortedDict() super(SortedDict, d).update(self) d.__keys = self.__keys.copy() return d ''' get the value's index ''' def value_at(self, index): return self[self.__keys[index]] ''' set the value at the index ''' def set_value_at(self, index, value): self[self.__keys[index]] = value
def test_add(self): sl = SortedList() sl.add(1) self.assertEqual(sl.sequence, [1])