dsa.deque API documentation

# dsa.deque

Module containing deque classes.

View Source

      1""" Module containing deque classes. """
      2
      3class Deque:
      4    """
      5    A static deque implementation that supports appending and popping elements
      6    from both ends, with a fixed capacity.
      7    """
      8    def __init__(self, capacity: int=10):
      9        """
     10        Initialize a deque with a fixed capacity.
     11
     12        Args:
     13            capacity (int): The initial size of the deque (default is 10).
     14        """
     15        self._array = [None] * capacity
     16        self._left = -1
     17        self._right = 0
     18        self.count = 0
     19
     20    def push_front(self, element):
     21        """
     22        Push an element at the front of the deque. (synonym for append_left)
     23        Raises an exception when the deque is full.
     24
     25        Args:
     26            element: The element to append.
     27
     28        Raises:
     29            Exception: If the deque is full.
     30        """
     31        self.append_left(element)
     32
     33    def push_back(self, element):
     34        """
     35        Push an element at the back of the deque. (synonym for append_right)
     36        Raises an exception when the deque is full.
     37
     38        Args:
     39            element: The element to append.
     40
     41        Raises:
     42            Exception: If the deque is full.
     43        """
     44        self.append_right(element)
     45
     46    def pop_front(self):
     47        """
     48        Pop an element from the front of the deque. (synonym for pop_left)
     49        Raises an exception if the deque is empty.
     50
     51        Returns:
     52            The leftmost element of the deque.
     53
     54        Raises:
     55            Exception: If the deque is empty.
     56        """
     57        return self.pop_left()
     58
     59    def pop_back(self):
     60        """
     61        Pop an element from the back of the deque. (synonym for pop_right)
     62        Raises an exception if the deque is empty.
     63
     64        Returns:
     65            The rightmost element of the deque.
     66
     67        Raises:
     68            Exception: If the deque is empty.
     69        """
     70        return self.pop_right()
     71
     72    def front(self):
     73        """
     74        Get the element at the front of the deque without removing it.
     75        Raises an exception if the deque is empty.
     76
     77        Returns:
     78            The leftmost element.
     79
     80        Raises:
     81            Exception: If the deque is empty.
     82        """
     83        return self.peek_left()
     84
     85    def back(self):
     86        """
     87        Get the element at the back of the deque without removing it.
     88        Raises an exception if the deque is empty.
     89
     90        Returns:
     91            The rightmost element.
     92
     93        Raises:
     94            Exception: If the deque is empty.
     95        """
     96        return self.peek_right()
     97
     98    def append_left(self, element):
     99        """
    100        Append an element to the left of the deque. Raises an exception when the deque is full.
    101
    102        Args:
    103            element: The element to append.
    104
    105        Raises:
    106            Exception: If the deque is full.
    107        """
    108        if self.count >= self.capacity():
    109            raise Exception("Deque Full")
    110        if self._left < 0:
    111            self._left = self.capacity() - 1
    112        self._array[self._left] = element
    113        self._left -= 1
    114        self.count += 1
    115
    116    def append_right(self, element):
    117        """
    118        Append an element to the right of the deque. Raises an exception when the deque is full.
    119
    120        Args:
    121            element: The element to append.
    122
    123        Raises:
    124            Exception: If the deque is full.
    125        """
    126        if self.count >= self.capacity():
    127            raise Exception("Deque Full")
    128        if self._right > self.capacity() - 1:
    129            self._right = 0
    130        self._array[self._right] = element
    131        self._right += 1
    132        self.count += 1
    133
    134    def pop_left(self):
    135        """
    136        Remove and return the element from the left of the deque. Raises an exception if the deque is empty.
    137
    138        Returns:
    139            The leftmost element of the deque.
    140
    141        Raises:
    142            Exception: If the deque is empty.
    143        """
    144        if self.is_empty():
    145            raise Exception("Empty Deque")
    146
    147        element = self._array[self._left + 1]
    148        self._left += 1
    149        if self._left + 1 >= len(self._array):
    150            self._left = -1
    151        self.count -= 1
    152        return element
    153
    154    def pop_right(self):
    155        """
    156        Pop an element from right the deque. Raise an exception if the deque is empty.
    157
    158        Returns:
    159            The rightmost element of the deque.
    160
    161        Raises:
    162            Exception: If the deque is empty.
    163        """
    164        if self.is_empty():
    165            raise Exception("Empty Deque")
    166        element = self._array[self._right - 1]
    167        self.count -= 1
    168
    169        self._right -= 1
    170        if self._right < 0:
    171            self._right = len(self._array) - 1
    172        return element
    173
    174    def peek_left(self):
    175        """
    176        Get the element at the left of the deque without removing it.
    177        Raises an exception if the deque is empty.
    178
    179        Returns:
    180            The leftmost element.
    181
    182        Raises:
    183            Exception: If the deque is empty.
    184        """
    185        if self.is_empty():
    186            raise Exception("Empty deque")
    187        return self._array[self._left + 1]
    188
    189    def peek_right(self):
    190        """
    191        Get the element at the right of the deque without removing it.
    192        Raises an exception if the deque is empty.
    193
    194        Returns:
    195            The rightmost element.
    196
    197        Raises:
    198            Exception: If the deque is empty.
    199        """
    200        if self.is_empty():
    201            raise Exception("Empty deque")
    202        return self._array[self._right - 1]
    203
    204    def is_empty(self) -> bool:
    205        """
    206        Return a Boolean on whether the deque is empty or not.
    207        """
    208        return self.count == 0
    209
    210    @classmethod
    211    def from_list(cls, alist: list):
    212        """
    213        Create a deque from a given list. Raises an exception if list exceeds the deque's capacity.
    214
    215        Args:
    216            alist: The list to initialize the deque with.
    217
    218        Returns:
    219            a deque instance with elements from the list.
    220
    221        Raises:
    222            Exception: If list exceeds the deque's capacity.
    223        """
    224        dq = cls()
    225        for e in alist:
    226            dq.append_right(e)
    227        return dq
    228
    229    def to_list(self):
    230        """
    231        Convert the deque's contents into a list.
    232
    233        Returns:
    234            A list containg the elements in the deque.
    235        """
    236        arr = []
    237        for i in range(self.count):
    238            index = (i + self._left + 1) % len(self._array)
    239            arr.append(self._array[index])
    240        return arr
    241
    242    def capacity(self) -> int:
    243        """
    244        Get the maximum capacity of the deque.
    245
    246        Returns:
    247            The capacity of the deque.
    248        """
    249        return len(self._array)
    250
    251    def __len__(self) -> int:
    252        """
    253        Get the number of elements in the deque.
    254
    255        Returns:
    256            The count of elements.
    257        """
    258        return self.count
    259
    260    def __repr__(self):
    261        """
    262        String representation of the deque for debugging purposes.
    263
    264        Returns:
    265            A string displaying the contents and size of the deque.
    266        """
    267        arr = []
    268        for i in range(self.count):
    269            index = (i + self._left + 1) % len(self._array)
    270            arr.append(str(self._array[index]))
    271        arrstr = ", ".join(arr)
    272        return f"[{arrstr}] Count: {self.count} Capacity: {len(self._array)}" # {self.array}"
    273
    274    def __eq__(self, other):
    275        """
    276        Compare two deques (static/dynamic) for value-based equality.
    277
    278        Returns:
    279            True if both are Deque or DynamicDeque instances and their contents are equal.
    280            For non-deque types, returns NotImplemented.
    281        """
    282        if isinstance(other, (Deque, DynamicDeque)):
    283            return self.to_list() == other.to_list()
    284        return NotImplemented
    285
    286class DynamicDeque(Deque):
    287    """
    288    A dynamic deque implementation that adjusts its capacity as needed.
    289    """
    290    def grow(self):
    291        """
    292        Helper method to double the capacity of the deque.
    293        """
    294        new_array = [ None ] * self.capacity() * 2
    295
    296        # copy elements
    297        for i in range(self.count):
    298            index = (i + self._left + 1) % len(self._array)
    299            new_array[i] = self._array[index]
    300
    301        self._array = new_array
    302        self._left = -1
    303        self._right = self.count
    304
    305    def shrink(self):
    306        """
    307        Helper method to halve the capacity of the deque.
    308        """
    309        if self.capacity() < 10:
    310            return
    311
    312        new_capacity = self.capacity() // 2
    313        new_array = [ None ] * new_capacity
    314
    315        # copy elements
    316        for i in range(self.count):
    317            index = (i + self._left + 1) % len(self._array)
    318            new_array[i] = self._array[index]
    319
    320        self._array = new_array
    321        self._left = -1
    322        self._right = self.count
    323
    324
    325    def check_capacity(self):
    326        """
    327        Helper method to adjust the capacity of the deque based on its count:
    328        if count >= capacity, grow the deque
    329        if count <= 1/4 of capacity, shrink the deque
    330        """
    331        if self.count >= self.capacity():
    332            self.grow()
    333        elif self.count * 4 <= self.capacity():
    334            self.shrink()
    335
    336    def append_left(self, element):
    337        """
    338        Append an element to the left, adjusting capacity if needed.
    339
    340        Args:
    341            element: The element to append.
    342         """
    343        self.check_capacity()
    344        super().append_left(element)
    345
    346    def append_right(self, element):
    347        """
    348        Append an element to the right, adjusting capacity if needed.
    349
    350        Args:
    351            element: The element to append.
    352        """
    353        self.check_capacity()
    354        super().append_right(element)
    355
    356    def pop_left(self):
    357        """
    358        Remove and return the element from the left, adjusting capacity if needed.
    359
    360        Returns:
    361            The leftmost element.
    362        """
    363        self.check_capacity()
    364        return super().pop_left()
    365
    366
    367    def pop_right(self):
    368        """
    369        Remove and return the element from the right, adjusting capacity if needed.
    370
    371        Returns:
    372            The rightmost element.
    373        """
    374        self.check_capacity()
    375        return super().pop_right()
    376

