You are in a role play game. The following roles are available:…
- User
Assistant: User: System: You are in a role play game. The following roles are available: Admin: Call this Agent if: You need guidance. The program is not working as expected. You need api key DO NOT CALL THIS AGENT IF: You need to execute the code. Developer: Call this Agent if:
You need to write code.
DO NOT CALL THIS AGENT IF:
You need to execute the code. Planner: Call this Agent if:
You need to build a plan.
DO NOT CALL THIS AGENT IF:
You need to execute the code. Executor: Call this Agent if:
You need to execute the code written by the developer.
You need to execute the last script.
You have an import issue.
DO NOT CALL THIS AGENT IF:
You need to modify code. Read the following conversation. Then select the next role from ['Admin', 'Developer', 'Planner', 'Executor'] to play. Only return the role. User: Разработка по TDD. Напишите функцию - обход графа в глубину. User: Для разработки функции обхода графа в глубину (DFS) с использованием подхода TDD (Test-Driven Development), мы сначала напишем тесты, а затем реализуем саму функцию. Давайте начнем с написания тестов.Шаг 1: Написание тестов
Мы создадим тесты, чтобы проверить корректность нашей функции обхода графа в глубину. Для этого будем использовать библиотеку
unittest.import unittest class TestDFS(unittest.TestCase): def test_dfs_simple_graph(self): graph = { 'A': ['B', 'C'], 'B': ['A', 'D', 'E'], 'C': ['A', 'F'], 'D': ['B'], 'E': ['B', 'F'], 'F': ['C', 'E'] } start_node = 'A' expected_result = ['A', 'B', 'D', 'E', 'F', 'C'] self.assertEqual(dfs(graph, start_node), expected_result) def test_dfs_disconnected_graph(self): graph = { 'A': ['B'], 'B': ['A'], 'C': ['D'], 'D': ['C'] } start_node = 'A' expected_result = ['A', 'B'] self.assertEqual(dfs(graph, start_node), expected_result) def test_dfs_single_node(self): graph = { 'A': [] } start_node = 'A' expected_result = ['A'] self.assertEqual(dfs(graph, start_node), expected_result) if __name__ == '__main__': unittest.main()Шаг 2: Реализация функции
Теперь, когда у нас есть тесты, мы можем реализовать функцию
dfs.def dfs(graph, start): visited = [] stack = [start] while stack: node = stack.pop() if node not in visited: visited.append(node) stack.extend(reversed(graph[node])) # Добавляем соседей в стек в обратном порядке return visitedШаг 3: Запуск тестов
После реализации функции, мы запускаем тесты, чтобы убедиться, что наша функция работает корректно. Если все тесты пройдены успешно, значит, функция реализована правильно.
- Assistant
Executor