ofnodes Docs
This site contains the project documentation for the
ofnodes project. The ofnodes project consists of data structures
and algoritms written in python>=3.11.5. The author uses node objects,
structure objects, and algorithms to illustrate the following programming
concepts:
- Packaging
- Version Control
- Automated CI/CD
- API
- TDD
- OOP: modular and maintainable code
Table Of Contents
The documentation follows the best practice for project documentation as described by Daniele Procida in the Diátaxis documentation framework and consists of four separate parts:
Quickly find what you're looking for depending on your use case by looking at the different pages.
Project Overview
RandomAccessArray
Bases: BubbleSortMixin, InsertionSortMixin, ReverseOrderMixin
An array supporting random access with bubble sort and order reversal capabilities.
This class represents an array that supports random access operations and also provides functionality for sorting elements using bubble sort algorithm and reversing the order of elements.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
size |
int
|
The size of the array. |
required |
Attributes:
| Name | Type | Description |
|---|---|---|
_data |
list
|
The underlying list to store array elements. |
Note
This class inherits from BubbleSortMixin and ReverseOrderMixin to leverage the bubble sort and order reversal functionalities.
Examples:
>>> raarray = RandomAccessArray(5)
>>> raarray
RandomAccessArray([None, None, None, None, None])
>>> raarray[0] = 8
>>> raarray
RandomAccessArray([8, None, None, None, None])
>>> [raarray.__setitem__(i+1, val) for i, val in enumerate([2, 6, 4, 5])]
[None, None, None, None]
>>> raarray
RandomAccessArray([8, 2, 6, 4, 5])
>>> str(raarray)
'[8, 2, 6, 4, 5]'
Source code in src/ofnodes/structures/randomaccessarray.py
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 | |
__repr__()
Returns a string representation of the array.
Returns:
| Name | Type | Description |
|---|---|---|
str |
A string representation of the array. |
Example
raarray = RandomAccessArray(5) [raarray.setitem(i, val) for i, val in enumerate([8, 2, 6, 4, 5])] raarray RandomAccessArray([8, 2, 6, 4, 5])
Source code in src/ofnodes/structures/randomaccessarray.py
54 55 56 57 58 59 60 61 62 63 64 65 66 67 | |
__str__()
Returns a string representation of the array.
Returns:
| Name | Type | Description |
|---|---|---|
str |
A string representation of the array. |
Example
raarray = RandomAccessArray(5) [raarray.setitem(i, val) for i, val in enumerate([8, 2, 6, 4, 5])] str(raarray) '[8, 2, 6, 4, 5]'
Source code in src/ofnodes/structures/randomaccessarray.py
69 70 71 72 73 74 75 76 77 78 79 80 81 | |
bubble_sort(ascending=True)
Sorts the elements of the data structure using bubble sort.
This method sorts the elements of the data structure in place using the
bubble sort algorithm. It supports both ascending and descending order
based on the ascending parameter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ascending |
bool
|
If True, sorts the elements in ascending order. If False, sorts the elements in descending order. Defaults to True. |
True
|
Examples: >>> raarray = RandomAccessArray(5) >>> [raarray.setitem(i, val) for i, val in enumerate([8, 2, 6, 4, 5])] [None, None, None, None, None] >>> raarray.bubble_sort() >>> raarray RandomAccessArray([2, 4, 5, 6, 8]) >>> raarray.bubble_sort(ascending=False) >>> raarray RandomAccessArray([8, 6, 5, 4, 2])
Source code in src/ofnodes/structures/randomaccessarray.py
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | |
insertion_sort(ascending=True, key=None)
Sorts the elements of the RandomAccessArray using the insertion sort algorithm.
This method sorts the elements in place either in ascending or descending order based on the specified parameters. An optional key function can be provided to customize the comparison behavior.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ascending |
bool
|
Determines the sort order. Defaults to True for ascending order. Set to False for descending order. |
True
|
key |
Callable
|
A function that serves as a key for the sort comparison. Defaults to None. |
None
|
Examples:
>>> # Custom comparison function
>>> def by_length(s):
... return len(s)
>>> strings = ["strawberry", "peach", "cherry", "date",]
>>> raarray = RandomAccessArray(len(strings))
>>> for i, val in enumerate(strings):
... raarray[i] = val
>>> raarray
RandomAccessArray(['strawberry', 'peach', 'cherry', 'date'])
>>> raarray.insertion_sort()
>>> raarray
RandomAccessArray(['cherry', 'date', 'peach', 'strawberry'])
>>> raarray.insertion_sort(key=by_length)
>>> raarray
RandomAccessArray(['date', 'peach', 'cherry', 'strawberry'])
>>> raarray.insertion_sort(ascending=False, key=by_length)
>>> raarray
RandomAccessArray(['strawberry', 'cherry', 'peach', 'date'])
Returns: None
Source code in src/ofnodes/structures/randomaccessarray.py
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 | |
reverse_order()
Examples:
>>> raarray = RandomAccessArray(5)
>>> [raarray.__setitem__(i, val) for i, val in enumerate([8, 2, 6, 4, 5])]
[None, None, None, None, None]
>>> raarray.reverse_order()
>>> raarray
RandomAccessArray([5, 4, 6, 2, 8])
Source code in src/ofnodes/structures/randomaccessarray.py
106 107 108 109 110 111 112 113 114 115 | |
SinglyLinkedList
Bases: CycleDetectionMixin, InsertionSortMixin, SearchMixin, RemoveMixin, InsertHeadMixin, InsertTailMixin, InsertAfterTargetMixin, InsertBeforeTargetMixin, PrintMixin, BubbleSortMixin, ReverseOrderMixin
A class representing a singly linked list.
This class provides functionality to create and manipulate a singly linked list data structure. Each node in the linked list contains a reference to the next node in the sequence.
Attributes:
| Name | Type | Description |
|---|---|---|
_head |
Optional[SinglyNode]
|
The head of the linked list. |
_tail |
Optional[SinglyNode]
|
The tail of the linked list. |
_target |
Optional[Any]
|
The target data or node instance. |
Examples:
>>> linked_list = SinglyLinkedList()
>>> linked_list
SinglyLinkedList(head=None, tail=None, target=None)
Source code in src/ofnodes/structures/singlylinkedlist.py
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 | |
bubble_sort(ascending=True, key=None)
Sorts the nodes of the singly linked data structure.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ascending |
bool
|
Specifies whether to sort the elements in ascending order (default) or descending order. |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
None |
This method modifies the original linked list in place. |
Examples:
>>> sllist = SinglyLinkedList([8, 2, 6, 4, 5])
>>> sllist.bubble_sort()
>>> sllist
SinglyLinkedList([2, 4, 5, 6, 8])
>>> sllist.bubble_sort(ascending=False)
>>> sllist
SinglyLinkedList([8, 6, 5, 4, 2])
Notes
- The comparison operation (
>) is used for elements. For non-numeric data types, ensure that the__gt__method is defined appropriately for comparison. - Time Complexity:
- Best Case: O(n), when the list is already sorted.
- Worst Case: O(n^2), when the list is in reverse order.
- Average Case: O(n^2).
Source code in src/ofnodes/structures/singlylinkedlist.py
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | |
cycle_detection()
Detects if the singly linked list instance contains a cycle.
This method checks whether the linked list contains a cycle using Floyd's Tortoise and Hare algorithm.
Returns:
| Name | Type | Description |
|---|---|---|
bool |
True if a cycle is detected, False otherwise. |
Examples:
>>> sllist = SinglyLinkedList([8, 2, 6, 4, 5])
>>> sllist.cycle_detection()
False
>>> setattr(sllist.head.next, '_next', sllist.head.next)
>>> sllist.cycle_detection()
True
Source code in src/ofnodes/structures/singlylinkedlist.py
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 | |
insertion_sort(ascending=True, key=None)
Sorts the nodes of a reference-based object using insertion sort.
This method sorts a singly linked list using the insertion sort algorithm. It traverses the list, starting from the second element, and divides it into sorted and unsorted portions. Each element in the unsorted portion is iteratively placed in the correct position within the sorted portion.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ascending |
bool
|
Determines the sort order. Defaults to True. If True, sorts in ascending order; if False, sorts in descending order. |
True
|
Raises:
| Type | Description |
|---|---|
TypeError
|
If the method is used on data structures that do not support reference-based operations. |
ValueError
|
If the singly linked list is empty or contains only one node. |
Returns:
| Type | Description |
|---|---|
|
None |
Example
def by_length(s): ... return len(s)
fruits = ['cherry', 'strawberry', 'fig', 'peach'] sllist = SinglyLinkedList(fruits) sllist.insertion_sort(ascending=False, key=by_length) sllist SinglyLinkedList(['strawberry', 'cherry', 'peach', 'fig']
Source code in src/ofnodes/structures/singlylinkedlist.py
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | |
reverse_order()
Reverses the order of elements in the singly linked data structure.
Returns:
| Name | Type | Description |
|---|---|---|
None |
This method modifies the original linked list in place. |
Examples:
>>> sllist = SinglyLinkedList([8, 2, 6, 4, 5])
>>> sllist.reverse_order()
>>> sllist
SinglyLinkedList([5, 4, 6, 2, 8])
Notes
- Time Complexity: O(n), where n is the number of elements in the linked list.
Source code in src/ofnodes/structures/singlylinkedlist.py
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 | |
SinglyNode
Bases: AddMixin
Represents a node in a singly linked list.
Attributes:
| Name | Type | Description |
|---|---|---|
data |
The data stored in the node. |
|
next |
Reference to the next node in the linked list. Defaults to None. |
Source code in src/ofnodes/nodes/singlynode.py
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | |
Stack
Bases: RemoveMixin, PrintMixin
Support for a reference-based LIFO object.
The head is considered the last node. The head is popped and a node pushed onto the stack becomes the new head.
Source code in src/ofnodes/structures/stack.py
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | |
Acknowledgements
I want to thank my house plants for providing me with a negligible amount of oxygen each day. Also, I want to thank the sun for providing more than half of their nourishment free of charge.
Special thanks to OpenAI's ChatGPT for valuable insights and assistance.