예제 #1
0
def tree_by_name(filename, listname, x_start, x_stop, y_start, y_stop, fields):
    length = get_len(x_start, x_stop)
    res = [['\t' for i in range(length-3)] for i in range(len(fields))]
    wb = load_workbook(filename=filename)
    ws = wb[listname]

    x_tree = get_tree(parse_xl(filename, listname, get_name(x_start, 1), y_start, y_stop + 1), tab_depth)
    y_tree = get_tree(fields, tab_depth)
    aliases = [get_full(y_tree[i], y_tree) for i in range(len(y_tree))]
    
    j = 0
    for i in range(y_start, y_stop + 1):
        dd, ni = compare(get_full(x_tree[j], x_tree), aliases, 0)
        if dd:
            #print(get_full(x_tree[j], x_tree), "\n###\n", aliases[ni], "\n$$$")
            ind = y_tree[ni][5]
            if res[ind][0] != '\t':
                continue
            for k in range(3, length):
                if ws[get_name(x_start, k) + str(i)].value == None:
                    res[ind][k-3] = ''
                else:
                    res[ind][k-3] = ws[get_name(x_start, k) + str(i)].value
        j += 1
    return res
예제 #2
0
def tree_by_index(filename, listname, x_index, x_start, x_stop, y_start, y_stop, fields):
    length = get_len(x_start, x_stop)
    res = [['\t' for i in range(length)] for i in range(len(fields))]
    wb = load_workbook(filename=filename, data_only=True)
    ws = wb[listname]

    x_tree = get_tree(index_xl(filename, listname, x_index, get_name(x_index, 1), y_start, y_stop + 1), code_depth)
    y_tree = get_tree(fields, code_depth)
    aliases = [full_index(y_tree[i], y_tree) for i in range(len(y_tree))]

    x = 0
    for i in range(y_start, y_stop + 1):
        for j in range(len(fields)):
            if full_index(x_tree[x], x_tree) == aliases[j]:
                for k in range(length):
                    if ws[get_name(x_start, k) + str(i)].value == None:
                        res[j][k] = ''
                    else:
                        res[j][k] = ws[get_name(x_start, k) + str(i)].value
        x += 1
    return res

#print(by_name('test.xlsx', 'Лист1', 'B', 'AB', 4, 16, [str(x) + '.' + 'Stonks' for x in range(1, 15)]))
예제 #3
0
파일: 99.py 프로젝트: bondlee/leetcode
            if p:
                stack.append(p)
                p = p.left
            else:
                p = stack.pop()
                if not pre:
                    pre = p
                if pre.val > p.val:
                    if len(wrong) > 1:
                        wrong[1] = p
                    else:
                        wrong.append(pre)
                        wrong.append(p)
                pre = p
                p = p.right
        if len(wrong) > 1:

            wrong[0].val ^= wrong[1].val
            wrong[1].val ^= wrong[0].val
            wrong[0].val ^= wrong[1].val



if __name__ == '__main__':
    from tree import get_tree, preorderTraversIter
    node = get_tree([3, 1, 2])
    print preorderTraversIter(node)
    sol = Solution()
    sol.recoverTree(node)
    print preorderTraversIter(node)
    pass
예제 #4
0
파일: 129.py 프로젝트: bondlee/leetcode
#         self.left = None
#         self.right = None

class Solution(object):
    def sumNumbers(self, root):
        """
        :type root: TreeNode
        :rtype: int
        """
        if not root:
            return 0
        rlist = []
        def DFS(r, ls):
            if not r:
                return
            t = ls*10 + r.val
            if not r.left and not r.right:
                rlist.append(t)
                return
            if r.left:
                DFS(r.left, t)
            if r.right:
                DFS(r.right, t)
        DFS(root, 0)
        return sum(rlist)

if __name__ == '__main__':
    node = get_tree()
    sol = Solution()
    print sol.sumNumbers(node)
    pass
예제 #5
0

