Python の docstring とコメントを生成する

Python 関数の docstring とコメントを生成します。

直接モデルにクエリを実行して、返された結果をテストできます。その場合、Cloud コンソールでさまざまなパラメータ値を使用することも、Vertex AI API を直接呼び出すこともできます。

自由形式のプロンプト


Here are Python codes. Insert the document comment (docstring) for all functions, including the nested functions.

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)

                

レスポンス


```python
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, where keys are nodes and values are lists of adjacent nodes.

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 DFS on the graph starting from the given node.

Args:
node: The node to start DFS from.

Returns:
bool: True if the graph is a DAG, False if a cycle is found.
"""
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.0-pro-001
温度: 0.2
最大出力トークン: 2048
トップ K 40
トップ P 0.95