Пример #1
0
def reverse_stack__Iterative__Auxilary_space(stack) -> StackADT:
    """
    This is not Inplace
    """
    reversed_stack = StackADT()
    while not stack.is_empty():
        reversed_stack.push(stack.pop())

    return reversed_stack
Пример #2
0
def convert_lombok_implementation(obj_str: str):
    stack = StackADT()
    # root = None  # root, can either be an Object, List or a Map
    root = None  # root, can either be an Object, List or a Map
    char_list = list(obj_str)
    left_end = 0
    right_end = len(char_list) - 1
    for idx, character in enumerate(char_list):
        if character in ['(', '{', '[']:
            # if (, then we are working with an Object
            if character == '(':
                stack.push(character)
                root = {}  # Empty dict
                object_type = "".join(
                    char_list[left_end:idx])  # excluding the comma
                root["Object Type"] = object_type

                # Reset Earlier Character and Update the Left pointer
                reset_character(char_list, left_end, idx + 1,
                                None)  # including the '('
                left_end = idx + 1  # update the Left-End, excluding the '('

            # if {, then we are working with a Map
            elif character == '{':
                stack.push(character)

            # if [, then we are working with a List
            elif character == '[':
                stack.push(character)

        # if `,`
        elif character == ',':
            # Take action
            key_value_string = "".join(
                char_list[left_end:idx])  # excluding the comma
            key_value_string_stripped = key_value_string.strip()
            kev_value_pair = key_value_string_stripped.split("=")
            print(kev_value_pair)

            # Pushing key value pair in dictionary
            key = kev_value_pair[0]
            value = kev_value_pair[1]
            root[key] = value

            # Reset Earlier Character and Update the Left pointer
            reset_character(char_list, left_end, idx + 1,
                            None)  # including the ','
            left_end = idx + 1  # update the Left-End, excluding the ','

    indented_obj_str = "null"
Пример #3
0
def reverse_stack__Recursive__Auxilary_space(stack) -> None:
    """
        This is Inplace
    """

    # Base Case
    if stack.size() == 0 or stack.size() == 1:  # Already Reversed
        return

    # Hypothesis Step
    top_element = stack.pop()
    reverse_stack__Recursive__Auxilary_space(stack)  # Stack has been reversed

    # Induction Step
    another_stack = StackADT()
    while not stack.is_empty():
        another_stack.push(stack.pop())

    stack.push(top_element)

    while not another_stack.is_empty():
        stack.push(another_stack.pop())  # pushing back into the original stack
Пример #4
0
 def __init__(self):
     self.main_stack = StackADT()
     self.min_element = None
Пример #5
0
class MinStackADT:
    def __init__(self):
        self.main_stack = StackADT()
        self.min_element = None

    def push(self, element):
        if self.main_stack.is_empty():
            self.main_stack.push(element)
            self.min_element = element
        else:
            # Stack is not empty
            if element >= self.min_element:
                self.main_stack.push(element)
            else:
                value = 2 * element - self.min_element  # Notice : Formula
                self.main_stack.push(value)
                self.min_element = element

    def pop(self):
        if self.main_stack.is_empty():
            return None
        else:
            element = self.main_stack.pop()
            if element >= self.min_element:
                # The minimum element of the stack, is still the min_element
                return element
            else:
                self.min_element = 2 * self.min_element - element  # Notice : Formula
                return self.min_element  # the correct value of stack, and the correct minimum

    def size(self):
        return self.main_stack.size()

    def peek(self):
        # Basically similar to pop, just without the actual pop
        if self.main_stack.is_empty():
            return None
        else:
            element = self.main_stack.peek(
            )  # Note : Only change from Pop() method
            if element >= self.min_element:
                # The minimum element of the stack, is still the min_element
                return element
            else:
                # Notice : we are not updating the min_element here, as this operation is peek() and not pop()
                return 2 * self.min_element - element  # Notice : Formula

    def min(self):
        return self.min_element
Пример #6
0
 def __init__(self):
     self.main_stack = StackADT()
     self.supporting_stack = StackADT()  # To maintain the corresponding minimum values
