A featureful, polymorphic Python3 stack datatype (for educational purposes) offering:
-
creation with any number of items:
s1 = Stack(0, 1, 2); s2 = Stack()
-
access to top item:
s1.top()
returns2
;s2.top()
returnsNone
-
emptiness and boolean truthiness testing:
s1.is_empty()
returnsFalse
;s2.is_empty()
returnsTrue
s1 and not s2
returnsTrue
-
pushing single items:
s2.push(2)
returnsStack[2]
-
concatenation with '+' adds items of s2, from bottom to top, to s1:
s1 + s2
returnss1 = Stack(0, 1, 2, '3')
-
duplication and extending with '*':
s1 * 2
returnss1 = Stack[0, 1, 2, '3', 0, 1, 2, '3']
-
iteration:
for item in s1
(iterates from top to bottom of Stack) -
containment for finding if an item is in the list:
if 2 in s1: print("found it")
-
extending by any number of items:
s2.extend(3, 4)
returnsStack[2, 3, 4]
s3.extend(4, 5)
returnsStack['3', 4, 5]
-
counting item occurrences:
s2.count(3)
returns1
-
conversion to a standard Python list of items:
from bottom to top:s2.items()
returns[2, 3, 4]
from top to bottom:s2.list()
returns[4, 3, 2]
-
a full set of self-tests:
Stack.test()
.