And allowing python shells for both I think the randomly selected human would do worse on average.
And I also think e.g. Google's IMO model would be able to pass but I have no way to verify that.
I mean, this is a super basic python task that I could write in 2 minutes. The naiive implementation is a couple of basic for loops. I wouldn't hire a junior dev that couldn't write it.
My point here is that for someone who can program, it's easier and better to write this in python than to write it in english and have an LLM translate it to python (and LLMs themselves are incapable of logic or reasoning, so the only option is for them to translate it to real code).
"You can ask any clarifying questions first before starting on the problem."
Here's what it asked (Claude Opus, web search off so it didn't find your post):
> Looking at this problem, I want to make sure I understand the process correctly before diving into the solution. Let me clarify a few things:
> When you say "flip on every fourth one" for the powers of two, do you mean:
> We flip switches at positions 4, 8, 12, 16, 20, 24, ... (every 4th position) Then flip switches at positions 8, 16, 24, 32, 40, 48, ... (every 8th position) And so on?
> When you move to "powers of three", do you mean:
> Start with every 3rd switch (3, 6, 9, 12, ...) Then every 9th switch (9, 18, 27, 36, ...) Then every 27th switch, etc.?
> Or does "powers of three" mean we start with 3² = 9 as the first increment, similar to how we started with 2² = 4 for powers of two?
> When you say "then four, then five, and so on" - do you mean we continue this process for all integers k ≥ 2, using powers of k as our increments?
> The final state of each switch depends on whether it gets flipped an odd or even number of times total, so I want to make sure I understand the flipping pattern correctly before calculating the answer.
So my experience in working with AI is actually much more similar to working with people, except I have to correct the AI much less frequently.
I always say, AI is technology that behaves like people, and so the trick to working with it effectively is to approach it like working with a colleague, with all their specific quirks and skillsets, rather than a tool.
It can translate, but it cannot reason or calculate. The python can calculate.
Here is one recent exchange (some proprietary details redacted) with Claude Sonnet 3.7 via the Cody VSCode Extension (RIP):
---
Me: At this point, "segments" represents a list of bounding boxes that can overlap. Write a function to coalesce only those boxes that are substantially (i.e. within some tolerance) horizontally aligned along one or both edges or their centers, either: 1) overlapping completely or partially; or 2) very closely spaced like the words of a sentence should be (you can assume the threshold for spacing could be a reasonable fraction of the height of the box).
Claude: I'll create a function to coalesce horizontally aligned boxes based on your requirements. This function will identify boxes that are either overlapping or closely spaced and horizontally aligned (by top edge, bottom edge, or vertical center).
```javascript function coalesceHorizontallyAlignedSegments(segments, options = {}) { // Default options const { verticalAlignmentTolerance = 0.1, // 10% tolerance for vertical alignment horizontalSpacingThreshold = 0.5, // Max horizontal spacing as a fraction of box height overlapThreshold = 0.2 // Minimum overlap required (20% of the smaller box) } = options;
if (!segments || segments.length <= 1) {
return segments;
}
// Sort segments by x position (left to right)
const sortedSegments = [...segments].sort((a, b) => a.x - b.x);
// Track which segments have been merged
const merged = new Array(sortedSegments.length).fill(false);
const mergedSegments = [];
for (let i = 0; i < sortedSegments.length; i++) {
// Skip if this segment has already been merged
if (merged[i]) continue;
const currentSegment = sortedSegments[i];
let mergedSegment = { ...currentSegment };
merged[i] = true;
// Check for potential merges with subsequent segments
let didMerge = true;
while (didMerge) {
didMerge = false;
for (let j = 0; j < sortedSegments.length; j++) {
if (merged[j]) continue;
const nextSegment = sortedSegments[j];
// Check if horizontally aligned (top, bottom, or center)
const topAligned = Math.abs(mergedSegment.y - nextSegment.y) <=
verticalAlignmentTolerance * Math.min(mergedSegment.height, nextSegment.height);
const bottomAligned = Math.abs((mergedSegment.y + mergedSegment.height) -
(nextSegment.y + nextSegment.height)) <=
verticalAlignmentTolerance * Math.min(mergedSegment.height, nextSegment.height);
const centerAligned = Math.abs((mergedSegment.y + mergedSegment.height/2) -
(nextSegment.y + nextSegment.height/2)) <=
verticalAlignmentTolerance * Math.min(mergedSegment.height, nextSegment.height);
const isVerticallyAligned = topAligned || bottomAligned || centerAligned;
if (!isVerticallyAligned) continue;
// Check for overlap or close spacing
const mergedRight = mergedSegment.x + mergedSegment.width;
const nextLeft = nextSegment.x;
// Calculate horizontal spacing
const horizontalSpacing = nextLeft - mergedRight;
const maxAllowedSpacing = horizontalSpacingThreshold * Math.min(mergedSegment.height, nextSegment.height);
// Check for overlap
const isOverlapping = mergedRight >= nextLeft;
// Calculate overlap percentage if overlapping
let hasSignificantOverlap = false;
if (isOverlapping) {
const overlapWidth = Math.min(mergedRight, nextSegment.x + nextSegment.width) -
Math.max(mergedSegment.x, nextSegment.x);
const smallerWidth = Math.min(mergedSegment.width, nextSegment.width);
hasSignificantOverlap = overlapWidth / smallerWidth >= overlapThreshold;
}
// Merge if overlapping significantly or closely spaced
if (hasSignificantOverlap || (horizontalSpacing >= 0 && horizontalSpacing <= maxAllowedSpacing)) {
// Create a new bounding box that encompasses both segments
const newX = Math.min(mergedSegment.x, nextSegment.x);
const newY = Math.min(mergedSegment.y, nextSegment.y);
const newRight = Math.max(mergedSegment.x + mergedSegment.width,
nextSegment.x + nextSegment.width);
const newBottom = Math.max(mergedSegment.y + mergedSegment.height,
nextSegment.y + nextSegment.height);
mergedSegment = {
x: newX,
y: newY,
width: newRight - newX,
height: newBottom - newY,
// Preserve the virtual path from the leftmost segment
imagePath: mergedSegment.x <= nextSegment.x ? mergedSegment.imagePath : nextSegment.imagePath
};
merged[j] = true;
didMerge = true;
break;
}
}
}
// Add the topLeft and bottomRight properties for consistency
mergedSegment.topLeft = { x: mergedSegment.x, y: mergedSegment.y };
mergedSegment.bottomRight = {
x: mergedSegment.x + mergedSegment.width,
y: mergedSegment.y + mergedSegment.height
};
mergedSegments.push(mergedSegment);
}
return mergedSegments;
}
```This function works as follows:
1. It sorts segments from left to right to process them in reading order 2. For each segment, it looks for potential merges with other segments based on: - Vertical alignment (top edges, bottom edges, or centers within tolerance) - Either significant overlap (controlled by overlapThreshold) or - Close horizontal spacing (controlled by horizontalSpacingThreshold as a fraction of box height) 3. When segments are merged, it creates a new bounding box that encompasses both 4. It continues merging until no more merges are possible
You can integrate this function into your segmentation pipeline by adding it after the segment extraction:
```javascript function segmentImage(ctx, imageData, options = {}) { // ... existing code ...
const segments = boundingRects.map(toSegment);
// Coalesce horizontally aligned segments
const coalescedSegments = coalesceHorizontallyAlignedSegments(segments, options.coalesceOptions);
return { segments: coalescedSegments, contourTrees };
}
```Me: Do not write any code yet, but would it help if the segments were first grouped into approximate "rows" and then coalesced, rather than simply sorted by the x-coordinate?
Claude: Yes, grouping segments into approximate "rows" before coalescing would be significantly more effective than simply sorting by x-coordinate. Here's why:
1. *Better handling of multi-column layouts*: When a page has multiple columns of text or elements, sorting purely by x-coordinate would attempt to merge segments across different columns if they happen to be horizontally aligned.
2. *Respecting natural reading flow*: Text typically flows in rows, and grouping by rows first would respect this natural organization of content.
3. *More accurate merging decisions*: By first establishing which segments belong to the same "row" or line of content, we can make more informed decisions about which segments should be considered for merging.
4. *Handling of varying vertical positions*: Text lines on a webpage aren't always perfectly aligned. Pre-grouping into rows would allow us to handle slight variations in vertical positioning within what should be considered the same line.
5. *Efficiency*: By limiting the coalescing operations to segments within the same row, we reduce the number of comparisons needed.
The approach would work like this: 1. Group segments into rows based on vertical overlap or proximity 2. Within each row, sort segments by x-coordinate 3. Apply the coalescing logic only to segments within the same row
This approach would better preserve the semantic structure of the content while still merging segments that represent parts of the same logical unit (like words in a sentence).
Looking at the codebase, this approach would align well with how visual content is typically structured and would produce more meaningful segment groupings for downstream processing.
---
And then it proceeded to update the code as discussed. Sure this is not a very novel problem and it required a bit of back and forth, but look at the ratio of prompt to code. This exchange took a couple of minutes; I'd estimate it would have taken me an hour to get that code down with all edge cases handled. Look at the exposition, the quality of code, the choice to use optional parameters for tolerances, and edge-case handling.
It's very, very hard for me to not see this as reasoning. I mean, how is this not mind-blowing?
Asking an LLM to do logic would be saying something like: "I have a row of a million light switches. They all start off. I start at the beginning and flip on every fourth one. Then I flip on every eighth one, then sixteen, and all the powers of two until I'm over a million. Now I do the same for the powers of three, then four, then five, and so on. How many light switches are on at the end? Do not use any external coding tools for this; use your own reasoning."
Note that the prompt itself is intentionally ambiguous -- a human getting this question should say "I don't understand why you started with every fourth instead of every second. Are you skipping the first integer of every power series or just when the exponent is two?"
When I asked GPT5 to do it, it didn't care about that; instead it complimented me on my "crisp statement of the problem," roughly described a similar problem, and gave a belivable but incorrect answer 270,961 .
I then asked it to write python code to simulate my question. It got the code correct, and said "If you run this, you’ll see it matches the 270,961 result I gave earlier." except, that was a hallucination.
Running the code actually produced 252711.
I guess it went with 270,961 because that was a lexically similar answer to some lexically similar problems in the training data.