admin 管理员组文章数量: 887031
2024年2月25日发(作者:哪里可以看免费的教学视频网站)
def append(self,*args,**kwargs): self._(*args,**kwargs) def print_all_1(self): print self for oChild in self._lChildren: _all_1() def print_all_2(self): def gen(o): lAll = [o,] while lAll: oNext = (0) (oNext._lChildren) yield oNext for oNode in gen(self): print oNodeoRoot = Node("root")oChild1 = Node("child1")oChild2 = Node("child2")oChild3 = Node("child3")oChild4 = Node("child4")oChild5 = Node("child5")oChild6 = Node("child6")oChild7 = Node("child7")oChild8 = Node("child8")oChild9 = Node("child9")oChild10 = Node("child10")(oChild1)(oChild2)(oChild3)
class Node(object): def __init__(self, data, left=None, right=None): = data = left = righttree = Node(1, Node(3, Node(7, Node(0)), Node(6)), Node(2, Node(5), Node(4)))15 层次遍历def lookup(root): stack = [root] while stack: current = (0) print if : () if : ()16 深度遍历def deep(root): if not root: return print deep() deep()if __name__ == '__main__': lookup(tree) deep(tree)17 前中后序遍历深度遍历改变顺序就OK了.#coding:utf-8
class Node(object): def __init__(self, data=None, next=None): = data = nextlink = Node(1, Node(2, Node(3, Node(4, Node(5, Node(6, Node(7, Node(8, Node(9)))))))))def rev(link): pre = link cur = = None while cur: tmp = = pre pre = cur cur = tmp return preroot = rev(link)while root: print root = 22 两个字符串是否是变位词class Anagram: """ @:param s1: The first string @:param s2: The second string @:return true or false """ def Solution1(s1,s2): alist = list(s2) pos1 = 0
stillOK = True while pos1 < len(s1) and stillOK: pos2 = 0 found = False while pos2 < len(alist) and not found: if s1[pos1] == alist[pos2]: found = True else: pos2 = pos2 + 1 if found: alist[pos2] = None else: stillOK = False pos1 = pos1 + 1 return stillOK print(Solution1('abcd','dcba')) def Solution2(s1,s2): alist1 = list(s1) alist2 = list(s2) () () pos = 0 matches = True while pos < len(s1) and matches: if alist1[pos] == alist2[pos]: pos = pos + 1 else: matches = False return matches
版权声明:本文标题:最全BATPython面试题及答案 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.freenas.com.cn/free/1708836400h532455.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论