class Solution(object):
    def invertTree(self, root):
        """
        :type root: TreeNode
        :rtype: TreeNode
        """
        def invert(r):
            if not r:
                return []
            left = right = None
            if r.left:
                left = invert(r.left)
            if r.right:
                right = invert(r.right)
            r.right = left
            r.left = right
            return r

        rt = invert(root)
        return rt


if __name__ == '__main__':
    sol = Solution()
    node = get_tree()
    rt = sol.invertTree(node)
    print preorderTraversIter(rt)

    pass
예제 #6
0
def index():
    if 'username' in session:
        return render_template('index.html', name=escape(session['username']), magic_html=Markup(tree.get_tree()))
    return render_template('splash.html')
예제 #7
0
파일: 199.py 프로젝트: bondlee/leetcode
        from collections import deque,defaultdict
        queue = deque([])
        level_dic = defaultdict(lambda: 0)
        level = 0
        queue.appendleft(root)
        level_dic[level] = 1

        while len(queue):
            lcout = level_dic[level]
            while lcout:
                q = queue.pop()
                lcout -= 1
                if q.left:
                    queue.appendleft(q.left)
                    level_dic[level+1] += 1
                if q.right:
                    queue.appendleft(q.right)
                    level_dic[level+1] += 1
                if not lcout:
                    rlist.append(q.val)
            level += 1

        return rlist


if __name__ == '__main__':
    from tree import  get_tree
    node = get_tree([1, 2, 3])
    sol = Solution()
    print sol.rightSideView(node)
    pass
예제 #8
0
파일: 98.py 프로젝트: bondlee/leetcode

class Solution(object):
    def isValidBST(self, root):
        """
        :type root: TreeNode
        :rtype: bool
        """
        stack = []
        p = root
        pre = None
        while p or len(stack):
            if p:
                stack.append(p)
                p = p.left
            else:
                p = stack.pop()
                if pre is not None and pre >= p.val:
                    return False
                pre = p.val
                p = p.right
        return True


if __name__ == '__main__':
    from tree import get_tree
    node = get_tree([0, "null", -1])
    sol = Solution()
    print sol.isValidBST(node)

    pass
예제 #9
0
파일: 101.py 프로젝트: bondlee/leetcode
        while len(queue):
            level_count = tree_dic[level]
            ind = 0
            val_dic = defaultdict(lambda : 0)
            while level_count:
                q = queue.pop()
                level_count -= 1
                # 每个节点编号
                val_dic[q.num] = q.val
                if q.left:
                    queue.appendleft(q.left)
                    tree_dic[level+1] += 1
                    q.left.num = q.num*2
                if q.right:
                    queue.appendleft(q.right)
                    tree_dic[level + 1] += 1
                    q.right.num = q.num*2+1

            for i in val_dic:
                if val_dic[i] != val_dic[2**(level)-1-i]:
                    return False
            level += 1
            # 检查该层是否符合要求
        return True

if __name__ == '__main__':

    node = get_tree([2,3,3,4,5,5,4,"null","null",8,9,9,8])
    sol = Solution()
    print sol.isSymmetric(node)
    pass
예제 #10
0
        while p or len(stack):
            if p:
                stack.append(p)
                p = p.left
            else:
                p = stack.pop()
                if not pre:
                    pre = p
                if pre.val > p.val:
                    if len(wrong) > 1:
                        wrong[1] = p
                    else:
                        wrong.append(pre)
                        wrong.append(p)
                pre = p
                p = p.right
        if len(wrong) > 1:

            wrong[0].val ^= wrong[1].val
            wrong[1].val ^= wrong[0].val
            wrong[0].val ^= wrong[1].val


if __name__ == '__main__':
    from tree import get_tree, preorderTraversIter
    node = get_tree([3, 1, 2])
    print preorderTraversIter(node)
    sol = Solution()
    sol.recoverTree(node)
    print preorderTraversIter(node)
    pass