class Deque: View Source

      4class Deque:
      5    """
      6    A static deque implementation that supports appending and popping elements
      7    from both ends, with a fixed capacity.
      8    """
      9    def __init__(self, capacity: int=10):
     10        """
     11        Initialize a deque with a fixed capacity.
     12
     13        Args:
     14            capacity (int): The initial size of the deque (default is 10).
     15        """
     16        self._array = [None] * capacity
     17        self._left = -1
     18        self._right = 0
     19        self.count = 0
     20
     21    def push_front(self, element):
     22        """
     23        Push an element at the front of the deque. (synonym for append_left)
     24        Raises an exception when the deque is full.
     25
     26        Args:
     27            element: The element to append.
     28
     29        Raises:
     30            Exception: If the deque is full.
     31        """
     32        self.append_left(element)
     33
     34    def push_back(self, element):
     35        """
     36        Push an element at the back of the deque. (synonym for append_right)
     37        Raises an exception when the deque is full.
     38
     39        Args:
     40            element: The element to append.
     41
     42        Raises:
     43            Exception: If the deque is full.
     44        """
     45        self.append_right(element)
     46
     47    def pop_front(self):
     48        """
     49        Pop an element from the front of the deque. (synonym for pop_left)
     50        Raises an exception if the deque is empty.
     51
     52        Returns:
     53            The leftmost element of the deque.
     54
     55        Raises:
     56            Exception: If the deque is empty.
     57        """
     58        return self.pop_left()
     59
     60    def pop_back(self):
     61        """
     62        Pop an element from the back of the deque. (synonym for pop_right)
     63        Raises an exception if the deque is empty.
     64
     65        Returns:
     66            The rightmost element of the deque.
     67
     68        Raises:
     69            Exception: If the deque is empty.
     70        """
     71        return self.pop_right()
     72
     73    def front(self):
     74        """
     75        Get the element at the front of the deque without removing it.
     76        Raises an exception if the deque is empty.
     77
     78        Returns:
     79            The leftmost element.
     80
     81        Raises:
     82            Exception: If the deque is empty.
     83        """
     84        return self.peek_left()
     85
     86    def back(self):
     87        """
     88        Get the element at the back of the deque without removing it.
     89        Raises an exception if the deque is empty.
     90
     91        Returns:
     92            The rightmost element.
     93
     94        Raises:
     95            Exception: If the deque is empty.
     96        """
     97        return self.peek_right()
     98
     99    def append_left(self, element):
    100        """
    101        Append an element to the left of the deque. Raises an exception when the deque is full.
    102
    103        Args:
    104            element: The element to append.
    105
    106        Raises:
    107            Exception: If the deque is full.
    108        """
    109        if self.count >= self.capacity():
    110            raise Exception("Deque Full")
    111        if self._left < 0:
    112            self._left = self.capacity() - 1
    113        self._array[self._left] = element
    114        self._left -= 1
    115        self.count += 1
    116
    117    def append_right(self, element):
    118        """
    119        Append an element to the right of the deque. Raises an exception when the deque is full.
    120
    121        Args:
    122            element: The element to append.
    123
    124        Raises:
    125            Exception: If the deque is full.
    126        """
    127        if self.count >= self.capacity():
    128            raise Exception("Deque Full")
    129        if self._right > self.capacity() - 1:
    130            self._right = 0
    131        self._array[self._right] = element
    132        self._right += 1
    133        self.count += 1
    134
    135    def pop_left(self):
    136        """
    137        Remove and return the element from the left of the deque. Raises an exception if the deque is empty.
    138
    139        Returns:
    140            The leftmost element of the deque.
    141
    142        Raises:
    143            Exception: If the deque is empty.
    144        """
    145        if self.is_empty():
    146            raise Exception("Empty Deque")
    147
    148        element = self._array[self._left + 1]
    149        self._left += 1
    150        if self._left + 1 >= len(self._array):
    151            self._left = -1
    152        self.count -= 1
    153        return element
    154
    155    def pop_right(self):
    156        """
    157        Pop an element from right the deque. Raise an exception if the deque is empty.
    158
    159        Returns:
    160            The rightmost element of the deque.
    161
    162        Raises:
    163            Exception: If the deque is empty.
    164        """
    165        if self.is_empty():
    166            raise Exception("Empty Deque")
    167        element = self._array[self._right - 1]
    168        self.count -= 1
    169
    170        self._right -= 1
    171        if self._right < 0:
    172            self._right = len(self._array) - 1
    173        return element
    174
    175    def peek_left(self):
    176        """
    177        Get the element at the left of the deque without removing it.
    178        Raises an exception if the deque is empty.
    179
    180        Returns:
    181            The leftmost element.
    182
    183        Raises:
    184            Exception: If the deque is empty.
    185        """
    186        if self.is_empty():
    187            raise Exception("Empty deque")
    188        return self._array[self._left + 1]
    189
    190    def peek_right(self):
    191        """
    192        Get the element at the right of the deque without removing it.
    193        Raises an exception if the deque is empty.
    194
    195        Returns:
    196            The rightmost element.
    197
    198        Raises:
    199            Exception: If the deque is empty.
    200        """
    201        if self.is_empty():
    202            raise Exception("Empty deque")
    203        return self._array[self._right - 1]
    204
    205    def is_empty(self) -> bool:
    206        """
    207        Return a Boolean on whether the deque is empty or not.
    208        """
    209        return self.count == 0
    210
    211    @classmethod
    212    def from_list(cls, alist: list):
    213        """
    214        Create a deque from a given list. Raises an exception if list exceeds the deque's capacity.
    215
    216        Args:
    217            alist: The list to initialize the deque with.
    218
    219        Returns:
    220            a deque instance with elements from the list.
    221
    222        Raises:
    223            Exception: If list exceeds the deque's capacity.
    224        """
    225        dq = cls()
    226        for e in alist:
    227            dq.append_right(e)
    228        return dq
    229
    230    def to_list(self):
    231        """
    232        Convert the deque's contents into a list.
    233
    234        Returns:
    235            A list containg the elements in the deque.
    236        """
    237        arr = []
    238        for i in range(self.count):
    239            index = (i + self._left + 1) % len(self._array)
    240            arr.append(self._array[index])
    241        return arr
    242
    243    def capacity(self) -> int:
    244        """
    245        Get the maximum capacity of the deque.
    246
    247        Returns:
    248            The capacity of the deque.
    249        """
    250        return len(self._array)
    251
    252    def __len__(self) -> int:
    253        """
    254        Get the number of elements in the deque.
    255
    256        Returns:
    257            The count of elements.
    258        """
    259        return self.count
    260
    261    def __repr__(self):
    262        """
    263        String representation of the deque for debugging purposes.
    264
    265        Returns:
    266            A string displaying the contents and size of the deque.
    267        """
    268        arr = []
    269        for i in range(self.count):
    270            index = (i + self._left + 1) % len(self._array)
    271            arr.append(str(self._array[index]))
    272        arrstr = ", ".join(arr)
    273        return f"[{arrstr}] Count: {self.count} Capacity: {len(self._array)}" # {self.array}"
    274
    275    def __eq__(self, other):
    276        """
    277        Compare two deques (static/dynamic) for value-based equality.
    278
    279        Returns:
    280            True if both are Deque or DynamicDeque instances and their contents are equal.
    281            For non-deque types, returns NotImplemented.
    282        """
    283        if isinstance(other, (Deque, DynamicDeque)):
    284            return self.to_list() == other.to_list()
    285        return NotImplemented

