87 lines
3.3 KiB
Markdown
87 lines
3.3 KiB
Markdown
# Roman numerals converter
|
|
|
|
Original assessment brief: https://github.com/turner-townsend/backend-assessment/blob/master/README.md#roman-numerals
|
|
|
|
A simple Python program that converts Roman numerals to Arabic numbers using additive logic.
|
|
|
|
## Supported numerals
|
|
|
|
| Numeral | Value |
|
|
| ------- | ----- |
|
|
| I | 1 |
|
|
| V | 5 |
|
|
| X | 10 |
|
|
| C | 100 |
|
|
| M | 1000 |
|
|
|
|
## Usage
|
|
|
|
```bash
|
|
python roman.py
|
|
```
|
|
|
|
### Examples
|
|
|
|
```
|
|
Please enter some Roman numerals: I
|
|
I = 1
|
|
|
|
Please enter some Roman numerals: VI
|
|
VI = 5 + 1 = 6
|
|
|
|
Please enter some Roman numerals: MXVII
|
|
MXVII = 1000 + 10 + 5 + 1 + 1 = 1017
|
|
|
|
Please enter some Roman numerals: ABC
|
|
ERROR: Invalid input
|
|
```
|
|
|
|
## Features
|
|
|
|
- **Additive conversion**: Treats all numerals as additive (IV = 1 + 5 = 6)
|
|
- **Case insensitive**: Accepts both uppercase and lowercase
|
|
- **Input filtering**: Ignores invalid characters, extracts valid numerals
|
|
- **Interactive**: Continuous input until Ctrl+C
|
|
|
|
## Requirements
|
|
|
|
- Python 3.10+ (uses pattern matching syntax)
|
|
- pytest (for running tests)
|
|
|
|
## Tests
|
|
|
|
```bash
|
|
pytest -v
|
|
```
|
|
|
|
Example output:
|
|
|
|
```shell
|
|
➜ roman git:(main) ✗ pytest -v
|
|
================================================================ test session starts ================================================================
|
|
platform linux -- Python 3.12.2, pytest-8.3.4, pluggy-1.5.0 -- /home/jamey/.venv/bin/python
|
|
cachedir: .pytest_cache
|
|
rootdir: /home/jamey/Projects/turner-townsend-backend-assessment/roman
|
|
plugins: mock-3.14.0, asyncio-0.21.2
|
|
asyncio: mode=Mode.STRICT
|
|
collected 9 items
|
|
|
|
test_roman.py::test_valid_single_numerals PASSED [ 11%]
|
|
test_roman.py::test_valid_multiple_numerals PASSED [ 22%]
|
|
test_roman.py::test_lowercase_input PASSED [ 33%]
|
|
test_roman.py::test_mixed_case_input PASSED [ 44%]
|
|
test_roman.py::test_invalid_characters PASSED [ 55%]
|
|
test_roman.py::test_mixed_valid_and_invalid_characters PASSED [ 66%]
|
|
test_roman.py::test_empty_string PASSED [ 77%]
|
|
test_roman.py::test_only_spaces PASSED [ 88%]
|
|
test_roman.py::test_spaces_and_valid_numerals PASSED [100%]
|
|
|
|
================================================================= 9 passed in 0.02s =================================================================
|
|
```
|
|
|
|
## Design notes
|
|
|
|
- **Simple approach**: Uses additive logic only, as the brief states complex Roman numeral rules are not required
|
|
- **User-friendly**: Filters out invalid characters rather than rejecting entire input
|
|
- **Modern Python**: Uses pattern matching (requires Python 3.10+)
|