Answer A: Anthropic Claude Sonnet 4.6
def resolve_dependencies(package_definitions, target_package): # Parse package definitions graph = {} for definition in package_definitions: if ':' not in definition: raise ValueError(f'Invalid package definition: {definition}') name, _, deps_str = definition.partition(':') name = name.strip() deps_str = deps_str.strip() if deps_str: deps = [d.strip() for d in deps_str.split(',')] else: deps = [] graph[name] = deps # Check for missing packages for pkg, deps in graph.items(): for dep in deps:...
Show Full Answer ▼
def resolve_dependencies(package_definitions, target_package): # Parse package definitions graph = {} for definition in package_definitions: if ':' not in definition: raise ValueError(f'Invalid package definition: {definition}') name, _, deps_str = definition.partition(':') name = name.strip() deps_str = deps_str.strip() if deps_str: deps = [d.strip() for d in deps_str.split(',')] else: deps = [] graph[name] = deps # Check for missing packages for pkg, deps in graph.items(): for dep in deps: if dep not in graph: raise ValueError(f'Missing package definition for: {dep}') if target_package not in graph: raise ValueError(f'Missing package definition for: {target_package}') # Topological sort using DFS, only for packages reachable from target_package # States: 0 = unvisited, 1 = in progress, 2 = done state = {} result = [] path = [] # track current path for cycle detection def dfs(pkg): if state.get(pkg) == 2: return if state.get(pkg) == 1: # Find cycle in path cycle_start = path.index(pkg) cycle = path[cycle_start:] + [pkg] raise ValueError(f'Circular dependency detected involving: {" -> ".join(cycle)}') state[pkg] = 1 path.append(pkg) for dep in graph[pkg]: dfs(dep) path.pop() state[pkg] = 2 result.append(pkg) dfs(target_package) return result
Result
Winning Votes
0 / 3
Average Score
Total Score
Overall Comments
Answer A provides a correct and functional dependency resolver using a standard DFS approach. It correctly handles circular dependencies and missing packages with the specified error messages. However, its input parsing is less robust, particularly regarding duplicate package definitions (which it overwrites) and malformed dependency lists (e.g., 'A: , B' would result in an empty string dependency).
View Score Details ▼
Correctness
Weight 35%The solution correctly performs topological sorting and raises appropriate errors for circular and missing dependencies. However, its parsing of dependency strings like 'A: , B' would incorrectly include an empty string as a dependency, which is a minor correctness flaw in graph construction.
Completeness
Weight 20%The solution implements all core features but lacks handling for duplicate package definitions (it overwrites them) and is less robust in parsing malformed dependency strings, which reduces its completeness for real-world inputs.
Code Quality
Weight 20%The code is readable and uses a standard DFS algorithm. The parsing logic is integrated directly into the main function. It is reasonably efficient and not overly complex.
Practical Value
Weight 15%The solution is functional but less robust to common real-world input variations, such as duplicate package definitions or slightly malformed dependency lists, which limits its practical applicability without further refinement.
Instruction Following
Weight 10%The solution adheres to the function signature, return type, error types, and specified error message formats. It correctly implements the core requirements.
Total Score
Overall Comments
Implements a correct DFS-based topological sort restricted to the target’s reachable subgraph, with cycle detection via an explicit path stack and clear cycle reporting. It correctly checks for missing package definitions (including missing target) before traversal. Weaknesses: parsing is a bit loose (doesn’t filter empty dependency tokens like 'A: B,', so it may raise a missing-package error for an empty string); it silently overwrites duplicate package definitions rather than rejecting them; and it does a global missing-dependency scan over all packages rather than just the reachable subset (not incorrect, but potentially surprising).
View Score Details ▼
Correctness
Weight 35%Correct DFS topological ordering for the target subgraph; cycle detection and missing-package detection generally work. However, trailing commas can produce an empty dependency name that triggers a misleading missing-package error, reducing correctness on loosely formatted inputs.
Completeness
Weight 20%Meets core requirements (parse, topo sort, cycle/missing errors). Lacks handling of duplicate definitions and doesn’t sanitize empty dep tokens, which are common minor format issues.
Code Quality
Weight 20%Readable and short; uses standard DFS states. Minor issues: path.index lookup inside cycle case and silent overwriting on duplicates; less defensive parsing.
Practical Value
Weight 15%Useful as-is for clean inputs, but less resilient to slightly messy definitions and duplicates, which reduces real-world usefulness.
Instruction Following
Weight 10%Implements the requested function and raises ValueError with descriptive messages including a cycle path; returns the correct format.
Total Score
Overall Comments
Answer A provides a correct and clean implementation of the dependency resolver. It properly parses input, detects missing packages, detects circular dependencies with path tracking, and performs DFS-based topological sort. The code is concise and readable. However, it lacks some edge case handling such as duplicate package definitions, empty package names, and non-string inputs. It also lacks a docstring and example usage.
View Score Details ▼
Correctness
Weight 35%Answer A correctly implements topological sort via DFS, properly detects circular dependencies with accurate cycle path reporting, detects missing packages, and produces valid installation orders. The core algorithm is correct for all specified test cases.
Completeness
Weight 20%Answer A handles the main requirements: parsing, topological sort, cycle detection, and missing package detection. However, it does not handle edge cases like duplicate package definitions, empty package names, non-string inputs, or empty strings resulting from splitting dependencies.
Code Quality
Weight 20%Answer A is concise and readable with clear variable names and comments. The code structure is straightforward. However, it lacks a docstring and the parsing logic is inline rather than separated into a helper function.
Practical Value
Weight 15%Answer A is functional and would work in practice for well-formed inputs. However, the lack of input validation for edge cases reduces its robustness in real-world scenarios where inputs may be malformed.
Instruction Following
Weight 10%Answer A follows the instructions well: it implements the required function signature, parses the input format, performs topological sort, detects cycles with descriptive messages, and detects missing packages. The error message format matches the examples.