{"record":{"id":"7ab28bc6bc8fed6d","repo":"krahets/hello-algo","slug":"error-7ab28b","errorCode":null,"errorMessage":"雙向佇列為空","messagePattern":"雙向佇列為空","errorType":"exception","errorClass":"IndexError","httpStatus":null,"severity":"error","filePath":"zh-hant/codes/python/chapter_stack_and_queue/array_deque.py","lineNumber":76,"sourceCode":"\n    def pop_first(self) -> int:\n        \"\"\"佇列首出列\"\"\"\n        num = self.peek_first()\n        # 佇列首指標向後移動一位\n        self._front = self.index(self._front + 1)\n        self._size -= 1\n        return num\n\n    def pop_last(self) -> int:\n        \"\"\"佇列尾出列\"\"\"\n        num = self.peek_last()\n        self._size -= 1\n        return num\n\n    def peek_first(self) -> int:\n        \"\"\"訪問佇列首元素\"\"\"\n        if self.is_empty():\n            raise IndexError(\"雙向佇列為空\")\n        return self._nums[self._front]\n\n    def peek_last(self) -> int:\n        \"\"\"訪問佇列尾元素\"\"\"\n        if self.is_empty():\n            raise IndexError(\"雙向佇列為空\")\n        # 計算尾元素索引\n        last = self.index(self._front + self._size - 1)\n        return self._nums[last]\n\n    def to_array(self) -> list[int]:\n        \"\"\"返回陣列用於列印\"\"\"\n        # 僅轉換有效長度範圍內的串列元素\n        res = []\n        for i in range(self._size):\n            res.append(self._nums[self.index(self._front + i)])\n        return res\n","sourceCodeStart":58,"sourceCodeEnd":94,"githubUrl":"https://github.com/krahets/hello-algo/blob/69932aed1891a7b7f6a0de88cd116d3fe13e7032/zh-hant/codes/python/chapter_stack_and_queue/array_deque.py#L58-L94","documentation":"Raised by peek_first() of a circular-array-based deque when the deque has zero elements. The check uses is_empty() which tests _size == 0. Since pop_first() delegates to peek_first() to retrieve the front value before advancing the _front pointer, both operations fail identically on an empty deque.","triggerScenarios":"Calling peek_first() or pop_first() on a freshly constructed ArrayDeque before any push; draining all elements and then accessing the front once more.","commonSituations":"Sliding-window algorithms that peek at the front after the window empties; BFS implementations where the deque is exhausted; testing edge cases with empty initial data.","solutions":["Check deque.is_empty() before calling peek_first() or pop_first()","Use while not deque.is_empty() as the loop condition when draining","Guard with try/except IndexError for defensive front-access"],"exampleFix":"# before\nval = deque.peek_first()\n\n# after\nval = deque.peek_first() if not deque.is_empty() else None","handlingStrategy":"validation","validationCode":"if not deque.is_empty():\n    val = deque.peek_first()\nelse:\n    val = None","typeGuard":null,"tryCatchPattern":"try:\n    val = deque.peek_first()\nexcept IndexError:\n    val = None","preventionTips":["Check is_empty() before peek_first() or pop_first()","Note that pop_first() internally calls peek_first(), so it inherits the guard","Use size() as the loop bound in single-ended draining loops"],"tags":["data-structure","deque","python","circular-array"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}