<jupyter_start><jupyter_text>Stacks<jupyter_code>class ArrayStack:
    """LIFO Stack implementation using a Python list as underlying storage"""
    
    def __init__(self):
        """Create an empty stack"""
        self._data = []
    
    def __len__(self):
        """Return number of elements in the stack"""
        return len(self._data)
    
    def is_empty(self):
        """Return True if the stack is empty"""
        return len(self._data) == 0
    
    def push(self, e):
        """Add element e to the top of the stack"""
        self._data.append(e)
    
    def top(self):
        """Return (but do not remove) the element at the top of the stack"""
        if self.is_empty():
            raise Empty('Stack is empty')
        return self._data[-1]
    
    def pop(self):
        """Remove and return the element from the top of the stack"""
        if self.is_empty():
            raise Empty('Stack is empty')
        return self._data.pop()<jupyter_output><empty_output><jupyter_text>Reversing data using a stack<jupyter_code>def reverse_file(filename):
    s = ArrayStack()
    original = open(filename)
    
    for line in original:
        s.push(line.rstrip('\n'))
    
    original.close()
    
    
    while not s.is_empty():
        print(s.pop())
reverse_file('test.txt')<jupyter_output>6
5
4
3
2
1<jupyter_text>Matching parentheses and HTML tags<jupyter_code>def is_matched(expr):
    lefty = '({['
    righty = ')}]'
    
    s = ArrayStack()
    
    for c in expr:
        if c in lefty:
            s.push(c)
        elif c in righty:
            if s.is_empty():
                return False
            if righty.index(c) != lefty.index(s.pop()):
                return False
    return s.is_empty()
expr = '({[({})]})'
print(is_matched(expr))
def is_matched_html(raw):
    """Return True if all HTML tags are properly matched; False otherwise"""
    s = ArrayStack()
    j = raw.find('<')
    
    while j != -1:
        k = raw.find('>', j+1)
        if k == -1:
            return False
        tag = raw[j+1:k]
        if not tag.startswith('/'):
            s.push(tag)
        else:
            if s.is_empty():
                return False
            if tag[1:] != s.pop():
                return False
        j = raw.find('<', k+1)
    return s.is_empty()
raw = """
<body>
<center>
<h1> The Little Boat </h1>
</center>
<p> The storm tossed the little
boat like a cheap sneaker in an
old washing machine. The three
drunken fishermen were used to
such treatment, of course, but
not the tree salesman, who even as
a stowaway now felt that he
had overpaid for the voyage. </p> 
<ol>
<li> Will the salesman die? </li>
<li> What color is the boat? </li>
<li> And what about Naomi? </li>
</body>
"""
is_matched_html(raw)<jupyter_output><empty_output><jupyter_text>Queues<jupyter_code>class ArrayQueue:
    """FIFO queue implementation using a Python list as underlying storage"""
    DEFAULT_CAPACITY = 10
    
    def __init__(self):
        self._data = [None] * ArrayQueue.DEFAULT_CAPACITY
        self._size = 0
        self._front = 0
    
    def __len__(self):
        """Return the number of elements in the queue"""
        return self._size
    
    def is_empty(self):
        """Return True if the queue is empty"""
        return self._size == 0
    
    def first(self):
        """Return the first element of the queue"""
        if self.is_empty():
            raise Exception('Queue is empty')
        return self._data[self._front]
    
    def dequeue(self):
        """Remove and return the first element of the queue"""
        if self.is_empty():
            raise Empty('Queue is empty')
        answer = self._data[self._front]
        self._data[self._front] = None
        self._front = (self._front + 1) % len(self._data)
        self._size -= 1
        return answer
    
    def enqueue(self, e):
        if self._size == len(self._data):
            self._resize(2 * len(self.data))
        avail = (self._front + self._size) % len(self._data)
        self._data[avail] = e
        self._size += 1
    
    def _resize(self, cap):
        old = self._data
        self._data = [None] * cap
        walk = self._front
        
        for k in range(self._size):
            self._data[k] = old[walk]
            walk = (1 + walk) % len(old)
        self._front = 0
        
a = ArrayQueue()
a.first()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12284287011807447, 'fraction_numerical': 0.006130790190735695, 'mean_word_length': 2.0890603085554, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '229409', 'license': 'MIT', 'n_tokens_mistral': 1397, 'n_tokens_neox': 1373, 'n_words': 419, 'path': 'data-structures-algorithms/6 - Stacks, Queues and Deques.ipynb', 'repo_name': 'joaoandre/100daysOfCode', 'size': '4404'}