A static deque implementation that supports appending and popping elements from both ends, with a fixed capacity.

Deque(capacity: int = 10) View Source

     9    def __init__(self, capacity: int=10):
    10        """
    11        Initialize a deque with a fixed capacity.
    12
    13        Args:
    14            capacity (int): The initial size of the deque (default is 10).
    15        """
    16        self._array = [None] * capacity
    17        self._left = -1
    18        self._right = 0
    19        self.count = 0

Initialize a deque with a fixed capacity.

Args: capacity (int): The initial size of the deque (default is 10).

count
def push_front(self, element): View Source

    21    def push_front(self, element):
    22        """
    23        Push an element at the front of the deque. (synonym for append_left)
    24        Raises an exception when the deque is full.
    25
    26        Args:
    27            element: The element to append.
    28
    29        Raises:
    30            Exception: If the deque is full.
    31        """
    32        self.append_left(element)

Push an element at the front of the deque. (synonym for append_left) Raises an exception when the deque is full.

Args: element: The element to append.

Raises: Exception: If the deque is full.

def push_back(self, element): View Source

    34    def push_back(self, element):
    35        """
    36        Push an element at the back of the deque. (synonym for append_right)
    37        Raises an exception when the deque is full.
    38
    39        Args:
    40            element: The element to append.
    41
    42        Raises:
    43            Exception: If the deque is full.
    44        """
    45        self.append_right(element)

