CS61A hw04

news/2025/3/29 18:12:48/

这道题目比较有代表性,recursive function是解题的关键
Question 6
Write a function has_path that takes in a tree t and a string word. It returns True if there is a path that starts from the root where the entries along the path spell out the word, and False otherwise. (This data structure is called a trie, and it has a lot of cool applications, such as autocomplete). You may assume that every node’s label is exactly one character.

def has_path(t, word):"""Return whether there is a path in a tree where the entries along the pathspell out a particular word.>>> greetings = tree('h', [tree('i'),...                        tree('e', [tree('l', [tree('l', [tree('o')])]),...                                   tree('y')])])>>> print_tree(greetings)hielloy>>> has_path(greetings, 'h')True>>> has_path(greetings, 'i')False>>> has_path(greetings, 'hi')True>>> has_path(greetings, 'hello')True>>> has_path(greetings, 'hey')True>>> has_path(greetings, 'bye')False>>> has_path(greetings, 'hint')False"""assert len(word) > 0, 'no path for empty word.'if label(t) == word:return Trueelse:nb = [has_path(b, word) for b in branches(t)]return label(t) + sum(nb) == word

Trees

Tree Data Abstraction Implementation:

def tree(label, branches=[]):"""Construct a tree with the given label value and a list of branches."""return [label] + list(branches)def label(tree):"""Return the label value of a tree."""return tree[0]def branches(tree):"""Return the list of branches of the given tree."""return tree[1:]def is_leaf(tree):"""Returns True if the given tree's list of branches is empty, and Falseotherwise."""return not branches(tree)

http://www.ppmy.cn/news/271742.html

相关文章

About P6

P6 P6是原是美国Primavera System Inc.公司研发的项目管理软件Primavera 6.0(2007年7月1日全球正式发布)的缩写,暨Primavera公司项目管理系列软件的最新注册商标,与2008年被ORACLE公司收购,对外统一称作 Oracle Primav…

TB6600HG原理图

PWM 斩波型双极步进电机驱动 IC TB6600HG 是 PWM 斩波型单芯片双极正弦微步步进马达驱动器。 可通过 2-相,1-2-相,W1-2-相,2W1-2-相,和 4W1-2-相励磁模式,实现正向和反向旋转控制。 2-相双极步进马达仅由低振动高效…

折腾BIOS,改开机logo图标

友情提示:刷机有风险,烧录需谨慎。折腾这个不像装系统,一旦刷失败了电脑就会变砖,救砖很难。我刷成功不代表在大家的主板上就能成功,刷废主板后果自负。 获取BIOS文件 下载 从各个厂商的官网上下载的BIOS各不相同&a…

cs61a pp+ch. 1.1-1.2+HW01

ppch. 1.1-1.2HW01 PP expressions:An expression describes a computation and evaluates to a value call expressions: function call notation (函数调用表达式) nested expressions:嵌套表达式 Discussion Question1 f min f max g, h min, max max g max(f(2,g(…

P8H61 换 CPU,升级 BIOS,IDE 转 AHCI

简述 最近把家里服役了10年的 华硕P8H61 i3-2100 进行了一次升级(内存早已经加到了12G了) 升级CPU 首先上淘宝买了个 E3-1230V2,¥260(i3-2100 淘宝158,我140包邮卖了) 如果是比较早的 BIOS …

Linux线程的同步与互斥(二) 条件变量+信号量

文章目录 二、线程同步条件变量1、条件变量的概念2、同步概念与竞态条件3、条件变量函数初始化4、条件变量函数销毁5、条件变量函数等待6、条件变量函数唤醒等待生产者消费者模型1、理论部分2、“3 2 1”原则3、基于阻塞队列的生产者消费者模型 POSIX信号量1、信号量的概念2、信…

读写ini配置文件(C++)

文章目录 1、为什么要使用ini或者其它(例如xml,json)配置文件?2、ini文件基本介绍3、ini配置文件的格式4、C读写ini配置文件5、 代码示例6、 配置文件的解析库 文章转载于:https://blog.csdn.net/weixin_44517656/article/details/109014236 1、为什么要…

Java性能权威指南-总结8

Java性能权威指南-总结8 垃圾收集算法理解CMS收集器针对并发模式失效的调优 垃圾收集算法 理解CMS收集器 针对并发模式失效的调优 调优CMS收集器时最要紧的工作就是要避免发生并发模式失效以及晋升失败。 正如在CMS垃圾收集日志中看到的那样,发生并发模式失效往往…