-
Notifications
You must be signed in to change notification settings - Fork 2
add roman numerals
#39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| # Introduction | ||
|
|
||
| Your task is to convert a number from Arabic numerals to Roman numerals. | ||
|
|
||
| For this exercise, we are only concerned about traditional Roman numerals, in which the largest number is MMMCMXCIX (or 3,999). | ||
|
|
||
| ~~~~exercism/note | ||
| There are lots of different ways to convert between Arabic and Roman numerals. | ||
| We recommend taking a naive approach first to familiarise yourself with the concept of Roman numerals and then search for more efficient methods. | ||
|
|
||
| Make sure to check out our Deep Dive video at the end to explore the different approaches you can take! | ||
| ~~~~ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| # Description | ||
|
|
||
| Today, most people in the world use Arabic numerals (0–9). | ||
| But if you travelled back two thousand years, you'd find that most Europeans were using Roman numerals instead. | ||
|
|
||
| To write a Roman numeral we use the following Latin letters, each of which has a value: | ||
|
|
||
| | M | D | C | L | X | V | I | | ||
| | ---- | --- | --- | --- | --- | --- | --- | | ||
| | 1000 | 500 | 100 | 50 | 10 | 5 | 1 | | ||
|
|
||
| A Roman numeral is a sequence of these letters, and its value is the sum of the letters' values. | ||
| For example, `XVIII` has the value 18 (`10 + 5 + 1 + 1 + 1 = 18`). | ||
|
|
||
| There's one rule that makes things trickier though, and that's that **the same letter cannot be used more than three times in succession**. | ||
| That means that we can't express numbers such as 4 with the seemingly natural `IIII`. | ||
| Instead, for those numbers, we use a subtraction method between two letters. | ||
| So we think of `4` not as `1 + 1 + 1 + 1` but instead as `5 - 1`. | ||
| And slightly confusingly to our modern thinking, we write the smaller number first. | ||
| This applies only in the following cases: 4 (`IV`), 9 (`IX`), 40 (`XL`), 90 (`XC`), 400 (`CD`) and 900 (`CM`). | ||
|
|
||
| Order matters in Roman numerals! | ||
| Letters (and the special compounds above) must be ordered by decreasing value from left to right. | ||
|
|
||
| Here are some examples: | ||
|
|
||
| ```text | ||
| 105 => CV | ||
| ---- => -- | ||
| 100 => C | ||
| + 5 => V | ||
| ``` | ||
|
|
||
| ```text | ||
| 106 => CVI | ||
| ---- => -- | ||
| 100 => C | ||
| + 5 => V | ||
| + 1 => I | ||
| ``` | ||
|
|
||
| ```text | ||
| 104 => CIV | ||
| ---- => --- | ||
| 100 => C | ||
| + 4 => IV | ||
| ``` | ||
|
|
||
| And a final more complex example: | ||
|
|
||
| ```text | ||
| 1996 => MCMXCVI | ||
| ----- => ------- | ||
| 1000 => M | ||
| + 900 => CM | ||
| + 90 => XC | ||
| + 5 => V | ||
| + 1 => I | ||
| ``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| node_modules | ||
| **/*.res.js |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| let roman = n => { | ||
| let rec aux = (s, remainder) => { | ||
| switch remainder { | ||
| | r if r >= 1000 => aux(s ++ "M", r - 1000) | ||
| | r if r >= 900 => aux(s ++ "CM", r - 900) | ||
| | r if r >= 500 => aux(s ++ "D", r - 500) | ||
| | r if r >= 400 => aux(s ++ "CD", r - 400) | ||
| | r if r >= 100 => aux(s ++ "C", r - 100) | ||
| | r if r >= 90 => aux(s ++ "XC", r - 90) | ||
| | r if r >= 50 => aux(s ++ "L", r - 50) | ||
| | r if r >= 40 => aux(s ++ "XL", r - 40) | ||
| | r if r >= 10 => aux(s ++ "X", r - 10) | ||
| | r if r >= 9 => aux(s ++ "IX", r - 9) | ||
| | r if r >= 5 => aux(s ++ "V", r - 5) | ||
| | r if r >= 4 => aux(s ++ "IV", r - 4) | ||
| | r if r >= 1 => aux(s ++ "I", r - 1) | ||
| | _ => s | ||
| } | ||
| } | ||
| aux("", n) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| let roman: int => string |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| { | ||
| "authors": [ | ||
| "therealowenrees" | ||
| ], | ||
| "files": { | ||
| "solution": [ | ||
| "src/RomanNumerals.res", | ||
| "src/RomanNumerals.resi" | ||
| ], | ||
| "test": [ | ||
| "tests/RomanNumerals_test.res" | ||
| ], | ||
| "example": [ | ||
| ".meta/RomanNumerals.res", | ||
| ".meta/RomanNumerals.resi" | ||
| ] | ||
| }, | ||
| "blurb": "Convert modern Arabic numbers into Roman numerals.", | ||
| "source": "The Roman Numeral Kata", | ||
| "source_url": "https://codingdojo.org/kata/RomanNumerals/" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| # This is an auto-generated file. | ||
| # | ||
| # Regenerating this file via `configlet sync` will: | ||
| # - Recreate every `description` key/value pair | ||
| # - Recreate every `reimplements` key/value pair, where they exist in problem-specifications | ||
| # - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion) | ||
| # - Preserve any other key/value pair | ||
| # | ||
| # As user-added comments (using the # character) will be removed when this file | ||
| # is regenerated, comments can be added via a `comment` key. | ||
|
|
||
| [19828a3a-fbf7-4661-8ddd-cbaeee0e2178] | ||
| description = "1 is I" | ||
|
|
||
| [f088f064-2d35-4476-9a41-f576da3f7b03] | ||
| description = "2 is II" | ||
|
|
||
| [b374a79c-3bea-43e6-8db8-1286f79c7106] | ||
| description = "3 is III" | ||
|
|
||
| [05a0a1d4-a140-4db1-82e8-fcc21fdb49bb] | ||
| description = "4 is IV" | ||
|
|
||
| [57c0f9ad-5024-46ab-975d-de18c430b290] | ||
| description = "5 is V" | ||
|
|
||
| [20a2b47f-e57f-4797-a541-0b3825d7f249] | ||
| description = "6 is VI" | ||
|
|
||
| [ff3fb08c-4917-4aab-9f4e-d663491d083d] | ||
| description = "9 is IX" | ||
|
|
||
| [6d1d82d5-bf3e-48af-9139-87d7165ed509] | ||
| description = "16 is XVI" | ||
|
|
||
| [2bda64ca-7d28-4c56-b08d-16ce65716cf6] | ||
| description = "27 is XXVII" | ||
|
|
||
| [a1f812ef-84da-4e02-b4f0-89c907d0962c] | ||
| description = "48 is XLVIII" | ||
|
|
||
| [607ead62-23d6-4c11-a396-ef821e2e5f75] | ||
| description = "49 is XLIX" | ||
|
|
||
| [d5b283d4-455d-4e68-aacf-add6c4b51915] | ||
| description = "59 is LIX" | ||
|
|
||
| [4465ffd5-34dc-44f3-ada5-56f5007b6dad] | ||
| description = "66 is LXVI" | ||
|
|
||
| [46b46e5b-24da-4180-bfe2-2ef30b39d0d0] | ||
| description = "93 is XCIII" | ||
|
|
||
| [30494be1-9afb-4f84-9d71-db9df18b55e3] | ||
| description = "141 is CXLI" | ||
|
|
||
| [267f0207-3c55-459a-b81d-67cec7a46ed9] | ||
| description = "163 is CLXIII" | ||
|
|
||
| [902ad132-0b4d-40e3-8597-ba5ed611dd8d] | ||
| description = "166 is CLXVI" | ||
|
|
||
| [cdb06885-4485-4d71-8bfb-c9d0f496b404] | ||
| description = "402 is CDII" | ||
|
|
||
| [6b71841d-13b2-46b4-ba97-dec28133ea80] | ||
| description = "575 is DLXXV" | ||
|
|
||
| [dacb84b9-ea1c-4a61-acbb-ce6b36674906] | ||
| description = "666 is DCLXVI" | ||
|
|
||
| [432de891-7fd6-4748-a7f6-156082eeca2f] | ||
| description = "911 is CMXI" | ||
|
|
||
| [e6de6d24-f668-41c0-88d7-889c0254d173] | ||
| description = "1024 is MXXIV" | ||
|
|
||
| [efbe1d6a-9f98-4eb5-82bc-72753e3ac328] | ||
| description = "1666 is MDCLXVI" | ||
|
|
||
| [bb550038-d4eb-4be2-a9ce-f21961ac3bc6] | ||
| description = "3000 is MMM" | ||
|
|
||
| [3bc4b41c-c2e6-49d9-9142-420691504336] | ||
| description = "3001 is MMMI" | ||
|
|
||
| [2f89cad7-73f6-4d1b-857b-0ef531f68b7e] | ||
| description = "3888 is MMMDCCCLXXXVIII" | ||
|
|
||
| [4e18e96b-5fbb-43df-a91b-9cb511fe0856] | ||
| description = "3999 is MMMCMXCIX" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| MIT License | ||
|
|
||
| Copyright (c) 2026 Exercism | ||
|
|
||
| Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| of this software and associated documentation files (the "Software"), to deal | ||
| in the Software without restriction, including without limitation the rights | ||
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| copies of the Software, and to permit persons to whom the Software is | ||
| furnished to do so, subject to the following conditions: | ||
|
|
||
| The above copyright notice and this permission notice shall be included in all | ||
| copies or substantial portions of the Software. | ||
|
|
||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| SOFTWARE. |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Entirely a modern invention but not worth updating the docs upstream. There are plenty of examples in classical times where this wasn’t true.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would you like me to make a change here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nah. If we touch it here, configlet will think our docs are out of date and flag it for a resync. We can add additional content via instructions or introductions append files that get appended to the end of the appropriate file. That’s more intended for track specific content though.