Push an element at the back of the deque. (synonym for append_right) Raises an exception when the deque is full.

Args: element: The element to append.

Raises: Exception: If the deque is full.

def pop_front(self): View Source

    47    def pop_front(self):
    48        """
    49        Pop an element from the front of the deque. (synonym for pop_left)
    50        Raises an exception if the deque is empty.
    51
    52        Returns:
    53            The leftmost element of the deque.
    54
    55        Raises:
    56            Exception: If the deque is empty.
    57        """
    58        return self.pop_left()

Pop an element from the front of the deque. (synonym for pop_left) Raises an exception if the deque is empty.

Returns: The leftmost element of the deque.

Raises: Exception: If the deque is empty.

def pop_back(self): View Source

    60    def pop_back(self):
    61        """
    62        Pop an element from the back of the deque. (synonym for pop_right)
    63        Raises an exception if the deque is empty.
    64
    65        Returns:
    66            The rightmost element of the deque.
    67
    68        Raises:
    69            Exception: If the deque is empty.
    70        """
    71        return self.pop_right()

Pop an element from the back of the deque. (synonym for pop_right) Raises an exception if the deque is empty.

Returns: The rightmost element of the deque.

Raises: Exception: If the deque is empty.

def front(self): View Source

    73    def front(self):
    74        """
    75        Get the element at the front of the deque without removing it.
    76        Raises an exception if the deque is empty.
    77
    78        Returns:
    79            The leftmost element.
    80
    81        Raises:
    82            Exception: If the deque is empty.
    83        """
    84        return self.peek_left()