예제 #11
0
파일: 236.py 프로젝트: bondlee/leetcode
            if node.right:
                node.right.parent = node
                queue.appendleft(node.right)

        pp = s[-2]
        while pp:
            ps.append(pp)
            pp = pp.parent
        qq = s[-1]
        while qq:
            qs.append(qq)
            qq = qq.parent

        pre = 1
        pi = len(ps) - pre
        qi = len(qs) - pre
        while qi>=0 and pi>=0 and ps[pi].val == qs[qi].val:
            pre += 1
            pi = len(ps) - pre
            qi = len(qs) - pre

        return ps[pi+1]

if __name__ == '__main__':
    from tree import  get_tree, TreeNode
    node = get_tree([37,-34,-48,"null",-100,-100,48,"null","null","null","null",-54,"null",-71,-22,"null","null","null",8])
    p = TreeNode(-100)
    q = TreeNode(-100)
    sol = Solution()
    print sol.lowestCommonAncestor(node, p, q).val
    pass
예제 #12
0
        from collections import deque, defaultdict
        queue = deque([])
        level_dic = defaultdict(lambda: 0)
        level = 0
        queue.appendleft(root)
        level_dic[level] = 1

        while len(queue):
            lcout = level_dic[level]
            while lcout:
                q = queue.pop()
                lcout -= 1
                if q.left:
                    queue.appendleft(q.left)
                    level_dic[level + 1] += 1
                if q.right:
                    queue.appendleft(q.right)
                    level_dic[level + 1] += 1
                if not lcout:
                    rlist.append(q.val)
            level += 1

        return rlist


if __name__ == '__main__':
    from tree import get_tree
    node = get_tree([1, 2, 3])
    sol = Solution()
    print sol.rightSideView(node)
    pass
예제 #13
0
파일: 236.py 프로젝트: bondlee/leetcode
        pp = s[-2]
        while pp:
            ps.append(pp)
            pp = pp.parent
        qq = s[-1]
        while qq:
            qs.append(qq)
            qq = qq.parent

        pre = 1
        pi = len(ps) - pre
        qi = len(qs) - pre
        while qi >= 0 and pi >= 0 and ps[pi].val == qs[qi].val:
            pre += 1
            pi = len(ps) - pre
            qi = len(qs) - pre

        return ps[pi + 1]


if __name__ == '__main__':
    from tree import get_tree, TreeNode
    node = get_tree([
        37, -34, -48, "null", -100, -100, 48, "null", "null", "null", "null",
        -54, "null", -71, -22, "null", "null", "null", 8
    ])
    p = TreeNode(-100)
    q = TreeNode(-100)
    sol = Solution()
    print sol.lowestCommonAncestor(node, p, q).val
    pass
예제 #14
0
파일: 98.py 프로젝트: bondlee/leetcode
#         self.left = None
#         self.right = None

class Solution(object):
    def isValidBST(self, root):
        """
        :type root: TreeNode
        :rtype: bool
        """
        stack = []
        p = root
        pre = None
        while p or len(stack):
            if p:
                stack.append(p)
                p = p.left
            else:
                p = stack.pop()
                if pre is not None and pre >= p.val:
                    return False
                pre = p.val
                p = p.right
        return True

if __name__ == '__main__':
    from tree import  get_tree
    node = get_tree([0, "null" ,-1])
    sol = Solution()
    print sol.isValidBST(node)

    pass
예제 #15
0
파일: 101.py 프로젝트: bondlee/leetcode
            level_count = tree_dic[level]
            ind = 0
            val_dic = defaultdict(lambda: 0)
            while level_count:
                q = queue.pop()
                level_count -= 1
                # 每个节点编号
                val_dic[q.num] = q.val
                if q.left:
                    queue.appendleft(q.left)
                    tree_dic[level + 1] += 1
                    q.left.num = q.num * 2
                if q.right:
                    queue.appendleft(q.right)
                    tree_dic[level + 1] += 1
                    q.right.num = q.num * 2 + 1

            for i in val_dic:
                if val_dic[i] != val_dic[2**(level) - 1 - i]:
                    return False
            level += 1
            # 检查该层是否符合要求
        return True


if __name__ == '__main__':

    node = get_tree([2, 3, 3, 4, 5, 5, 4, "null", "null", 8, 9, 9, 8])
    sol = Solution()
    print sol.isSymmetric(node)
    pass