コード例 #1
0
    def test_accepts_duplicates(self):
        """ Test if object accepts duplicate items """
        sl = SortedList()
        sl.add(0)
        sl.add(0)

        self.assertEqual(len(sl), 2)
コード例 #2
0
def test2():
    """Construct from list."""
    from SortedList import SortedList
    a = SortedList([b for b in range(10)])
    assert a.getCountLT(5) == 5
    assert a.getCountGT(5) == 4
    assert a.removeLT(5) == 5
    assert a.getCountLT(5) == 0
コード例 #3
0
    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)
コード例 #4
0
ファイル: SortedDict.py プロジェクト: zfh1005/PythonPackage
 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)
コード例 #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)
コード例 #6
0
    def test_remove(self):
        """ Test if object removes items """
        sl = SortedList()
        sl.extend([0, 10, 20, -5, 10])

        sl.remove(0)
        self.assertEqual(sl.sequence, [-5, 10, 10, 20])

        sl.remove(10)
        self.assertEqual(sl.sequence, [-5, 10, 20])
        sl.remove(10)
        self.assertEqual(sl.sequence, [-5, 20])

        self.assertRaises(ValueError, sl.remove, 10)
コード例 #7
0
    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)
コード例 #8
0
    def test_count(self):
        """ Test if returns correct count of an item """
        sl = SortedList()
        sl.extend([0, 4, 3, 4, 3, 4, 10, 4, 10, 3])

        self.assertEqual(sl.count(4), 4)
        self.assertEqual(sl.count(3), 3)
        self.assertEqual(sl.count(0), 1)
        self.assertEqual(sl.count(2000), 0)
コード例 #9
0
ファイル: SortedDict.py プロジェクト: zfh1005/PythonPackage
 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)
コード例 #10
0
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
コード例 #11
0
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))
コード例 #12
0
    def test_copy(self):
        """ Test if returns a copy of it's contents """
        sl = SortedList()

        self.assertEqual(sl.copy(), [])

        sl.extend([10, 5, 20, 3])
        self.assertEqual(sl.copy(), [3, 5, 10, 20])
コード例 #13
0
    def test_clear(self):
        """ Test if object clears it's contents """
        sl = SortedList()
        sl.extend([0, 10, 20, -5, -200])

        sl.clear()
        self.assertEqual(len(sl), 0)
        self.assertEqual(sl.sequence, [])
コード例 #14
0
    def test_extend(self):
        """ Test if object accepts an iterable
            and puts all of it's items in correct
            order """
        sl = SortedList()

        items = [3, 0, 1, 15]
        sl.extend(items)
        self.assertEqual(sorted(items), sl.sequence)

        more_items = [-5, 20, 3]
        sl.extend(more_items)
        self.assertEqual(sorted(items + more_items), sl.sequence)
        sl.extend([])
        self.assertEqual(sorted(items + more_items), sl.sequence)
コード例 #15
0
    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)
コード例 #16
0
    def test_index(self):
        """ Test if returns the index of an item """
        sl = SortedList()
        self.assertRaises(ValueError, sl.index, 0)

        sl.extend([0, 1, 1, 10, 1, 15, 20])
        self.assertEqual(sl.index(0), 0)
        self.assertEqual(sl.index(1), 1)

        self.assertEqual(sl.index(1, 3), 3)
        self.assertRaises(ValueError, sl.index, 10, 0, 3)
コード例 #17
0
ファイル: SortedDict.py プロジェクト: zfh1005/PythonPackage
 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)
コード例 #18
0
ファイル: SortedDict.py プロジェクト: zfh1005/PythonPackage
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
コード例 #19
0
ファイル: SortedDict.py プロジェクト: zfh1005/PythonPackage
 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)
コード例 #20
0
ファイル: SortedDict.py プロジェクト: zfh1005/PythonPackage
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
コード例 #21
0
 def test_empty_at_first(self):
     """ Test if the SortedList is empty at the start """
     self.assertEqual(len(SortedList()), 0)
コード例 #22
0
    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])
コード例 #23
0
    def test_add(self):
        sl = SortedList()
        sl.add(1)

        self.assertEqual(sl.sequence, [1])
コード例 #24
0
ファイル: HashTable.py プロジェクト: 01mu/misc
    def __init__(self, size):
        self.size = size
        self.array = [None] * size

        for i in range(size):
            self.array[i] = SortedList()
コード例 #25
0
ファイル: lab3_linked_lists.py プロジェクト: alomon-16/CS2303
        Generate and return a native list of random integers
        of size n. The randomly generated integers range from 1
        to 2n (e.g. if n is 100, each integer will range from
        1 to 200). Duplicates are not allowed, and the list will only
        be returned once it is full.
        """
        L = []
        while len(L) < self.n:
            num = random.randint(self.low, self.high * 2)
            if num not in L:
                L.append(num)
        return L


L1 = List()
L2 = SortedList()
L3 = List()
L4 = SortedList()

size = int(input("Enter a value for the size of the lists to be generated: "))
add_duplicates = input("Would you like your lists to have duplicates? (y/n): ")
if add_duplicates == 'y':
    list_object = GenerateList(size, size)
    native_list = list_object.ListWithDuplicates()
elif add_duplicates == 'n':
    list_object = GenerateList(size, size)
    native_list = list_object.ListWithoutDuplicates()

list_time = 0
sorted_list_time = 0
for num in native_list:
コード例 #26
0
 def __init__(self):
     self.__sortedListIdentifiers = SortedList()
     self.__sortedListConstants = SortedList()