AI tools

Free AI code generator

Generate a small Python or JavaScript program with a run command and sample assertions. Review and test the code before using it.

How it works

  1. Describe one small computation, including an input and its expected result.
  2. Choose Python or JavaScript and specify edge cases or dependencies to avoid.
  3. Read the complete code, run it in an isolated environment and add tests for your own data.

Examples

Sample results for the inputs shown, so you can see the format and quality before you try it.

Example 1: input

Task, inputs and expected result: Define unique_keep_order(values), returning distinct integers in first-seen order. Example [3,1,3,2,1] gives [3,1,2]. Include the empty-list case.

Language: Python 3

Constraints or edge cases: Do not mutate the input list.

Example 1: output
# run command: python3 main.py # Assumptions: values is any iterable of hashable integers; duplicates removed by # first occurrence; input is never mutated (we only iterate over it). def unique_keep_order(values): seen = set() result = [] for v in values: if v not in seen: seen.add(v) result.append(v) return result # Supplied example assert unique_keep_order([3, 1, 3, 2, 1]) == [3, 1, 2] # Empty-list boundary case assert unique_keep_order([]) == [] # Single element assert unique_keep_order([7]) == [7] # All duplicates assert unique_keep_order([5, 5, 5, 5]) == [5] # Already unique assert unique_keep_order([1, 2, 3, 4]) == [1, 2, 3, 4] # Negatives and zero assert unique_keep_order([0, -1, 0, 2, -1, 3]) == [0, -1, 2, 3] # Input list must not be mutated original = [3, 1, 3, 2, 1] snapshot = list(original) unique_keep_order(original) assert original == snapshot # Works on any iterable, e.g. a generator, without consuming a list copy gen_result = unique_keep_order(x for x in [2, 2, 4, 2, 5]) assert gen_result == [2, 4, 5] # Result is a new list, independent of the input src = [9, 9, 8] out = unique_keep_order(src) out.append(99) assert src == [9, 9, 8] # Large-ish case with interleaved repeats preserves first-seen order big = [i % 10 for i in range(100)] assert unique_keep_order(big) == list(range(10))
Example 2: input

Task, inputs and expected result: Define sumByCategory(rows), adding integer cents for each category. [{category:"books",cents:300},{category:"food",cents:125},{category:"books",cents:50}] yields {books:350,food:125}. Empty rows yields {}.

Language: JavaScript (Node.js)

Constraints or edge cases: Accept zero and negative cents; do not mutate rows.

Example 2: output
// run: node main.js "use strict"; // Assumptions: rows is an array of objects with string `category` and integer `cents`. // Grouping uses Map to safely handle arbitrary keys such as "__proto__", "constructor", "toString". // Input is not mutated. Sums may be zero or negative. const assert = require("node:assert"); function sumByCategory(rows) { const totals = new Map(); for (const row of rows) { const key = row.category; const cents = row.cents; totals.set(key, (totals.has(key) ? totals.get(key) : 0) + cents); } return Object.fromEntries(totals); } // Supplied example assert.deepStrictEqual( sumByCategory([ { category: "books", cents: 300 }, { category: "food", cents: 125 }, { category: "books", cents: 50 }, ]), Object.fromEntries([["books", 350], ["food", 125]]) ); // Empty rows assert.deepStrictEqual(sumByCategory([]), {}); // Zero and negative cents, and repeated categories summing to zero assert.deepStrictEqual( sumByCategory([ { category: "a", cents: 0 }, { category: "a", cents: -5 }, { category: "a", cents: 5 }, { category: "b", cents: -125 }, ]), Object.fromEntries([["a", 0], ["b", -125]]) ); // Dangerous-looking keys must be plain own properties, not prototype pollution const tricky = sumByCategory([ { category: "__proto__", cents: 1 }, { category: "__proto__", cents: 2 }, { category: "constructor", cents: 3 }, { category: "toString", cents: 4 }, { category: "hasOwnProperty", cents: -4 }, ]); assert.deepStrictEqual( tricky, Object.fromEntries([ ["__proto__", 3], ["constructor", 3], ["toString", 4], ["hasOwnProperty", -4], ]) ); assert.strictEqual(Object.getPrototypeOf(tricky), Object.prototype); assert.strictEqual(Object.prototype.polluted, undefined); assert.strictEqual(Object.hasOwn(tricky, "__proto__"), true); assert.strictEqual(Object.hasOwn(tricky, "constructor"), true); assert.strictEqual(Object.hasOwn(tricky, "toString"), true); // Input must not be mutated const rows = [ { category: "x", cents: 10 }, { category: "y", cents: 20 }, { category: "x", cents: 30 }, ]; const snapshot = JSON.stringify(rows); const out = sumByCategory(rows); assert.strictEqual(JSON.stringify(rows), snapshot); assert.notStrictEqual(out, rows); assert.deepStrictEqual(out, Object.fromEntries([["x", 40], ["y", 20]])); // Single row and large-ish values assert.deepStrictEqual( sumByCategory([{ category: "z", cents: 2147483647 }]), Object.fromEntries([["z", 2147483647]]) ); console.log("All assertions passed.");
Example 3: input

