Exemple #1
0
def is_matched_html(raw):
    """Return True if all HTML tags are properly matched;
    False Otherwise."""
    S = ArrayStack()
    # find first '<' character
    start = raw.find('<')
    while start != -1:
        # find next '>' character
        end = raw.find('>', start + 1)
        if end == -1:
            return False
        # strip away < >
        tag = raw[start + 1:end]
        # this is opening tag
        if not tag.startswith('/'):
            S.push(tag)
        # this is closing tag
        else:
            if S.is_empty():
                return False
            if tag[1:] != S.pop():
                return False
        start = raw.find('<', end + 1)
    # all tags matched
    return S.is_empty()
Exemple #2
0
def is_match(expr):
    """Return True if all the delimiters are properly matched; False Otherwise;"""
    lefty = '({['
    righty = ')}]'
    stack = ArrayStack()
    for c in expression:
        if c in lefty:
            stack.push(c)
        elif c in righty:
            if stack.is_empty():
                return False
            if righty.index(c) != lefty.index(stack.pop()):
                return False
    return stack.is_empty()
def is_matched(expr):
    """Return True if  all delimiters properly match (or closed); False otherwise"""
    left_delimiters = "({["
    right_delimiter = ")}]"
    S = ArrayStack()
    for c in expr:
        if c in left_delimiters:
            S.push(c)
        elif c in right_delimiter:
            if S.is_empty():
                return False
            if right_delimiter.index(c) != left_delimiters.index(S.pop()):
                return False
    return S.is_empty()
Exemple #4
0
def is_matched(expression):
    """Return True if all delimiters are properly match.
     False otherwise"""
    lefty = '({['
    righty = ')}]'
    S = ArrayStack()
    for char in expression:
        if char in lefty:
            S.push(char)
        elif char in righty:
            if S.is_empty():
                return False
            if righty.index(char) != lefty.index(S.pop()):
                return False
    return S.is_empty()
def is_matched(expr):
    """Return True if all delimiters are properly matched"""
    
    lefty ='({['                    # opening delimiters
    righty = ')}]'                  # closing delimiters
    s = ArrayStack()
    for c in expr:
        if c in lefty:
            s.push(c)               # push delimiter onto the stack
        elif c in righty:
            if s.is_empty():
                return False        # nothing to match with
            if righty.index(c) != lefty.index(s.pop()):
                return False        # mismatched
    return s.is_empty()             # were all symbols matched?
Exemple #6
0
def is_match(raw):
    stack = 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('/'):
            stack.push(tag)
        else:
            if stack.is_empty():
                return False
            if tag[1:] != stack.pop():
                return False
        j = raw.find('<', k + 1)
    return stack.is_empty()
Exemple #7
0
def test_stack():
    S = ArrayStack()
    S.push(5)
    print(S.top())
    S.push(6)
    print(len(S))
    print(S.pop())
    print(S.is_empty())
    print(S.data)
Exemple #8
0
def is_matched(expr):
    """
    Return True if all delimiters are properly mathc;
    False otherwise.
    """
    lefty = '({['   # opening delimiters
    righty = ')}]'  # respective closing delims

    S = ArrayStack()
    for c in expr:
        if c in lefty:
            S.push(c)   # push left delimiter on stack
        elif c in righty:
            if S.is_empty():
                return False        # nothing to match with
            if righty.index(c) != lefty.index(S.pop()):
                return False        # mismatched
    return S.is_empty()             # were all symbols matched?
Exemple #9
0
def is_matched_html(raw):
    """
    False otherwise.
    """

    S = ArrayStack()
    j = raw.find('<')  # find first '<' character (if any)
    while j != -1:
        k = raw.find('>', j + 1)  # find next '>' character
        if k == -1:
            return False  # invalid tag
        tag = raw[j + 1:k]  # strip away < >
        if not tag.startswith('/'):  # this is opening tag
            S.push(tag)
        else:
            if S.is_empty():
                return False  # nothing to match with
            if tag[1:] != S.pop():
                return False  # mismatched delimiter
        j = raw.find('<', k + 1)  # find next '<' character (if any)
    return S.is_empty()  # were all opening tags mathced?
def reverse_file(filename):
    """Overwrite a given file with contents line-by-line in reversed order"""
    S = ArrayStack()
    original = open(filename)

    for line in original:
        S.push(line.rstrip('\n'))  # we will re-insert newlines while writing
    original.close()

    # now write the contents in LIFO order
    output = open(filename, 'w')
    while not S.is_empty():
        output.write(S.pop() + '\n')  # add trailing new line
    output.close()
def is_matched_html(raw_html: str):
    """Returns True if all HTML tags are properly matched; False otherwise"""
    S = ArrayStack()
    j = raw_html.find("<")  # find first '<' char (if any)
    while j != -1:  # find() returns -1 if it fails
        k = raw_html.find(">", j + 1)  # find next '>' char if any
        if k == -1:
            return False
        tag = raw_html[j + 1:k]  # remove '<>'

        if not tag.startswith('/'):  # opening tag
            S.push(tag)
        else:  # closing tag
            if S.is_empty():
                return False  # nothing to match with
            if tag[1:] != S.pop(
            ):  # remove '/' from closing tag and match with opening tag stored in stack
                return False

        j = raw_html.find('<', k +
                          1)  # find the next '<' char (if any) for next tag

    return S.is_empty(
    )  # were all tags were matched, then stack should be empty?
Exemple #12
0
def reverse_file(filename):
    """
    Overwrite given file with its contents line-by-line reversed.
    """
    S = ArrayStack()
    original = open(filename)
    for line in original:
        S.push(line.rstrip('\n'))  # we will re-insert newlines when writing
    original.close()

    # now we over write with contents in LIFO order
    output = open(filename, 'w')  # reopening file overwrites orignal
    while not S.is_empty():
        output.write(S.pop() + '\n')  # re-insert newline characters
    output.close()
Exemple #13
0
from arraystack import ArrayStack

# sample usage
if __name__ == '__main__':
    mystack = ArrayStack()
    print(mystack.is_empty())
    mystack.push('hello')
    mystack.push(7)
    print(mystack.__len__())
    print(mystack.pop())