LLineCitrine
Workflows

Coding workflows · Minimum-raggedness line wrapping

03
Built around your work

A concrete starting point

Wrap at most 100 whitespace-free words into lines of one through 80 code points, minimizing squared unused space except on the last line. Test empty input and competing breaks, rejecting empty or oversized words without claiming terminal display width.

01

Open the context

Choose the project folder and name the files or behavior involved.

02

Define the change

Describe the input, expected output and boundaries that must stay intact.

03

Inspect the work

Review the proposed edits and any commands before relying on their result.

04

Check the outcome

Compare actual behavior with the expected result. If a check fails, investigate it before applying more changes.

Minimum-raggedness line wrapping

A concrete starting point
def wrap_words(words, width):
    words = tuple(words)
    if type(width) is not int or not 1 <= width <= 80 or len(words) > 100:
        raise ValueError("wrap.limit")
    if any(type(w) is not str or not w or len(w) > width or any(c.isspace() for c in w) for w in words):
        raise ValueError("wrap.word")
    n = len(words)
    costs, ends = [0] * (n+1), [0] * n
    for i in range(n-1, -1, -1):
        used, best = 0, None
        for j in range(i, n):
            used += len(words[j]) + (j > i)
            if used > width:
                break
            cost = 0 if j == n-1 else (width-used)**2 + costs[j+1]
            if best is None or cost < best:
                best, ends[i] = cost, j+1
        costs[i] = best
    lines, i = [], 0
    while i < n:
        lines.append(" ".join(words[i:ends[i]]))
        i = ends[i]
    return lines

The example is a starting point for a real project, not a claim that generated code is already tested. Adjust paths, dependencies and expected behavior to match your application.

AI coding workspace

Your next change starts here.

Get the Windows app ↗