Task, inputs and expected result: Define merge_intervals(intervals) for inclusive integer pairs. [[1,3],[2,5],[8,9]] gives [[1,5],[8,9]]. Merge overlap, but not merely adjacent pairs. Empty list gives [].

Language: Python 3

Constraints or edge cases: Accept unsorted intervals; leave the input unchanged.

Example 3: output
# run: python3 main.py # Assumptions: intervals are inclusive [start, end] integer pairs with start <= end. # Overlap means intervals share at least one integer; adjacency (e.g. [1,2],[3,4]) is NOT merged. # Input is not mutated; output is sorted by start. def merge_intervals(intervals): items = sorted((list(p) for p in intervals), key=lambda p: (p[0], p[1])) result = [] for start, end in items: if result and start <= result[-1][1]: if end > result[-1][1]: result[-1][1] = end else: result.append([start, end]) return result # Supplied example assert merge_intervals([[1, 3], [2, 5], [8, 9]]) == [[1, 5], [8, 9]] # Empty input assert merge_intervals([]) == [] # Unsorted input assert merge_intervals([[8, 9], [2, 5], [1, 3]]) == [[1, 5], [8, 9]] # Input must not be mutated data = [[1, 3], [2, 5], [8, 9]] snapshot = [list(p) for p in data] merge_intervals(data) assert data == snapshot # Adjacent pairs must NOT merge assert merge_intervals([[1, 2], [3, 4]]) == [[1, 2], [3, 4]] # Touching at a single point does merge assert merge_intervals([[1, 2], [2, 4]]) == [[1, 4]] # Nested interval assert merge_intervals([[1, 10], [3, 5]]) == [[1, 10]] # Identical intervals assert merge_intervals([[4, 6], [4, 6]]) == [[4, 6]] # Chain of overlaps assert merge_intervals([[1, 2], [2, 3], [3, 4]]) == [[1, 4]] # Single interval, and gap of exactly one integer assert merge_intervals([[5, 5]]) == [[5, 5]] assert merge_intervals([[1, 2], [4, 5]]) == [[1, 2], [4, 5]] # Negative values assert merge_intervals([[-5, -1], [-3, 0], [2, 3]]) == [[-5, 0], [2, 3]] # Result is a fresh structure, not aliasing the input out = merge_intervals([[1, 3]]) assert out == [[1, 3]] and out is not data print("all assertions passed")

Tips for better results

  • Use a concrete expected value; “sort this well” cannot serve as a test.
  • Start with pure functions so a draft cannot accidentally change files or call services.
  • Review assertions as carefully as the implementation; a generated test can repeat the same mistake.
  • For a larger Python-specific task, use the Python code generator and split the work into smaller functions.

FAQ

Has the generated code been executed?

The tool itself does not execute your generated result. Run it in an isolated environment and review it before use.

Which languages are supported?

This tool is limited to Python 3 and JavaScript for Node.js, using their standard libraries.

Can it build a production application?

It is intended for small computations. Authentication, storage, deployment and production security need separate design and review.

What does it cost to use this tool?

It is free after an email signup for Something Big. Daily usage limits apply; you can unsubscribe from the newsletter.

More free AI tools

Related prompts

Prefer to use ChatGPT or Claude directly? These free prompts do similar jobs.

Get the best free AI tools and prompts every week

Something Big is a free AI newsletter read by 50,000+ professionals. About three short emails a week with the AI tools and prompts that actually work, plus what changed in AI and what to do about it.