Example #1
0
def test_islice():
    ss = SortedSet(load=7)

    assert [] == list(ss.islice())

    values = list(range(53))
    ss.update(values)

    for start in range(53):
        for stop in range(53):
            assert list(ss.islice(start, stop)) == values[start:stop]

    for start in range(53):
        for stop in range(53):
            assert list(ss.islice(start, stop,
                                  reverse=True)) == values[start:stop][::-1]

    for start in range(53):
        assert list(ss.islice(start=start)) == values[start:]
        assert list(ss.islice(start=start,
                              reverse=True)) == values[start:][::-1]

    for stop in range(53):
        assert list(ss.islice(stop=stop)) == values[:stop]
        assert list(ss.islice(stop=stop, reverse=True)) == values[:stop][::-1]
Example #2
0
class TwoSum:
    """Stores a set of integers and returns the pairs of values that sum to a 
    specified total.
    
    """
    def __init__(self, startList=None):
        """Initialize set of integers.
        
        """
        self._intSet = SortedSet()
        if startList is not None: self.addIntegerList(startList)

    def addIntegerList(self, intList):
        """Add integers from list to set
        
        """
        for i in intList:
            self.addInteger(i)

    def addInteger(self, integer):
        """Add a single integer to set
        
        """
        self._intSet.add(integer)

    def findSums(self, totalLow, totalHigh, requireUnique=False):
        """Return set of totals in range for which at least one pair of
        integers in set existins such that x + y = total.
        
        Optional parameter for uniqueness will exclude x + x = integer.
        """
        # Flip total high and low if passed out of order
        if totalLow > totalHigh:
            swap = totalLow
            totalLow = totalHigh
            totalHigh = swap

        # Initalize empty found totals set
        foundTotals = set()

        # Iterate over each integer in the set
        for x in self._intSet:
            # Determine search range for y
            # NOTE: start index is inclusive where stop index is exclusive
            # to match behavior of SortedSet.islice()
            iStart = self._intSet.bisect_left(totalLow - x)
            iStop = self._intSet.bisect_right(totalHigh - x)

            # Continue if search range invalid
            if iStart >= iStop: continue

            # Add each sum x + y to found totals
            for y in self._intSet.islice(iStart, iStop):
                foundTotals.add(x + y)

        # Return found totals set
        return foundTotals
    def _compute_virtual_points(self, lines : float, columns : float, inter_dist : float):
        self._start = ActuatorPoint(self._startX * inter_dist, self._startY * inter_dist)
        self._end = ActuatorPoint(self._endX * inter_dist, self._endY * inter_dist)

        v = SortedSet(key = cmp_to_key(self._cmp))

        v.add(self._start)

        if abs(self._end.first - self._start.first) < EPSILON:
            for l in range(0, lines):
                c = ActuatorPoint(self._start.first, l * inter_dist)
                if self._is_point_on_stroke(c):
                    v.add(c)

        else:
            coef = (self._end.second - self._start.second) / (self._end.first - self._start.first)
            orig = self._start.second - coef * self._start.first

            for l in range(0, lines):
                y = l * inter_dist

                ant = ActuatorPoint((y - orig)/coef, y)
                if self._is_point_on_stroke(ant):
                    v.add(ant)

                
            for c in range(0, columns):
                x = c * inter_dist
                res = ActuatorPoint(x, coef * x + orig)
                if self._is_point_on_stroke(res):
                    v.add(res)

        v.add(self._end)

        self._virtual_points = list(v.islice(0, len(v)))        

        if self._start > self._end:
            self._virtual_points = reversed(self._virtual_points)
def test_islice():
    ss = SortedSet(load=7)

    assert [] == list(ss.islice())

    values = list(range(53))
    ss.update(values)

    for start in range(53):
        for stop in range(53):
            assert list(ss.islice(start, stop)) == values[start:stop]

    for start in range(53):
        for stop in range(53):
            assert list(ss.islice(start, stop, reverse=True)) == values[start:stop][::-1]

    for start in range(53):
        assert list(ss.islice(start=start)) == values[start:]
        assert list(ss.islice(start=start, reverse=True)) == values[start:][::-1]

    for stop in range(53):
        assert list(ss.islice(stop=stop)) == values[:stop]
        assert list(ss.islice(stop=stop, reverse=True)) == values[:stop][::-1]