Get the element at the front of the deque without removing it. Raises an exception if the deque is empty.

Returns: The leftmost element.

Raises: Exception: If the deque is empty.

def back(self): View Source

    86    def back(self):
    87        """
    88        Get the element at the back of the deque without removing it.
    89        Raises an exception if the deque is empty.
    90
    91        Returns:
    92            The rightmost element.
    93
    94        Raises:
    95            Exception: If the deque is empty.
    96        """
    97        return self.peek_right()

Get the element at the back of the deque without removing it. Raises an exception if the deque is empty.

Returns: The rightmost element.

Raises: Exception: If the deque is empty.

def append_left(self, element): View Source

     99    def append_left(self, element):
    100        """
    101        Append an element to the left of the deque. Raises an exception when the deque is full.
    102
    103        Args:
    104            element: The element to append.
    105
    106        Raises:
    107            Exception: If the deque is full.
    108        """
    109        if self.count >= self.capacity():
    110            raise Exception("Deque Full")
    111        if self._left < 0:
    112            self._left = self.capacity() - 1
    113        self._array[self._left] = element
    114        self._left -= 1
    115        self.count += 1

Append an element to the left of the deque. Raises an exception when the deque is full.

Args: element: The element to append.

Raises: Exception: If the deque is full.

def append_right(self, element): View Source

    117    def append_right(self, element):
    118        """
    119        Append an element to the right of the deque. Raises an exception when the deque is full.
    120
    121        Args:
    122            element: The element to append.
    123
    124        Raises:
    125            Exception: If the deque is full.
    126        """
    127        if self.count >= self.capacity():
    128            raise Exception("Deque Full")
    129        if self._right > self.capacity() - 1:
    130            self._right = 0
    131        self._array[self._right] = element
    132        self._right += 1
    133        self.count += 1

Append an element to the right of the deque. Raises an exception when the deque is full.

Args: element: The element to append.

Raises: Exception: If the deque is full.

def pop_left(self): View Source

    135    def pop_left(self):
    136        """
    137        Remove and return the element from the left of the deque. Raises an exception if the deque is empty.
    138
    139        Returns:
    140            The leftmost element of the deque.
    141
    142        Raises:
    143            Exception: If the deque is empty.
    144        """
    145        if self.is_empty():
    146            raise Exception("Empty Deque")
    147
    148        element = self._array[self._left + 1]
    149        self._left += 1
    150        if self._left + 1 >= len(self._array):
    151            self._left = -1
    152        self.count -= 1
    153        return element

Remove and return the element from the left of the deque. Raises an exception if the deque is empty.

Returns: The leftmost element of the deque.

Raises: Exception: If the deque is empty.

def pop_right(self): View Source

    155    def pop_right(self):
    156        """
    157        Pop an element from right the deque. Raise an exception if the deque is empty.
    158
    159        Returns:
    160            The rightmost element of the deque.
    161
    162        Raises:
    163            Exception: If the deque is empty.
    164        """
    165        if self.is_empty():
    166            raise Exception("Empty Deque")
    167        element = self._array[self._right - 1]
    168        self.count -= 1
    169
    170        self._right -= 1
    171        if self._right < 0:
    172            self._right = len(self._array) - 1
    173        return element

Pop an element from right the deque. Raise an exception if the deque is empty.

Returns: The rightmost element of the deque.

Raises: Exception: If the deque is empty.

def peek_left(self): View Source

    175    def peek_left(self):
    176        """
    177        Get the element at the left of the deque without removing it.
    178        Raises an exception if the deque is empty.
    179
    180        Returns:
    181            The leftmost element.
    182
    183        Raises:
    184            Exception: If the deque is empty.
    185        """
    186        if self.is_empty():
    187            raise Exception("Empty deque")
    188        return self._array[self._left + 1]

