生成 Python 文档字符串和注释

为 Python 函数生成文档字符串和注释。

在对 Cloud 控制台使用不同参数值时,或通过直接调用 Vertex AI API,您可以直接查询模型,并测试返回的结果。

提示自由格式

下面是 Python 代码。为所有函数(包括嵌套函数)插入文档注释(文档字符串)。
def is_dag_dfs(graph):
    visited = set()
    on_stack = set()  # Track nodes on recursion stack

def dfs(node):
    if node in on_stack:
        return False  # Back-edge found (cycle)
    if node in visited:
        return True

    on_stack.add(node)
    visited.add(node)

    for neighbor in graph.get(node, []):
        if not dfs(neighbor):
            return False

    on_stack.remove(node)
    return True

return all(dfs(node) for node in graph)

响应

    def is_dag_dfs(graph):
    """
    Determines if a directed graph is a Directed Acyclic Graph (DAG) using Depth-First Search (DFS).

Args:
    graph (dict): A dictionary representing the graph. Keys are nodes, and values are lists of their neighbors.

Returns:
    bool: True if the graph is a DAG, False otherwise.
"""
visited = set()
on_stack = set()  # Track nodes on recursion stack

def dfs(node):
    """
    Performs Depth-First Search starting from a given node.

    Args:
        node (object): The node to start the DFS from.

    Returns:
        bool: True if no cycle is detected, False otherwise.
    """
    if node in on_stack:
        return False  # Back-edge found (cycle)
    if node in visited:
        return True

    on_stack.add(node)
    visited.add(node)

    for neighbor in graph.get(node, []):
        if not dfs(neighbor):
            return False

    on_stack.remove(node)
    return True

return all(dfs(node) for node in graph)

型号: gemini-1.5-flash-001
温度: 1
输出令牌数量上限: 8192
TopK: 40
TopP: 0.95