clanker: veet-hard-problems (run)

This commit is contained in:
kubasync-clanker 2026-02-10 18:51:21 +00:00
parent c4d74aca3e
commit de869bc230
4 changed files with 239 additions and 0 deletions

View file

@ -0,0 +1,40 @@
"""
Shortest Path
You're building a navigation system for a logistics company. Given a
network of cities connected by weighted roads, implement a shortest-path
finder that computes the minimum-cost route between two cities using
Dijkstra's algorithm. The system must also detect when no route exists.
Example 1:
Input: edges = [("A", "B", 4), ("A", "C", 2), ("C", "B", 1), ("B", "D", 5), ("C", "D", 8)], start = "A", end = "D"
Output: (7, ["A", "C", "B", "D"])
Explanation: A->C (2) + C->B (1) + B->D (5) = 7, cheaper than A->C->D (10) or A->B->D (9).
Example 2:
Input: edges = [("X", "Y", 3)], start = "Y", end = "X"
Output: (-1, [])
Explanation: No path from Y to X because the edge is one-directional.
Example 3:
Input: edges = [("A", "B", 1), ("B", "C", 2), ("A", "C", 10)], start = "A", end = "C"
Output: (3, ["A", "B", "C"])
Explanation: Going through B costs 3, which beats the direct edge of 10.
Constraints:
- Edges are directed: (source, destination, weight)
- All weights are positive integers (>= 1)
- No duplicate edges (same source and destination)
- Node names are non-empty strings
- Return (-1, []) if no path exists
- Return (0, [start]) if start == end
- If multiple shortest paths exist, return any one of them
- The path list includes both start and end nodes
"""
def shortest_path(
edges: list[tuple[str, str, int]], start: str, end: str
) -> tuple[int, list[str]]:
"""Return (cost, path) for shortest path, or (-1, []) if unreachable."""
pass # Your implementation here

View file

@ -0,0 +1,75 @@
"""Tests for shortest-path."""
import pytest
from solution import shortest_path
class TestBasicCases:
"""Test basic functionality with typical inputs."""
def test_example_one(self):
"""Test first example from problem description."""
edges = [("A", "B", 4), ("A", "C", 2), ("C", "B", 1), ("B", "D", 5), ("C", "D", 8)]
assert shortest_path(edges, "A", "D") == (7, ["A", "C", "B", "D"])
def test_example_two(self):
"""Test second example with no reverse path."""
edges = [("X", "Y", 3)]
assert shortest_path(edges, "Y", "X") == (-1, [])
def test_example_three(self):
"""Test indirect path cheaper than direct edge."""
edges = [("A", "B", 1), ("B", "C", 2), ("A", "C", 10)]
assert shortest_path(edges, "A", "C") == (3, ["A", "B", "C"])
def test_direct_edge_is_shortest(self):
"""Test when the direct edge is the cheapest route."""
edges = [("A", "B", 1), ("A", "C", 5), ("C", "B", 5)]
assert shortest_path(edges, "A", "B") == (1, ["A", "B"])
class TestEdgeCases:
"""Test edge cases and boundary conditions."""
def test_start_equals_end(self):
"""Test when start and end are the same node."""
edges = [("A", "B", 1)]
assert shortest_path(edges, "A", "A") == (0, ["A"])
def test_no_edges(self):
"""Test with empty edge list and different start/end."""
assert shortest_path([], "A", "B") == (-1, [])
def test_single_edge_path(self):
"""Test path that is a single edge."""
edges = [("A", "B", 7)]
assert shortest_path(edges, "A", "B") == (7, ["A", "B"])
def test_long_chain(self):
"""Test shortest path through a long chain of nodes."""
edges = [("A", "B", 1), ("B", "C", 1), ("C", "D", 1), ("D", "E", 1)]
assert shortest_path(edges, "A", "E") == (4, ["A", "B", "C", "D", "E"])
def test_disconnected_graph(self):
"""Test with disconnected components."""
edges = [("A", "B", 1), ("C", "D", 1)]
assert shortest_path(edges, "A", "D") == (-1, [])
def test_multiple_paths_picks_cheapest(self):
"""Test graph with many paths to verify optimal is chosen."""
edges = [
("S", "A", 10), ("S", "B", 3), ("B", "A", 1),
("A", "T", 2), ("B", "T", 20),
]
cost, path = shortest_path(edges, "S", "T")
assert cost == 6
assert path[0] == "S" and path[-1] == "T"
def test_large_weights(self):
"""Test with large edge weights."""
edges = [("A", "B", 1000000), ("B", "C", 1000000)]
assert shortest_path(edges, "A", "C") == (2000000, ["A", "B", "C"])
def test_end_node_not_in_graph(self):
"""Test when end node has no edges at all."""
edges = [("A", "B", 1), ("B", "C", 2)]
assert shortest_path(edges, "A", "Z") == (-1, [])