💻 Coding
Code Translation Prompt: Port Code Between Languages, Tested
Paste code, the source and target language versions and the behavior that must match: get a construct map, idiomatic ported code, gotchas and matching tests.
1Reviews
Prompt
Act as a polyglot software engineer who ports production code between languages and treats "same behavior" as the requirement, not "looks similar". You read the source code for what it actually does, including edge cases the author may not have noticed (rounding, integer division, string encoding, error types), map each construct to its idiomatic equivalent in the target language, and prove the port with tests that run the same cases in both. Inputs: - The code to port, complete, with its imports: [SourceCode] - Source language and version: [FromLanguage] - Target language, version and runtime: [ToLanguage] - Libraries allowed or banned in the target, and the test runner: [TargetStack] - Behavior that must match exactly, including known edge cases: [MustMatch] - Naming, style and error handling rules in the target codebase: [StyleRules] Generate: 1. Behavior summary: what the source code does, its inputs, outputs and errors, in plain words. 2. Construct map: a table of each source construct and its target equivalent, marking any that behave differently. 3. Ported code: complete, idiomatic for the target, following StyleRules, with no placeholder functions. 4. Behavior differences: every place where a literal translation would change results (rounding, division, overflow, null handling, sorting, string length), and how the port handles each. 5. Tests: target language tests that cover MustMatch and the differences in step 4, with expected values computed from the source code's behavior. 6. How to run: the exact command to run the tests in the target runtime. 7. Review checklist: what a reviewer should compare by hand before merging. Rules: do not change behavior silently, even to fix a bug; flag suspected bugs separately. State any expected value you could not verify by running the source code.
Instructions
Paste complete code with imports; missing helpers lead to guessed behavior. Variation: port Go to Rust, Java to Kotlin or a shell script to Python by changing the languages; for a whole module, port one file at a time with its tests. Tip: run the source and the port on the same inputs and diff the output before you trust the tests. If your chat tool can run code, ask it to run the source to compute expected values. Review every line before merging.
Generated Output
This image was generated using the prompt above.

Examples
Example Input
SourceCode:
def split_bill(total_cents: int, people: int, tip_pct: int) -> list[int]:
if people <= 0:
raise ValueError("people must be positive")
tip = round(total_cents * tip_pct / 100)
grand = total_cents + tip
base, extra = divmod(grand, people)
return [base + 1 if i < extra else base for i in range(people)]
FromLanguage: Python 3.12
ToLanguage: TypeScript, run on Node.js 24
TargetStack: no dependencies; built-in node:test and node:assert
MustMatch: same shares as the Python version for every input, including tips that land on exactly half a cent; shares always add up to the grand total
StyleRules: camelCase, named exports, throw RangeError for bad argumentsExample Output
1. BEHAVIOR SUMMARY
Splits a bill in integer cents. Adds a tip rounded to whole cents, then divides the grand total so shares differ by at most 1 cent; the first people get the extra cents. Raises ValueError when people is 0 or negative.
2. CONSTRUCT MAP
round(x) -> custom roundHalfEven(x). DIFFERENT: Python rounds halves to even (252.5 -> 252), Math.round rounds them up (252.5 -> 253).
divmod(a, b) -> Math.floor(a / b) and a % b. Same for non-negative values; differs for negatives, so the port rejects negative totals.
list comprehension -> Array.from with a map function.
ValueError -> RangeError (StyleRules).
3. PORTED CODE (splitBill.ts)
export function roundHalfEven(x: number): number {
const r = Math.round(x);
return Math.abs(x % 1) === 0.5 ? (r % 2 === 0 ? r : r - 1) : r;
}
export function splitBill(totalCents: number, people: number, tipPct: number): number[] {
if (!Number.isInteger(people) || people <= 0) {
throw new RangeError("people must be positive");
}
if (!Number.isInteger(totalCents) || totalCents < 0) {
throw new RangeError("totalCents must be a non-negative integer");
}
const tip = roundHalfEven((totalCents * tipPct) / 100);
const grand = totalCents + tip;
const base = Math.floor(grand / people);
const extra = grand % people;
return Array.from({ length: people }, (_, i) => (i < extra ? base + 1 : base));
}
4. BEHAVIOR DIFFERENCES
- Half-cent tips: 2525 cents at 10% is 252.5. Python gives 252; a plain Math.round port gives 253 and every share changes. roundHalfEven fixes it.
- Negative totals: Python's divmod floors and JavaScript's % truncates, so results would differ. Python accepts negatives; the port throws instead. Flagged: confirm negative totals never occur.
- Non-integer people: Python's range raises TypeError for a float; the port throws RangeError.
5. TESTS (splitBill.test.ts)
import { test } from "node:test";
import assert from "node:assert/strict";
import { splitBill } from "./splitBill.ts";
test("half-cent tip rounds to even like Python", () => {
assert.deepEqual(splitBill(2525, 3, 10), [926, 926, 925]);
});
test("remainder cents go to the first people", () => {
assert.deepEqual(splitBill(1250, 4, 18), [369, 369, 369, 368]);
});
test("shares always add up to the grand total", () => {
assert.equal(splitBill(1000, 3, 15).reduce((a, b) => a + b, 0), 1150);
});
test("zero people throws", () => {
assert.throws(() => splitBill(1000, 0, 10), RangeError);
});
Expected values come from running the Python function: [926, 926, 925], [369, 369, 369, 368] and a 1150 total.
6. HOW TO RUN
node --test splitBill.test.ts
Node.js 24 runs these .ts files directly by stripping types. If you compile with tsc instead, enable allowImportingTsExtensions or import "./splitBill.js".
7. REVIEW CHECKLIST
- Run both versions on the same 20 random inputs and compare.
- Confirm negative totals cannot reach this function.
- Check callers expect a RangeError, not a ValueError message string.