def lookups(k, key): """Yield one lookup function for each node of k that has the label key. >>> k = tree(5, [tree(7, [tree(2)]), tree(8, [tree(3), tree(4)]), tree(5, [tree(4), tree(2)])]) >>> v = tree('Go', [tree('C', [tree('C')]), tree('A', [tree('S'), tree(6)]), tree('L', [tree(1), tree('A')])]) >>> type(lookups(k, 4)) <class 'generator'> >>> sorted([f(v) for f in lookups(k, 2)]) ['A', 'C'] >>> sorted([f(v) for f in lookups(k, 3)]) ['S'] >>> [f(v) for f in lookups(k, 6)] [] """ "*** YOUR CODE HERE ***"
时间: 2024-02-07 11:04:06 浏览: 23
该函数的作用是在树结构中查找所有节点中标签为指定值的节点,并返回对应节点的查找函数。
具体实现可以使用递归,对每个节点进行遍历,如果当前节点的标签与指定值相等,则将对应节点的查找函数加入到生成器中返回。如果当前节点不是叶子节点,则对其所有子节点递归调用该函数。
以下是具体的实现代码:
```python
def lookups(k, key):
if label(k) == key:
yield lambda v: v
for b in branches(k):
yield from lookups(b, key)
```
相关问题
def lookups(k, key): """Yield one lookup function for each node of k that has the label key. >>> k = tree(5, [tree(7, [tree(2)]), tree(8, [tree(3), tree(4)]), tree(5, [tree(4), tree(2)])]) >>> v = tree('Go', [tree('C', [tree('C')]), tree('A',
代码不完整,但是根据函数名和示例代码,我们可以猜测该函数的作用是在一棵树中查找标签为 key 的节点,并且返回一个生成器,每次生成一个函数,该函数可以查找一个满足要求的节点。
下面是完整的代码实现:
```python
def lookups(k, key):
if label(k) == key:
yield lambda: k
for b in branches(k):
yield from lookups(b, key)
# 等价于 yield from list(lookups(b, key))
```
首先,我们判断当前节点的标签是否为 key,如果是,则生成一个 lambda 函数,该函数返回当前节点 k。
如果当前节点不是要查找的节点,那么我们需要遍历当前节点的每个子节点,对于每个子节点,我们再次调用 lookups 函数,查找标签为 key 的节点。这里使用了 yield from 语句,将子节点的查找结果直接作为生成器的输出,避免了递归过程中生成器嵌套的问题。
最终,我们得到一个生成器,每次生成一个函数,该函数可以查找一个满足要求的节点。
## Problem 4: Trictionary or Treat ![k_and_v](pic/k_and_v.png) A trictionary is a pair of tree `k` and `v`. They have identical structure: each node in `k` has a corresponding node in `v`. The labels in `k` are called keys. Each key may be the label for multiple nodes in `k`, and the values for that key are the labels of all the corresponding nodes in `v`. A lookup function returns one of the values for a key. Specifically, a lookup function for a node in `k` is a function that takes `v` as an argument and returns the label for the corresponding node in `v`. Implement the generator function `lookups`, which takes as input a tree `k` and a key. It yields all lookup functions for nodes in `k` that have key as their label, the functions could be yielded in any order. ```python def lookups(k, key): """Yield one lookup function for each node of k that has the label key. >>> k = tree(5, [tree(7, [tree(2)]), tree(8, [tree(3), tree(4)]), tree(5, [tree(4), tree(2)])]) >>> v = tree('Go', [tree('C', [tree('C')]), tree('A', [tree('S'), tree(6)]), tree('L', [tree(1), tree('A')])]) >>> type(lookups(k, 4)) <class 'generator'> >>> sorted([f(v) for f in lookups(k, 2)]) ['A', 'C'] >>> sorted([f(v) for f in lookups(k, 3)]) ['S'] >>> [f(v) for f in lookups(k, 6)] [] """ "*** YOUR CODE HERE ***" ```
这道题需要实现一个 `lookups` 生成器函数,它的输入参数是两棵树 `k` 和一个 key,输出结果是所有在 `k` 中标签为 key 的节点对应的在 `v` 中的节点的标签。
我们可以使用递归的方式遍历树 `k`,对于每个节点,如果它的标签等于 key,则生成一个 lookup 函数,该函数的输入为 `v`,输出为对应节点在 `v` 中的标签;否则递归遍历它的子节点。最终,我们需要将所有生成的 lookup 函数作为生成器的输出。
以下是实现代码:
```python
def lookups(k, key):
"""
Yield one lookup function for each node of k that has the label key.
"""
if label(k) == key:
yield lambda v: label(v)
for child_k, child_v in zip(children(k), children(v)):
yield from lookups(child_k, child_v)
```
其中的 `label` 函数返回节点的标签,`children` 函数返回节点的子节点列表。`yield from` 可以将子生成器产生的值逐一地传递给父生成器的调用者。
这段代码首先判断当前节点的标签是否等于 key,如果是,则生成一个新的 lookup 函数,否则递归遍历子节点,并将生成的 lookup 函数加入生成器中。
注意,这里的 `v` 是在函数定义时未定义的,它应该在调用 `lookups` 函数时作为参数传入。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![json](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)