<repo_name>ramponnamanda/coding-challenges<file_sep>/io/pyprojects/all/StackMax.py

if __name__ == '__main__':
    N = input()
    stack = [[0,0]]
    for i in range(int(N)):
        inputstring  = input()
        tokens = inputstring.split(" ")
        query = tokens[0]
        if query == "1":
            item = int(tokens[1])
            maxval = max(item, stack[-1][1])
            stack.append([item, max(item, maxval)])
        elif query == "2":
            stack.pop()
        elif query == "3":
            print(stack[-1][1])<file_sep>/io/pyprojects/all/BinarySearchTree.py
class Node:

    def __init__(self, key):
        self.data = key
        self.right = None
        self.left = None


def insert(root, key):
    if root is None :
        return Node(key)

    if root.data < key:
        root.right = insert(root.right, key)
    elif root.data > key:
        root.left = insert(root.left, key)

    return root


def insertwithoutrec(root, key):
    if root is None:
        return Node(key)

    temp = root
    while temp:
        if temp.data > key:
            if temp.left :
                temp = temp.left
            else:
                temp.left = Node(key)
                break

        elif temp.data < key:
            if temp.right:
                temp = temp.right
            else:
                temp.right = Node(key)
                break
        else:
            break;
    return root


def inorderwithoutrecursion(root):
    stack = []
    stack.push(root)
    while stack:



def inorder(root):
    if root:
        inorder(root.left)
        print(root.data)
        inorder(root.right)


def preorder(root):
    if root:
        print(root.data)
        preorder(root.left)
        preorder(root.right)


def postorder(root):
    if root:
        postorder(root.left)
        postorder(root.right)
        print(root.data)


def delete(root, key):
    if root is None:
        return root

    if root.data < key:
        root.right = delete(root.right, key)
    elif root.data > key:
        root.left = delete(root.left, key)
    else:
        if root.left is None:
            return root.right
        if root.right is None:
            return root.left

        successor = inorderSuccessor(root)
        root.right = delete(root.right, successor.data)
        successor.right = root.right
        successor.left = root.left
        return successor
    return root


def inorderSuccessor(root):
    temp = root.right
    while temp.left is not None:
        temp = temp.left
    return temp


if __name__ == '__main__':
    data = [50, 25, 75, 15, 35, 65, 85, 10, 20, 30, 40, 60, 70, 80, 90]
    rootNode = None
    for key in data:
        rootNode = insertwithoutrec(rootNode, key)

    inorder(rootNode)

    delete(rootNode, 70)
    delete(rootNode, 75)
    print("updated tree")

    inorder(rootNode)
<file_sep>/README.md
# coding-challenges
Coding challenges from Hackerrank, GeeksForGeeks etc
<file_sep>/io/pyprojects/all/TextEdit.py
if __name__ == '__main__':
    N = int(input())
    stack = []
    text = ''
    for i in range(N):
        l = list(input().split())
        query = int(l[0])
        if query == 1:
            stack.append(len(l[1]))
            text += l[1]
        if query == 2:
            length = int(l[1])
            stack.append(text[len(text) - length:])
            text = text[:len(text) - length]
        if query == 3:
            print(text[int(l[1]) - 1])
        if query == 4:
            item = stack.pop()
            if type(item) is int:
                text = text[:len(text) - item]
            else:
                text += item<file_sep>/io/pyprojects/all/BST.py
class Node:

    def __init__(self, key):
        self.data = key
        self.right = None
        self.left = None


def insert(root, key):
    if root is None :
        return Node(key)

    if root.data < key:
        root.right = insert(root.right, key)
    elif root.data > key:
        root.left = insert(root.left, key)

    return root


class BinarySearchTree:
    @staticmethod
    def init():
        sampledata = [50, 25, 75, 15, 35, 65, 85, 10, 20, 30, 40, 60, 70, 80, 90]
        rootnode = None
        for item in sampledata:
            rootnode = insert(rootnode, item)
        return rootnode


if __name__ == '__main__':
    data = [50, 25, 75, 15, 35, 65, 85, 10, 20, 30, 40, 60, 70, 80, 90]
    rootNode = None
    for key in data:
        rootNode = insert(rootNode, key)

<file_sep>/io/pyprojects/all/BSTTopView.py
import queue

class Node:

    def __init__(self, key):
        self.data = key
        self.right = None
        self.left = None


def insert(root, key):
    if root is None :
        return Node(key)

    if root.data < key:
        root.right = insert(root.right, key)
    elif root.data > key:
        root.left = insert(root.left, key)

    return root


def topview(root):
    if root is None:
        return

    q = queue.Queue()
    q.put([root, 0])

    hdmap = {}

    while not q.empty():
        qobj = q.get()
        obj = qobj[0]
        dis = qobj[1]
        if hdmap.get(dis) is None:
            print(obj.data)
            hdmap[dis] = obj
        if obj.left:
            q.put([obj.left, dis - 1])
        if obj.right:
            q.put([obj.right, dis + 1])


if __name__ == '__main__':
    data = [50, 25, 75, 15, 35, 65, 85, 10, 20, 30, 40, 60, 70, 80, 90]
    rootNode = None
    for key in data:
        rootNode = insert(rootNode, key)
    topview(rootNode)<file_sep>/io/pyprojects/all/TextEditor.py
class AppendOperation:
    def __init__(self, txt, text : list):
        self.txt = txt
        self.text = text

    def evaluate(self):
        # for ch in self.txt:
        #     self.text.append(ch)
        # self.text + list(self.txt)
        return self.text + list(self.txt)

    def undo(self):
        return self.text[:len(self.text) - len(self.txt)]


class RemoveOperation:
    def __init__(self, length, text : list):
        self.length = length
        self.text = text
        self.backUp = []

    def evaluate(self):
        textlen = len(self.text)
        self.backUp = self.text[textlen - self.length:]
        return self.text[:textlen - self.length]

    def undo(self):
        return self.text + self.backUp


class PrintOperation:
    def __init__(self, index, text):
        self.index = index
        self.text = text

    def evaluate(self):
        print(self.text[self.index - 1])

    def undo(self):
        return self.text


if __name__ == '__main__':
    N = input()
    info = []
    stack = []
    for i in range(int(N)):
        inputstring  = input()
        tokens = inputstring.split(" ")
        query = tokens[0]
        if query == "1":
            op = AppendOperation(tokens[1], info)
            stack.append(op)
            info = op.evaluate()
        elif query == "2":
            op = RemoveOperation(int(tokens[1]),info)
            stack.append(op)
            info = op.evaluate()
        elif query == "3":
            op = PrintOperation(int(tokens[1]),info)
            op.evaluate()
        elif query == "4":
            op = stack.pop()
            info = op.undo()
<file_sep>/io/pyprojects/all/BSTtraversals.py
from io.pyprojects.all.BST import BinarySearchTree

if __name__ == '__main__':
    node = BinarySearchTree.init()
    print(node)


def inorderwithoutrecursion(root):
    if root is None:
        return
    stack = []

    while True:
        print(root.data)
        stack.add(root)
        while root.right != None:
            stack.add(root.right)
        # if len(stack)


def checkBST(root):
    result = True
    if root:
        if root.left:
            result &= (root.left.data < root.data) & checkBST(root.left)
        if root.right:
            result &= (root.right.data > root.data) & checkBST(root.right)
    return result
