Repository navigation
LeetCode mode: run plain functions and your own node structs - #37
Merged
Merged
Conversation
LeetCode mode only took a class Solution and the built-in ListNode and TreeNode, so a pre_order(Node* root) with its own struct Node stopped at "needs a class Solution". Now: - Without a Solution class it calls the free function no other function calls; if several qualify, the first one a pasted main() calls. - Any struct with one value field and one self-pointer (a list) or two (a tree) is built from the test case, whatever its field names, through its constructor or by setting the value field. ListNode and TreeNode are the same generator with LeetCode's definitions. - A pasted main() is renamed with #define so line numbers stay put; with an empty test case the program runs as it is. The editor footer names the function that will be called. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
LeetCode mode only accepted
class Solutionwith the built-inListNode/TreeNode. A common learner program such asfailed immediately with LeetCode mode needs a class Solution. Even past that check,
Node*was not a type the harness could build.What changes
Which function runs
class Solution: its first public method, as before.height()is treated as a helper andisBalanced()runs.main(), the first one thatmain()calls wins.Node structs
data/next,key/l/r, …). Multi-declarator fields likeT *l, *r;work too.nullptr, because the harness owns the shape.nullgaps, viavector<optional<T>>, so string-valued trees work. Printing uses the same generic code.ListNode/TreeNodeare now just this generator fed LeetCode's definitions, still added when used but not defined.Pasting a whole program
main(), a#define main stackbloom_pasted_mainsets it aside without moving any lines, and the test case drives the chosen function.Checks
Compiled and traced eight generated programs through the real tracer:
pre_orderas pasted (constructor doesval = val)this->val = val, nomain1 2 3 4main, empty test casemain()runs,1 2 3 4struct Node { int data; Node* next; }without a constructor,reverse[3,2,1]T *l, *r,mainpicksmirrorovercount["a",null,"b",null,"c"]height+balancedtrue(entry =balanced)Solution::maxDepth(TreeNode*)3Solution::middle(ListNode*)[3,4,5]Trace stops only land in the pasted code and the generated
main, never in the harness.web/tests/leetcode.test.mjsgoes from 5 to 9 tests (17 Node tests in total, all passing). Also checked in the browser: footer, hint, badge and a run ofpre_orderfromroot = [1,2,4,3].README and
docs/usage.mdare updated.🤖 Generated with Claude Code