dsa.array module

Module containing array classes.

class dsa.array.Array(contents=None, capacity=10)

Bases: object

A static array implementation.

Special Methods:

Index Operator: array[index] Assignment: array[index] = value

Equality:

Array instances can be compared for equality with other Array or DynamicArray instances (but not CircularArray), based on their contents.

append(element)

Append an element to the array. Raise an exception if capacity is exceeded.

Parameters:

element – The element to append.

Raises:

Exception – If the array is full.

capacity()

Get the total capacity of the array.

Return type:

int

Returns:

The capacity of the array.

count

number of elements currently in array

delete(index)

Delete an element at a specified index, shifting subsequent elements to the left.

Parameters:

index (int) – The index of the element to delete.

Raises:

IndexError – If index is out of bounds.

extend(array)

Append multiple elements from a given array.

Parameters:

array – An iterable containing elements to append.

Raises:

Exception – If appending the elements exceeds the array’s capacity.

classmethod from_list(mylist)

Create an array from a standard Python list.

Parameters:

mylist (list) – A Python list to initialize the array.

Returns:

An instance of the Array class.

insert(index, element)

Insert an element at a specified index, shifting existing elements to the right.

Parameters:
  • index (int) – The index at which to insert the element.

  • element – The element to insert.

Raises:

IndexError – If the index is out of bounds.

is_empty()

Check if the array is empty.

Return type:

bool

Returns:

True if the array is empty, False otherwise.

shift_left(start)

Helper method to shift elements to the left starting at a start index. (May delete an element but does not affect the count.)

Parameters:

start (int) – The starting index of the shift.

shift_right(start)

Helper method to shift elements to the right from a specified start index until the last element. (May delete an element but does not affect the count.) :type start: int :param start: The index at which to start shifting (inclusive). :type start: int

Raises:

Exception – If the array is full and cannot accommodate the shift.

to_list()

Convert the array’s elements to a standard Python list.

Return type:

list

Returns:

A list containing the elements of the array.

class dsa.array.CircularArray(contents=None, capacity=10)

Bases: Array

A circular array implementation.

Special Methods:

Index Operator:

array[index]

Assignment:

array[index] = value

append(element)

Append an element to the circular array. If appending exceeds capacity, it will wrap around to the oldest element.

Parameters:

element – The element to append.

delete(index)

Delete an element at a specified index, shifting subsequent elements to the left.

Parameters:

index (int) – The index of the element to delete.

Raises:

IndexError – If the index is out of bounds.

insert(index, element)

Insert an element at a specified index, shifting existing elements to the right.

Parameters:
  • index (int) – The index at which to insert the element.

  • element – The element to insert.

Raises:
  • IndexError – If the index is out of bounds.

  • Exception – If the array is full.

raw_view()

Return a raw view of the array.

Returns:

A raw view of the array.

to_list()

Convert the array’s elements to a standard Python list.

Returns:

A list containing the elements of the array.

class dsa.array.DynamicArray(contents=None, capacity=10)

Bases: Array

A dynamic array implementation. Capacity will adjust as needed.

Special Methods:

Index Operator: array[index] Assignment: array[index] = value

Equality:

DynamicArray instances can be compared for equality with other DynamicArray or Array instances (but not CircularArray), based on their contents.

append(element)

Append an element to the array. Adjust the capacity as needed.

Parameters:

element – The element to append.

check_capacity()

if count >= capacity, grow the array. if count <= 1/4 of capacity, shrink the array.

delete(index)

Delete an element at a specified index, shifting subsequent elements to the left. Adjust the capacity as needed.

Parameters:

index (int) – The index of the element to delete.

extend(array)

Append multiple elements from a given array. Adjust the capacity as needed.

Parameters:

array – An iterable containing elements to append.

grow()

Helper method to double the capacity of the current array.

insert(index, element)

Insert an element at a specified index, shifting existing elements to the right. Adjust the capacity as needed.

Parameters:
  • index (int) – The index at which to insert the element.

  • element – The element to insert.

shrink()

Helper method to halve the capacity of the current array.