Пример #7
0
class MinStackADT:
    def __init__(self):
        self.main_stack = StackADT()
        self.supporting_stack = StackADT()  # To maintain the corresponding minimum values

    def push(self, element):
        # Push into both the main_stack and the supporting_stacl
        self.main_stack.push(element)

        if self.supporting_stack.is_empty():
            self.supporting_stack.push(element)
        else:
            # supporting stack is not empty, find the minimum and push it to the supporting_stack
            minimum = min(element, self.supporting_stack.peek())
            self.supporting_stack.push(minimum)

    def pop(self):
        if self.main_stack.is_empty():
            return None
        else:
            # Pop from both the main_stack and the supporting_stacl
            self.supporting_stack.pop()
            return self.main_stack.pop()

    def size(self):
        # Size of both the stack will be similar here
        return self.main_stack.size()

    def peek(self):
        if self.main_stack.is_empty():
            return None
        else:
            return self.main_stack.peek()

    def min(self):
        # Will give minimum using the supporting stack
        if self.supporting_stack.is_empty():
            return None
        else:
            return self.supporting_stack.peek()
Пример #8
0
    if stack.size() == 0 or stack.peek() <= element:
        stack.push(element)
        return

    top_element = stack.pop()
    insert_into_sorted_stack(stack, element)  # Hypothesis
    stack.push(top_element)

    # No induction Step


def sort_stack(stack) -> None:
    # Edge case
    if stack is None:
        raise ValueError("Invalid Stack")

    # Base case
    if stack.size() == 0 or stack.size() == 1:  # Already sorted
        return

    top_element = stack.pop()
    sort_stack(stack)  # stack has been reduced
    insert_into_sorted_stack(stack, top_element)


if __name__ == "__main__":
    s = StackADT([5, 1, 0, 2])
    s.print()  # unsorted stack
    sort_stack(s)
    s.print()  # sorted stack
Пример #9
0
def nearest_greater_element_index_to_left(nums):
    length = len(nums)
    result = [-1] * len(nums)
    stack = StackADT()

    for i in range(0, length):
        curr = nums[i]

        if stack.size() == 0:
            result[i] = -1

        elif stack.size() != 0 and stack.peek().get_value() > curr:  # comparing value Notice
            result[i] = stack.peek().get_index()  # putting index Notice

        elif stack.size() != 0 and stack.peek().get_value() <= curr:  # comparing value Notice
            while stack.size() != 0 and stack.peek().get_value() <= curr:  # comparing value Notice
                stack.pop()

            if stack.size() == 0:
                result[i] = -1
            else:
                result[i] = stack.peek().get_index()  # putting index Notice

        # Processed Curr
        stack.push(Pair(curr, i))  # Pair(value, index) Notice

    print(nums, "<- original")
    print(result, "<- ngl index")
    return result
Пример #10
0
    The code is same, here in the induction step we are calling another recursive method for inserting into bottom

    This is also Inplace
    """

    # Base Case
    if stack.size() == 0 or stack.size() == 1:  # Already Reversed
        return

    # Hypothesis Step
    top_element = stack.pop()
    reverse_stack__Recursive__Auxilary_space(stack)  # Stack has been reversed

    # Induction Step
    insert_into_bottom(stack, top_element)


if __name__ == "__main__":
    # s = StackADT([2, 4, 6, 0, 1])
    s = StackADT([9, 1, 2, 8, 4, 3])
    s.print()

    reverse_stack__Recursive__Constant_Aux_Space(s)
    s.print()

    reverse_stack__Recursive__Auxilary_space(s)
    s.print()

    reversed_stack = reverse_stack__Iterative__Auxilary_space(s)
    reversed_stack.print()
Пример #11
0
from Abstract_Data_Type.StackADT import StackADT


# Here we treat k as 1 indexed
def remove_kth_element_from_top_of_stack(stack, kth):
    if stack is None or kth <= 0:
        raise ValueError("Invalid Parameters")

    if kth == 1:
        return stack.pop()

    top_element = stack.pop()
    removed_element = remove_kth_element_from_top_of_stack(stack, kth - 1)
    stack.push(top_element)
    return removed_element


if __name__ == "__main__":
    # s = StackADT([2, 4, 6, 0, 1])
    s = StackADT([9, 1, 2, 8, 4, 3])
    k = 2

    s.print()  # Not Removed
    popped_element = remove_kth_element_from_top_of_stack(s, k)
    print(popped_element, " < Removed")
    s.print()  # Kth element Removed