Skip to content

Fix three bugs in the reference Python programs - #54

Closed
scottchristensen9-lgtm wants to merge 1 commit into
jkoppel:masterfrom
scottchristensen9-lgtm:fix-reference-bugs
Closed

scottchristensen9-lgtm wants to merge 1 commit into
jkoppel:masterfrom
scottchristensen9-lgtm:fix-reference-bugs

Conversation

@scottchristensen9-lgtm

Copy link
Copy Markdown

While running the Python test suite I found three bugs that are still present in correct_python_programs/ (and the shared node.py helpers). None of them are the seeded bugs, and this PR leaves python_programs/ alone apart from its copy of node.py, which has no seeded bug.

1. shortest_path_length: a distance update can break the heap. insert_or_update overwrites an entry in place (the comment says heapq keeps the order, but it doesn't). When a node's distance drops below its parent's in the heap, heappop returns the wrong node. Graph: a->b 1, a->c 8, a->d 7, b->c 2, c->d 2. The answer is 5 (a, b, c, d), but the reference returns 7 because d is popped before the updated c. Fix: heapify after the replacement.

2. wrap: hangs when the remaining text starts with a space. wrap("hello world", 5) cuts "hello", then rfind(' ', 0, 6) on " world" returns 0, so the loop appends "" forever. Fix: when end == 0, emit the space as its own line, which gives ["hello", " ", "world"]. That output keeps the docstring's rules: every line fits, spaces are preserved, and the lines join back into the original.

3. node.py: default lists are shared by every node. successors=[] (and the other three) is one list object for every Node built without that argument, so appending to one node's default list changes all of them. On master, adding a test that does Node("a").successors.append(...) makes five unrelated graph tests fail (test_shortest_path_length 2 and 4, test_topological_ordering 1 to 3). The successor(), successors() and predecessors() methods in the program copies were also hidden by the attributes of the same name, so they could never be called. I removed them.

Tests. Each fix comes with a repro test: test5 in test_shortest_path_length.py, test_wrap_leading_space in test_wrap.py, and a new test_node.py. All three fail on master (wrong distance, timeout, shared list) and pass here.

  • pytest --correct --timeout=20 python_testcases -k "not knapsack": 270 passed, 1 skipped. Master gives 267 passed; the 3 extra are the new tests.
  • Buggy mode (--timeout=5, same filter, new tests excluded): all 264 outcomes match master, so benchmark scoring is unchanged.
  • I excluded knapsack only for speed. It passes with no timeout but takes about 5 minutes here, on master too.

🤖 Generated with Claude Code

https://claude.ai/code/session_014ko1H1xkFYEG8XajqP9FPS

- shortest_path_length: insert_or_update overwrote a heap entry in place,
  which breaks the heap order when a distance drops; re-heapify after it.
- wrap: when the remaining text starts with a space, rfind returns 0 and
  the loop never advances; emit the space as its own line.
- node.py: the [] defaults were one list shared by every Node, so adding
  a successor to one default-built node added it to all of them. The
  successor/successors/predecessors methods were also shadowed by the
  attributes of the same name and could never be called; removed.

Adds a repro test for each. pytest --correct passes; buggy-mode outcomes
are unchanged for every existing test.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014ko1H1xkFYEG8XajqP9FPS
@jkoppel

jkoppel commented Oct 17, 2026 •

Copy link
Copy Markdown
Owner

I'm going to close this without reading. First, as a general policy: to accept an AI written PR from a stranger, I at least expect the contributor to put as much effort into preparing it as I would into reviewing it, and at least to give a human-written PR description. Second: it seems unlikely any errors of this type have persisted in such shallow code after so many eyeballs have been on it. Third: at first glance, without having tried to understand, this sounds like exactly the kind of thing AIs get wrong. AIs are infamous for reporting as "issues" when a program doesn't handle illegal input.

Feel free to reopen after human review if you believe this issue is genuine.

@jkoppel jkoppel closed this Oct 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants