Ejemplo n.º 1
0
    def hasNext(self) -> bool:
        return self.current is not None

# Your BSTIterator object will be instantiated and called as such:
# obj = BSTIterator(root)
# param_1 = obj.next()
# param_2 = obj.hasNext()

root = TreeNode(5)
root.left = TreeNode(3)
root.left.right = TreeNode(4)
root.left.left = TreeNode(2)
root.right = TreeNode(7)
root.right.left = TreeNode(6)

# it = BSTIterator(root)
#
# while it.hasNext():
#     print(it.next())

tests = [
    (
        ["BSTIterator", "hasNext", "next", "hasNext", "next", "hasNext", "next", "hasNext", "next", "hasNext", "next", "hasNext", "next", "hasNext"],
        [[root], [], [], [], [], [], [], [], [], [], [], null, null, null],
        [null, true, 2, true, 3, true, 4, true, 5, true, 6, true, 7, false]
    )
]

run_object_tests(tests, cls=BSTIterator)
Ejemplo n.º 2
0
0 <= big, medium, small <= 1000
carType is 1, 2, or 3
At most 1000 calls will be made to addCar
"""
from Common.Constants import null, false, true
from Common.ObjectTestingUtils import run_object_tests


# Runtime: 280 ms, faster than 5.05% of Python3 online submissions for Design Parking System.
# Memory Usage: 14.6 MB, less than 80.79% of Python3 online submissions for Design Parking System.
class ParkingSystem:
    def __init__(self, big: int, medium: int, small: int):
        self.slots = [big, medium, small]

    def addCar(self, carType: int) -> bool:
        if self.slots[carType - 1] > 0:
            self.slots[carType - 1] -= 1
            return True
        return False


# Your ParkingSystem object will be instantiated and called as such:
# obj = ParkingSystem(big, medium, small)
# param_1 = obj.addCar(carType)

tests = [[["ParkingSystem", "addCar", "addCar", "addCar", "addCar"],
          [[1, 1, 0], [1], [2], [3], [1]], [null, true, true, false, false]]]

run_object_tests(tests, cls=ParkingSystem)
Ejemplo n.º 3
0
    "book", "book", "book", "book", "book", "book", "book", "book", "book",
    "book", "book", "book", "book", "book"
],
          [[], [20, 29], [13, 22], [44, 50], [1, 7],
           [2, 10], [14, 20], [19, 25], [36, 42], [45, 50], [47, 50], [39, 45],
           [44, 50], [16, 25], [45, 50], [45, 50], [12, 20], [21,
                                                              29], [11, 20],
           [12, 17], [34, 40], [10, 18], [38, 44], [23, 32], [38,
                                                              44], [15, 20],
           [27, 33], [34, 42], [44, 50], [35, 40], [24, 31]],
          [
              null, true, false, true, true, false, true, false, true, false,
              false, false, false, false, false, false, false, false, false,
              false, false, false, false, false, false, false, false, false,
              false, false, false
          ]],
         [[
             "MyCalendar", "book", "book", "book", "book", "book", "book",
             "book", "book", "book", "book"
         ],
          [[], [47, 50], [33, 41], [39, 45], [33, 42], [25, 32], [26, 35],
           [19, 25], [3, 8], [8, 13], [18, 27]],
          [
              null, true, true, false, false, true, false, true, true, true,
              false
          ]],
         [["MyCalendar", "book", "book", "book"],
          [[], [10, 20], [15, 25], [20, 30]], [None, true, false, true]]]

run_object_tests(tests, cls=MyCalendar)
Ejemplo n.º 4
0
from Common.ObjectTestingUtils import run_object_tests


class OrderedStream:
    def __init__(self, n: int):
        self.next_id = 1
        self.inserted = {}

    def insert(self, idKey: int, value: str) -> List[str]:
        self.inserted[idKey] = value
        result = []
        i = self.next_id
        while self.inserted.get(i):
            result.append(self.inserted[i])
            del self.inserted[i]
            i += 1
        self.next_id = i
        return result


# Your OrderedStream object will be instantiated and called as such:
# obj = OrderedStream(n)
# param_1 = obj.insert(idKey,value)

tests = [(["OrderedStream", "insert", "insert", "insert", "insert",
           "insert"], [[5], [3, "ccccc"], [1, "aaaaa"], [2, "bbbbb"],
                       [5, "eeeee"], [4, "ddddd"]],
          [null, [], ["aaaaa"], ["bbbbb", "ccccc"], [], ["ddddd", "eeeee"]])]

run_object_tests(tests, cls=OrderedStream)
Ejemplo n.º 5
0
        idx = bisect.bisect_left(self.data[index], [snap_id, 0])
        if idx < len(self.data[index]) and self.data[index][idx][0] > snap_id:
            idx -= 1
        return self.data[index][idx][1] if idx < len(
            self.data[index]) else self.data[index][-1][1]


# Your SnapshotArray object will be instantiated and called as such:
# obj = SnapshotArray(length)
# obj.set(index,val)
# param_2 = obj.snap()
# param_3 = obj.get(index,snap_id)

tests = [([
    "SnapshotArray", "set", "snap", "snap", "set", "set", "get", "get", "get"
], [[3], [1, 6], [], [], [1, 19], [0, 4], [2, 1], [2, 0],
    [0, 1]], [null, null, 0, 1, null, null, 0, 0, 0]),
         ([
             "SnapshotArray", "snap", "get", "get", "set", "get", "set", "get",
             "set"
         ], [[2], [], [1, 0], [0, 0], [1, 8], [1, 0], [0, 20], [0, 0],
             [0, 7]], [null, 0, 0, 0, null, 0, null, 0, null]),
         (["SnapshotArray", "snap", "snap", "get", "set", "snap",
           "set"], [[4], [], [], [3, 1], [2, 4], [],
                    [1, 4]], [null, 0, 1, 0, null, 2, null]),
         (["SnapshotArray", "set", "snap", "set",
           "get"], [[3], [0, 5], [], [0, 6], [0, 0]], [null, null, 0, null,
                                                       5])]

run_object_tests(tests, cls=SnapshotArray)
Ejemplo n.º 6
0
    def isEmpty(self) -> bool:
        return self.f == self.b

    def isFull(self) -> bool:
        return self.b == self.f + len(self.data)

# Your MyCircularQueue object will be instantiated and called as such:
# obj = MyCircularQueue(k)
# param_1 = obj.enQueue(value)
# param_2 = obj.deQueue()
# param_3 = obj.Front()
# param_4 = obj.Rear()
# param_5 = obj.isEmpty()
# param_6 = obj.isFull()

tests = [
    (
        ["MyCircularQueue","enQueue","deQueue","Front","deQueue","Front","Rear","enQueue","isFull","deQueue","Rear","enQueue"],
        [[3],[7],[],[],[],[],[],[0],[],[],[],[3]],
        [null,true,true,-1,false,-1,-1,true,false,true,-1,true]
    ),
    (
        ["MyCircularQueue", "enQueue", "enQueue", "enQueue", "enQueue", "Rear", "isFull", "deQueue", "enQueue", "Rear"],
        [[3], [1], [2], [3], [4], [], [], [], [4], []],
        [null, true, true, true, false, 3, true, true, true, 4]
    )
]

run_object_tests(tests, cls=MyCircularQueue)
Ejemplo n.º 7
0
                cur[0] = index
                for j in range(i, 2 * len(word) - 1):
                    cur = cur[word[j % len(word)]]
                    cur[0] = index

    def f(self, prefix: str, suffix: str) -> int:
        cur = self.trie
        for l in suffix + "#" + prefix:
            if l not in cur:
                return -1
            cur = cur[l]
        return cur[0]


# Your WordFilter object will be instantiated and called as such:
# obj = WordFilter(words)
# param_1 = obj.f(prefix,suffix)

tests = [[["WordFilter", "f", "f", "f", "f", "f", "f", "f", "f", "f", "f"],
          [[[
              "cabaabaaaa", "ccbcababac", "bacaabccba", "bcbbcbacaa",
              "abcaccbcaa", "accabaccaa", "cabcbbbcca", "ababccabcb",
              "caccbbcbab", "bccbacbcba"
          ]], ["bccbacbcba", "a"], ["ab", "abcaccbcaa"], ["a", "aa"],
           ["cabaaba", "abaaaa"], ["cacc", "accbbcbab"], ["ccbcab", "bac"],
           ["bac", "cba"], ["ac", "accabaccaa"], ["bcbb", "aa"],
           ["ccbca", "cbcababac"]], [null, 9, 4, 5, 0, 8, 1, 2, 5, 3, 1]],
         [["WordFilter", "f"], [[["apple"]], ["a", "e"]], [null, 0]]]

run_object_tests(tests, cls=WordFilter)