Get the element at the left of the deque without removing it. Raises an exception if the deque is empty.

Returns: The leftmost element.

Raises: Exception: If the deque is empty.

def peek_right(self): View Source

    190    def peek_right(self):
    191        """
    192        Get the element at the right of the deque without removing it.
    193        Raises an exception if the deque is empty.
    194
    195        Returns:
    196            The rightmost element.
    197
    198        Raises:
    199            Exception: If the deque is empty.
    200        """
    201        if self.is_empty():
    202            raise Exception("Empty deque")
    203        return self._array[self._right - 1]

Get the element at the right of the deque without removing it. Raises an exception if the deque is empty.

Returns: The rightmost element.

Raises: Exception: If the deque is empty.

def is_empty(self) -> bool: View Source

    205    def is_empty(self) -> bool:
    206        """
    207        Return a Boolean on whether the deque is empty or not.
    208        """
    209        return self.count == 0

Return a Boolean on whether the deque is empty or not.

@classmethod
def from_list(cls, alist: list): View Source

    211    @classmethod
    212    def from_list(cls, alist: list):
    213        """
    214        Create a deque from a given list. Raises an exception if list exceeds the deque's capacity.
    215
    216        Args:
    217            alist: The list to initialize the deque with.
    218
    219        Returns:
    220            a deque instance with elements from the list.
    221
    222        Raises:
    223            Exception: If list exceeds the deque's capacity.
    224        """
    225        dq = cls()
    226        for e in alist:
    227            dq.append_right(e)
    228        return dq

Create a deque from a given list. Raises an exception if list exceeds the deque's capacity.

Args: alist: The list to initialize the deque with.

Returns: a deque instance with elements from the list.

Raises: Exception: If list exceeds the deque's capacity.

def to_list(self): View Source

    230    def to_list(self):
    231        """
    232        Convert the deque's contents into a list.
    233
    234        Returns:
    235            A list containg the elements in the deque.
    236        """
    237        arr = []
    238        for i in range(self.count):
    239            index = (i + self._left + 1) % len(self._array)
    240            arr.append(self._array[index])
    241        return arr

Convert the deque's contents into a list.

Returns: A list containg the elements in the deque.

def capacity(self) -> int: View Source

    243    def capacity(self) -> int:
    244        """
    245        Get the maximum capacity of the deque.
    246
    247        Returns:
    248            The capacity of the deque.
    249        """
    250        return len(self._array)

Get the maximum capacity of the deque.

Returns: The capacity of the deque.

class DynamicDeque(Deque): View Source

    287class DynamicDeque(Deque):
    288    """
    289    A dynamic deque implementation that adjusts its capacity as needed.
    290    """
    291    def grow(self):
    292        """
    293        Helper method to double the capacity of the deque.
    294        """
    295        new_array = [ None ] * self.capacity() * 2
    296
    297        # copy elements
    298        for i in range(self.count):
    299            index = (i + self._left + 1) % len(self._array)
    300            new_array[i] = self._array[index]
    301
    302        self._array = new_array
    303        self._left = -1
    304        self._right = self.count
    305
    306    def shrink(self):
    307        """
    308        Helper method to halve the capacity of the deque.
    309        """
    310        if self.capacity() < 10:
    311            return
    312
    313        new_capacity = self.capacity() // 2
    314        new_array = [ None ] * new_capacity
    315
    316        # copy elements
    317        for i in range(self.count):
    318            index = (i + self._left + 1) % len(self._array)
    319            new_array[i] = self._array[index]
    320
    321        self._array = new_array
    322        self._left = -1
    323        self._right = self.count
    324
    325
    326    def check_capacity(self):
    327        """
    328        Helper method to adjust the capacity of the deque based on its count:
    329        if count >= capacity, grow the deque
    330        if count <= 1/4 of capacity, shrink the deque
    331        """
    332        if self.count >= self.capacity():
    333            self.grow()
    334        elif self.count * 4 <= self.capacity():
    335            self.shrink()
    336
    337    def append_left(self, element):
    338        """
    339        Append an element to the left, adjusting capacity if needed.
    340
    341        Args:
    342            element: The element to append.
    343         """
    344        self.check_capacity()
    345        super().append_left(element)
    346
    347    def append_right(self, element):
    348        """
    349        Append an element to the right, adjusting capacity if needed.
    350
    351        Args:
    352            element: The element to append.
    353        """
    354        self.check_capacity()
    355        super().append_right(element)
    356
    357    def pop_left(self):
    358        """
    359        Remove and return the element from the left, adjusting capacity if needed.
    360
    361        Returns:
    362            The leftmost element.
    363        """
    364        self.check_capacity()
    365        return super().pop_left()
    366
    367
    368    def pop_right(self):
    369        """
    370        Remove and return the element from the right, adjusting capacity if needed.
    371
    372        Returns:
    373            The rightmost element.
    374        """
    375        self.check_capacity()
    376        return super().pop_right()

