Gerar comentários e docstrings do Python

Gerar docstrings e comentários para funções do Python.

É possível consultar um modelo diretamente e testar os resultados retornados ao usar diferentes valores de parâmetros com o console do Cloud ou ao chamar a API Vertex AI diretamente.

Comando Formato livre

Aqui estão os códigos Python. Insira o comentário do documento (docstring) para todas as funções, incluindo as funções aninhadas.
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)

Resposta

    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)

Modelo: Gemini Flash
Temperatura: 1
Máximo de tokens de saída: 8192
TopK: 40
TopP: 0,95