Generate a working function that converts between Roman numerals and integers in a chosen language, with the algorithm design explained and edge cases covered.
You are a coding exercise mentor who treats the Roman numeral converter as what it actually is, a classic algorithm design exercise about mapping values and handling a subtractive edge case correctly, not a syntax typing exercise. My language is [LANGUAGE:select:Python,JavaScript,Java,C++,C#,Go,Ruby]. My direction is [DIRECTION:select:integer to Roman numeral,Roman numeral to integer,both directions]. My starting point is [STARTING_POINT:select:explain the algorithm first, then show code,just show me the code with comments]. If I chose explain the algorithm first, walk through the approach in plain language before any code: for integer to Roman, working through a value-to-symbol list from largest to smallest and greedily subtracting, for Roman to integer, comparing each symbol's value to the one after it and adding or subtracting based on that comparison. Name the specific data structure that makes this clean, an ordered list of value-symbol pairs, not a lookup object alone, since order matters for the greedy approach and an unordered structure would break it. Write the actual function for [DIRECTION] in [LANGUAGE], using proper function signatures, meaningful variable names, and comments only where the logic isn't self-evident from the code itself. If I chose both directions, write them as two separate functions, not one function branching on a mode flag. Cover the input validation the exercise implies, standard Roman numerals only represent 1 through 3999, and handle or explicitly reject input outside that range rather than silently producing a wrong or malformed result. If converting Roman numeral to integer, decide and state plainly whether the function validates that the input is a well-formed numeral, such as rejecting more than three repeated symbols in a row, or assumes well-formed input for simplicity, and say why that choice makes sense for a first version of this exercise. Include three to five test cases as actual assertions or print statements, covering a simple case, a case requiring at least one subtractive pair, and the boundary values at the edges of the valid range. If I ask to see this same algorithm written a different, less efficient way, such as a lookup-table approach that handles each specific number's Roman form as a special case, show that version too and state specifically why the value-list approach above scales better as the range of numbers grows.
Use this prompt anywhere
10,000+ expert prompts for ChatGPT, Claude, Gemini, and wherever you use AI.
Get Early AccessDiscover more prompts that could help with your workflow.
Build a small working program that calls a real public API, with the request, response, and API key setup explained step by step.
Explain a core functional programming idea, such as pure functions or immutability, with a broken code example and its fixed version shown side by side.
Explain a built-in math function, square root, power, absolute value, or rounding, covering math and syntax, then generate practice calls to predict before revealing results.
10,000+ expert-curated prompts for ChatGPT, Claude, Gemini, and wherever you use AI. Our extension helps any prompt deliver better results.