A dynamic deque implementation that adjusts its capacity as needed.

def grow(self): View Source

    291    def grow(self):
    292        """
    293        Helper method to double the capacity of the deque.
    294        """
    295        new_array = [ None ] * self.capacity() * 2
    296
    297        # copy elements
    298        for i in range(self.count):
    299            index = (i + self._left + 1) % len(self._array)
    300            new_array[i] = self._array[index]
    301
    302        self._array = new_array
    303        self._left = -1
    304        self._right = self.count

Helper method to double the capacity of the deque.

def shrink(self): View Source

    306    def shrink(self):
    307        """
    308        Helper method to halve the capacity of the deque.
    309        """
    310        if self.capacity() < 10:
    311            return
    312
    313        new_capacity = self.capacity() // 2
    314        new_array = [ None ] * new_capacity
    315
    316        # copy elements
    317        for i in range(self.count):
    318            index = (i + self._left + 1) % len(self._array)
    319            new_array[i] = self._array[index]
    320
    321        self._array = new_array
    322        self._left = -1
    323        self._right = self.count

Helper method to halve the capacity of the deque.

def check_capacity(self): View Source

    326    def check_capacity(self):
    327        """
    328        Helper method to adjust the capacity of the deque based on its count:
    329        if count >= capacity, grow the deque
    330        if count <= 1/4 of capacity, shrink the deque
    331        """
    332        if self.count >= self.capacity():
    333            self.grow()
    334        elif self.count * 4 <= self.capacity():
    335            self.shrink()

Helper method to adjust the capacity of the deque based on its count: if count >= capacity, grow the deque if count <= 1/4 of capacity, shrink the deque

def append_left(self, element): View Source

    337    def append_left(self, element):
    338        """
    339        Append an element to the left, adjusting capacity if needed.
    340
    341        Args:
    342            element: The element to append.
    343         """
    344        self.check_capacity()
    345        super().append_left(element)

Append an element to the left, adjusting capacity if needed.

Args: element: The element to append.

def append_right(self, element): View Source

    347    def append_right(self, element):
    348        """
    349        Append an element to the right, adjusting capacity if needed.
    350
    351        Args:
    352            element: The element to append.
    353        """
    354        self.check_capacity()
    355        super().append_right(element)

Append an element to the right, adjusting capacity if needed.

Args: element: The element to append.

def pop_left(self): View Source

    357    def pop_left(self):
    358        """
    359        Remove and return the element from the left, adjusting capacity if needed.
    360
    361        Returns:
    362            The leftmost element.
    363        """
    364        self.check_capacity()
    365        return super().pop_left()

Remove and return the element from the left, adjusting capacity if needed.

Returns: The leftmost element.

def pop_right(self): View Source

    368    def pop_right(self):
    369        """
    370        Remove and return the element from the right, adjusting capacity if needed.
    371
    372        Returns:
    373            The rightmost element.
    374        """
    375        self.check_capacity()
    376        return super().pop_right()

Remove and return the element from the right, adjusting capacity if needed.

Returns: The rightmost element.

##### Inherited Members

Deque
  Deque
  count
  push_front
  push_back
  pop_front
  pop_back
  front
  back
  peek_left
  peek_right
  is_empty
  from_list
  to_list
  capacity