Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Sandbox

Post History

#20: Post edited by user avatar trichoplax‭ · 2024-03-11T17:56:14Z (9 months ago)
Mark as finalized
  • The 50 substrings that validate any string of Roman numerals
  • The 50 substrings that validate any string of Roman numerals [FINALIZED]
  • Given a string of Roman numerals, decide whether it forms a valid Roman number. If not, output the substring that proves this, from the list of 50 strings described below.
  • ## Relevant fact
  • This challenge is based around the following fact:
  • > A string of Roman numerals is a valid Roman number if and only if it contains none of the following 50 strings as a substring:
  • >
  • > ```text
  • > "CCCC", "CCD", "CCM", "CDC", "CMC", "CMD", "CMM", "DCD", "DCM", "DD", "DM", "IC", "ID", "IIII", "IIV", "IIX", "IL", "IM", "IVI", "IXC", "IXI", "IXL", "IXV", "IXX", "LC", "LD", "LL", "LM", "LXC", "LXL", "MMMM", "VC", "VD", "VIV", "VIX", "VL", "VM", "VV", "VX", "XCC", "XCD", "XCL", "XCM", "XCX", "XD", "XLX", "XM", "XXC", "XXL", "XXXX"
  • > ```
  • This works for any length string, if a valid Roman number is defined as follows:
  • ## Valid Roman numbers[^1]
  • - Each numeral appears no more than 3 times consecutively.
  • - Each of `V` (5), `L` (50), and `D` (500) appears no more than once consecutively.
  • - A Roman number is constructed by concatenating the strings representing its ***thousands***, ***hundreds***, ***tens***, and ***units*** components.
  • Decimal | Thousands | Hundreds | Tens | Units
  • - | - | - | - | -
  • 1 | M | C | X | I
  • 2 | MM | CC | XX | II
  • 3 | MMM | CCC | XXX | III
  • 4 | | CD | XL | IV
  • 5 | | D | L | V
  • 6 | | DC | LX | VI
  • 7 | | DCC | LXX | VII
  • 8 | | DCCC | LXXX | VIII
  • 9 | | CM | XC | IX
  • So, for example, 2345 would be represented as the concatenation of `MM` (for 2000), `CCC` (for 300), `XL` (for 40), and `V` (for 5), or `MMCCCXLV`.
  • This defines a unique correct representation for each number from 1 to 3999 (4000 and above are not representable without breaking the first two rules). The 50 substrings method described above will identify each of these 3999 strings as valid, and all other strings of Roman numerals as invalid.
  • ## Input
  • - A string containing only Roman numerals (`I`, `V`, `X`, `L`, `C`, `D`, `M`).
  • - The string will have length at least 1.
  • - The string will have length at most 15 (this is the length of the longest valid string of Roman numerals).
  • ## Output
  • - If the input is a valid Roman number, output a consistent value indicating this.
  • - Consistent means that the value must be the same for all valid Roman numbers.
  • - The output for a valid Roman number must not be one of the strings from the list of 50.
  • - If the input is not a valid Roman number, output exactly 1 string from the list of 50.
  • - The output in this case must be a substring of the input.
  • - If the input has 2 or more of the strings from the list of 50 as substrings, you may choose any 1 of them to be the output, but you must choose only 1 of them (you must not output 2 or more).
  • ## Examples
  • ### A valid Roman number
  • The input `MCMXCVI` is the unique correct representation of 1996. It contains none of the 50 strings.
  • ### A string that is not a valid Roman number
  • Although the input `MMXDIII` might be suspected of representing 2493, it is not the unique correct representation of this number (which is `MMCDXCIII`). Note that it has `XD` as a substring, identifying it as invalid. The only correct output is therefore `XD`.
  • ### An invalid string with more than 1 potential output
  • The input `MMCCMDXXV` has 2 substrings that make it invalid, so either `CCM` or `CMD` would be correct outputs. It would not be correct to output both of these, or to output their overlap `CCMD`, as this is not one of the 50 strings.
  • ## Test cases
  • Test cases are in the format `INPUT : VALID, OUTPUTS`. Note that only one of the valid outputs can be chosen - outputting 2 or more is incorrect.
  • ```text
  • I : VALID
  • V : VALID
  • X : VALID
  • L : VALID
  • C : VALID
  • D : VALID
  • M : VALID
  • II : VALID
  • VV : VV
  • XX : VALID
  • LL : LL
  • CC : VALID
  • DD : DD
  • MM : VALID
  • III : VALID
  • VII : VALID
  • IVI : IVI
  • IIV : IIV
  • CCI : VALID
  • CCV : VALID
  • CCX : VALID
  • CCL : VALID
  • CCC : VALID
  • CCD : CCD
  • CCM : CCM
  • IIII : IIII
  • MLDI : LD
  • MXXC : XXC
  • DCIIX : IIX
  • MCXXXX : XXXX
  • MCCCCXVI : CCCC
  • MMLXCVII : LXC
  • MMMCMXCIX : VALID
  • MMMDCCCLXXXVIII : VALID
  • MMCCCXLV : VALID
  • MCMXCVI : VALID
  • MMXDIII : XD
  • MMCDXCIII : VALID
  • MMCCMDXXV : CCM, CMD
  • XXX : VALID
  • CLLX : LL
  • DXXDMMV : DM, XD
  • CCDDDIMDD : DD, IM, CCD
  • VLCXIVXMCVXLC : VX, VL, LC, XM
  • DVLIILVCXVXVMLI : VX, VL, IL, VM, VC
  • VVDLMIVILXXDX : VV, VD, IL, XD, LM, IVI
  • DMXXCMILVCMLLMV : DM, IL, LL, VC, LM, XXC, XCM
  • CMXDVLCCDDLXLXC : DD, VL, LC, XD, XLX, LXC, CCD, LXL
  • XDIXCLLMVVLCMCM : VV, VL, LC, XD, LL, LM, IXC, CMC, XCL
  • IIXXCDVVLMILVDD : DD, VV, VL, VD, IL, LM, XXC, IXX, IIX, XCD
  • DDMIIXXCMCCDCMM : DD, DM, CMC, CMM, XXC, IXX, DCM, CDC, XCM, CCD, IIX
  • ```
  • ## Scoring
  • This is a [code golf challenge]. Your score is the number of bytes in your code. Lowest score for each language wins.
  • > Explanations are optional, but I'm more likely to upvote answers that have one.
  • [code golf challenge]: https://codegolf.codidact.com/categories/49/tags/4274 "The code-golf tag"
  • [^1]: This is a common modern set of rules, described as [Standard form](https://en.wikipedia.org/wiki/Roman_numerals#Standard_form) on Wikipedia. It does not reflect all usages during history, but will be the basis of this challenge, since otherwise the 50 substrings approach does not work.
  • ### Now posted: [The 50 substrings that validate any string of Roman numerals](https://codegolf.codidact.com/posts/291048)
  • ---
  • Given a string of Roman numerals, decide whether it forms a valid Roman number. If not, output the substring that proves this, from the list of 50 strings described below.
  • ## Relevant fact
  • This challenge is based around the following fact:
  • > A string of Roman numerals is a valid Roman number if and only if it contains none of the following 50 strings as a substring:
  • >
  • > ```text
  • > "CCCC", "CCD", "CCM", "CDC", "CMC", "CMD", "CMM", "DCD", "DCM", "DD", "DM", "IC", "ID", "IIII", "IIV", "IIX", "IL", "IM", "IVI", "IXC", "IXI", "IXL", "IXV", "IXX", "LC", "LD", "LL", "LM", "LXC", "LXL", "MMMM", "VC", "VD", "VIV", "VIX", "VL", "VM", "VV", "VX", "XCC", "XCD", "XCL", "XCM", "XCX", "XD", "XLX", "XM", "XXC", "XXL", "XXXX"
  • > ```
  • This works for any length string, if a valid Roman number is defined as follows:
  • ## Valid Roman numbers[^1]
  • - Each numeral appears no more than 3 times consecutively.
  • - Each of `V` (5), `L` (50), and `D` (500) appears no more than once consecutively.
  • - A Roman number is constructed by concatenating the strings representing its ***thousands***, ***hundreds***, ***tens***, and ***units*** components.
  • Decimal | Thousands | Hundreds | Tens | Units
  • - | - | - | - | -
  • 1 | M | C | X | I
  • 2 | MM | CC | XX | II
  • 3 | MMM | CCC | XXX | III
  • 4 | | CD | XL | IV
  • 5 | | D | L | V
  • 6 | | DC | LX | VI
  • 7 | | DCC | LXX | VII
  • 8 | | DCCC | LXXX | VIII
  • 9 | | CM | XC | IX
  • So, for example, 2345 would be represented as the concatenation of `MM` (for 2000), `CCC` (for 300), `XL` (for 40), and `V` (for 5), or `MMCCCXLV`.
  • This defines a unique correct representation for each number from 1 to 3999 (4000 and above are not representable without breaking the first two rules). The 50 substrings method described above will identify each of these 3999 strings as valid, and all other strings of Roman numerals as invalid.
  • ## Input
  • - A string containing only Roman numerals (`I`, `V`, `X`, `L`, `C`, `D`, `M`).
  • - The string will have length at least 1.
  • - The string will have length at most 15 (this is the length of the longest valid string of Roman numerals).
  • ## Output
  • - If the input is a valid Roman number, output a consistent value indicating this.
  • - Consistent means that the value must be the same for all valid Roman numbers.
  • - The output for a valid Roman number must not be one of the strings from the list of 50.
  • - If the input is not a valid Roman number, output exactly 1 string from the list of 50.
  • - The output in this case must be a substring of the input.
  • - If the input has 2 or more of the strings from the list of 50 as substrings, you may choose any 1 of them to be the output, but you must choose only 1 of them (you must not output 2 or more).
  • ## Examples
  • ### A valid Roman number
  • The input `MCMXCVI` is the unique correct representation of 1996. It contains none of the 50 strings.
  • ### A string that is not a valid Roman number
  • Although the input `MMXDIII` might be suspected of representing 2493, it is not the unique correct representation of this number (which is `MMCDXCIII`). Note that it has `XD` as a substring, identifying it as invalid. The only correct output is therefore `XD`.
  • ### An invalid string with more than 1 potential output
  • The input `MMCCMDXXV` has 2 substrings that make it invalid, so either `CCM` or `CMD` would be correct outputs. It would not be correct to output both of these, or to output their overlap `CCMD`, as this is not one of the 50 strings.
  • ## Test cases
  • Test cases are in the format `INPUT : VALID, OUTPUTS`. Note that only one of the valid outputs can be chosen - outputting 2 or more is incorrect.
  • ```text
  • I : VALID
  • V : VALID
  • X : VALID
  • L : VALID
  • C : VALID
  • D : VALID
  • M : VALID
  • II : VALID
  • VV : VV
  • XX : VALID
  • LL : LL
  • CC : VALID
  • DD : DD
  • MM : VALID
  • III : VALID
  • VII : VALID
  • IVI : IVI
  • IIV : IIV
  • CCI : VALID
  • CCV : VALID
  • CCX : VALID
  • CCL : VALID
  • CCC : VALID
  • CCD : CCD
  • CCM : CCM
  • IIII : IIII
  • MLDI : LD
  • MXXC : XXC
  • DCIIX : IIX
  • MCXXXX : XXXX
  • MCCCCXVI : CCCC
  • MMLXCVII : LXC
  • MMMCMXCIX : VALID
  • MMMDCCCLXXXVIII : VALID
  • MMCCCXLV : VALID
  • MCMXCVI : VALID
  • MMXDIII : XD
  • MMCDXCIII : VALID
  • MMCCMDXXV : CCM, CMD
  • XXX : VALID
  • CLLX : LL
  • DXXDMMV : DM, XD
  • CCDDDIMDD : DD, IM, CCD
  • VLCXIVXMCVXLC : VX, VL, LC, XM
  • DVLIILVCXVXVMLI : VX, VL, IL, VM, VC
  • VVDLMIVILXXDX : VV, VD, IL, XD, LM, IVI
  • DMXXCMILVCMLLMV : DM, IL, LL, VC, LM, XXC, XCM
  • CMXDVLCCDDLXLXC : DD, VL, LC, XD, XLX, LXC, CCD, LXL
  • XDIXCLLMVVLCMCM : VV, VL, LC, XD, LL, LM, IXC, CMC, XCL
  • IIXXCDVVLMILVDD : DD, VV, VL, VD, IL, LM, XXC, IXX, IIX, XCD
  • DDMIIXXCMCCDCMM : DD, DM, CMC, CMM, XXC, IXX, DCM, CDC, XCM, CCD, IIX
  • ```
  • ## Scoring
  • This is a [code golf challenge]. Your score is the number of bytes in your code. Lowest score for each language wins.
  • > Explanations are optional, but I'm more likely to upvote answers that have one.
  • [code golf challenge]: https://codegolf.codidact.com/categories/49/tags/4274 "The code-golf tag"
  • [^1]: This is a common modern set of rules, described as [Standard form](https://en.wikipedia.org/wiki/Roman_numerals#Standard_form) on Wikipedia. It does not reflect all usages during history, but will be the basis of this challenge, since otherwise the 50 substrings approach does not work.
#19: Post edited by user avatar trichoplax‭ · 2024-03-11T17:49:17Z (9 months ago)
Clarify output
  • Given a string of Roman numerals, decide whether it forms a valid Roman number. If not, output the substring that proves this, from the list of 50 strings described below.
  • ## Relevant fact
  • This challenge is based around the following fact:
  • > A string of Roman numerals is a valid Roman number if and only if it contains none of the following 50 strings as a substring:
  • >
  • > ```text
  • > "CCCC", "CCD", "CCM", "CDC", "CMC", "CMD", "CMM", "DCD", "DCM", "DD", "DM", "IC", "ID", "IIII", "IIV", "IIX", "IL", "IM", "IVI", "IXC", "IXI", "IXL", "IXV", "IXX", "LC", "LD", "LL", "LM", "LXC", "LXL", "MMMM", "VC", "VD", "VIV", "VIX", "VL", "VM", "VV", "VX", "XCC", "XCD", "XCL", "XCM", "XCX", "XD", "XLX", "XM", "XXC", "XXL", "XXXX"
  • > ```
  • This works for any length string, if a valid Roman number is defined as follows:
  • ## Valid Roman numbers[^1]
  • - Each numeral appears no more than 3 times consecutively.
  • - Each of `V` (5), `L` (50), and `D` (500) appears no more than once consecutively.
  • - A Roman number is constructed by concatenating the strings representing its ***thousands***, ***hundreds***, ***tens***, and ***units*** components.
  • Decimal | Thousands | Hundreds | Tens | Units
  • - | - | - | - | -
  • 1 | M | C | X | I
  • 2 | MM | CC | XX | II
  • 3 | MMM | CCC | XXX | III
  • 4 | | CD | XL | IV
  • 5 | | D | L | V
  • 6 | | DC | LX | VI
  • 7 | | DCC | LXX | VII
  • 8 | | DCCC | LXXX | VIII
  • 9 | | CM | XC | IX
  • So, for example, 2345 would be represented as the concatenation of `MM` (for 2000), `CCC` (for 300), `XL` (for 40), and `V` (for 5), or `MMCCCXLV`.
  • This defines a unique correct representation for each number from 1 to 3999 (4000 and above are not representable without breaking the first two rules). The 50 substrings method described above will identify each of these 3999 strings as valid, and all other strings of Roman numerals as invalid.
  • ## Input
  • - A string containing only Roman numerals (`I`, `V`, `X`, `L`, `C`, `D`, `M`).
  • - The string will have length at least 1.
  • - The string will have length at most 15 (this is the length of the longest valid string of Roman numerals).
  • ## Output
  • - If the input is a valid Roman number, output a consistent value indicating this.
  • - Consistent means that the value must be the same for all valid Roman numbers.
  • - The output for a valid Roman number must not be one of the strings from the list of 50.
  • - If the input is not a valid Roman number, output exactly 1 string from the list of 50.
  • - The output must be a substring of the input.
  • - If the input has 2 or more of the strings from the list of 50 as substrings, you may choose any 1 of them to be the output, but you must choose only 1 of them (you must not output 2 or more).
  • ## Examples
  • ### A valid Roman number
  • The input `MCMXCVI` is the unique correct representation of 1996. It contains none of the 50 strings.
  • ### A string that is not a valid Roman number
  • Although the input `MMXDIII` might be suspected of representing 2493, it is not the unique correct representation of this number (which is `MMCDXCIII`). Note that it has `XD` as a substring, identifying it as invalid. The only correct output is therefore `XD`.
  • ### An invalid string with more than 1 potential output
  • The input `MMCCMDXXV` has 2 substrings that make it invalid, so either `CCM` or `CMD` would be correct outputs. It would not be correct to output both of these, or to output their overlap `CCMD`, as this is not one of the 50 strings.
  • ## Test cases
  • Test cases are in the format `INPUT : VALID, OUTPUTS`. Note that only one of the valid outputs can be chosen - outputting 2 or more is incorrect.
  • ```text
  • I : VALID
  • V : VALID
  • X : VALID
  • L : VALID
  • C : VALID
  • D : VALID
  • M : VALID
  • II : VALID
  • VV : VV
  • XX : VALID
  • LL : LL
  • CC : VALID
  • DD : DD
  • MM : VALID
  • III : VALID
  • VII : VALID
  • IVI : IVI
  • IIV : IIV
  • CCI : VALID
  • CCV : VALID
  • CCX : VALID
  • CCL : VALID
  • CCC : VALID
  • CCD : CCD
  • CCM : CCM
  • IIII : IIII
  • MLDI : LD
  • MXXC : XXC
  • DCIIX : IIX
  • MCXXXX : XXXX
  • MCCCCXVI : CCCC
  • MMLXCVII : LXC
  • MMMCMXCIX : VALID
  • MMMDCCCLXXXVIII : VALID
  • MMCCCXLV : VALID
  • MCMXCVI : VALID
  • MMXDIII : XD
  • MMCDXCIII : VALID
  • MMCCMDXXV : CCM, CMD
  • XXX : VALID
  • CLLX : LL
  • DXXDMMV : DM, XD
  • CCDDDIMDD : DD, IM, CCD
  • VLCXIVXMCVXLC : VX, VL, LC, XM
  • DVLIILVCXVXVMLI : VX, VL, IL, VM, VC
  • VVDLMIVILXXDX : VV, VD, IL, XD, LM, IVI
  • DMXXCMILVCMLLMV : DM, IL, LL, VC, LM, XXC, XCM
  • CMXDVLCCDDLXLXC : DD, VL, LC, XD, XLX, LXC, CCD, LXL
  • XDIXCLLMVVLCMCM : VV, VL, LC, XD, LL, LM, IXC, CMC, XCL
  • IIXXCDVVLMILVDD : DD, VV, VL, VD, IL, LM, XXC, IXX, IIX, XCD
  • DDMIIXXCMCCDCMM : DD, DM, CMC, CMM, XXC, IXX, DCM, CDC, XCM, CCD, IIX
  • ```
  • ## Scoring
  • This is a [code golf challenge]. Your score is the number of bytes in your code. Lowest score for each language wins.
  • > Explanations are optional, but I'm more likely to upvote answers that have one.
  • [code golf challenge]: https://codegolf.codidact.com/categories/49/tags/4274 "The code-golf tag"
  • [^1]: This is a common modern set of rules, described as [Standard form](https://en.wikipedia.org/wiki/Roman_numerals#Standard_form) on Wikipedia. It does not reflect all usages during history, but will be the basis of this challenge, since otherwise the 50 substrings approach does not work.
  • Given a string of Roman numerals, decide whether it forms a valid Roman number. If not, output the substring that proves this, from the list of 50 strings described below.
  • ## Relevant fact
  • This challenge is based around the following fact:
  • > A string of Roman numerals is a valid Roman number if and only if it contains none of the following 50 strings as a substring:
  • >
  • > ```text
  • > "CCCC", "CCD", "CCM", "CDC", "CMC", "CMD", "CMM", "DCD", "DCM", "DD", "DM", "IC", "ID", "IIII", "IIV", "IIX", "IL", "IM", "IVI", "IXC", "IXI", "IXL", "IXV", "IXX", "LC", "LD", "LL", "LM", "LXC", "LXL", "MMMM", "VC", "VD", "VIV", "VIX", "VL", "VM", "VV", "VX", "XCC", "XCD", "XCL", "XCM", "XCX", "XD", "XLX", "XM", "XXC", "XXL", "XXXX"
  • > ```
  • This works for any length string, if a valid Roman number is defined as follows:
  • ## Valid Roman numbers[^1]
  • - Each numeral appears no more than 3 times consecutively.
  • - Each of `V` (5), `L` (50), and `D` (500) appears no more than once consecutively.
  • - A Roman number is constructed by concatenating the strings representing its ***thousands***, ***hundreds***, ***tens***, and ***units*** components.
  • Decimal | Thousands | Hundreds | Tens | Units
  • - | - | - | - | -
  • 1 | M | C | X | I
  • 2 | MM | CC | XX | II
  • 3 | MMM | CCC | XXX | III
  • 4 | | CD | XL | IV
  • 5 | | D | L | V
  • 6 | | DC | LX | VI
  • 7 | | DCC | LXX | VII
  • 8 | | DCCC | LXXX | VIII
  • 9 | | CM | XC | IX
  • So, for example, 2345 would be represented as the concatenation of `MM` (for 2000), `CCC` (for 300), `XL` (for 40), and `V` (for 5), or `MMCCCXLV`.
  • This defines a unique correct representation for each number from 1 to 3999 (4000 and above are not representable without breaking the first two rules). The 50 substrings method described above will identify each of these 3999 strings as valid, and all other strings of Roman numerals as invalid.
  • ## Input
  • - A string containing only Roman numerals (`I`, `V`, `X`, `L`, `C`, `D`, `M`).
  • - The string will have length at least 1.
  • - The string will have length at most 15 (this is the length of the longest valid string of Roman numerals).
  • ## Output
  • - If the input is a valid Roman number, output a consistent value indicating this.
  • - Consistent means that the value must be the same for all valid Roman numbers.
  • - The output for a valid Roman number must not be one of the strings from the list of 50.
  • - If the input is not a valid Roman number, output exactly 1 string from the list of 50.
  • - The output in this case must be a substring of the input.
  • - If the input has 2 or more of the strings from the list of 50 as substrings, you may choose any 1 of them to be the output, but you must choose only 1 of them (you must not output 2 or more).
  • ## Examples
  • ### A valid Roman number
  • The input `MCMXCVI` is the unique correct representation of 1996. It contains none of the 50 strings.
  • ### A string that is not a valid Roman number
  • Although the input `MMXDIII` might be suspected of representing 2493, it is not the unique correct representation of this number (which is `MMCDXCIII`). Note that it has `XD` as a substring, identifying it as invalid. The only correct output is therefore `XD`.
  • ### An invalid string with more than 1 potential output
  • The input `MMCCMDXXV` has 2 substrings that make it invalid, so either `CCM` or `CMD` would be correct outputs. It would not be correct to output both of these, or to output their overlap `CCMD`, as this is not one of the 50 strings.
  • ## Test cases
  • Test cases are in the format `INPUT : VALID, OUTPUTS`. Note that only one of the valid outputs can be chosen - outputting 2 or more is incorrect.
  • ```text
  • I : VALID
  • V : VALID
  • X : VALID
  • L : VALID
  • C : VALID
  • D : VALID
  • M : VALID
  • II : VALID
  • VV : VV
  • XX : VALID
  • LL : LL
  • CC : VALID
  • DD : DD
  • MM : VALID
  • III : VALID
  • VII : VALID
  • IVI : IVI
  • IIV : IIV
  • CCI : VALID
  • CCV : VALID
  • CCX : VALID
  • CCL : VALID
  • CCC : VALID
  • CCD : CCD
  • CCM : CCM
  • IIII : IIII
  • MLDI : LD
  • MXXC : XXC
  • DCIIX : IIX
  • MCXXXX : XXXX
  • MCCCCXVI : CCCC
  • MMLXCVII : LXC
  • MMMCMXCIX : VALID
  • MMMDCCCLXXXVIII : VALID
  • MMCCCXLV : VALID
  • MCMXCVI : VALID
  • MMXDIII : XD
  • MMCDXCIII : VALID
  • MMCCMDXXV : CCM, CMD
  • XXX : VALID
  • CLLX : LL
  • DXXDMMV : DM, XD
  • CCDDDIMDD : DD, IM, CCD
  • VLCXIVXMCVXLC : VX, VL, LC, XM
  • DVLIILVCXVXVMLI : VX, VL, IL, VM, VC
  • VVDLMIVILXXDX : VV, VD, IL, XD, LM, IVI
  • DMXXCMILVCMLLMV : DM, IL, LL, VC, LM, XXC, XCM
  • CMXDVLCCDDLXLXC : DD, VL, LC, XD, XLX, LXC, CCD, LXL
  • XDIXCLLMVVLCMCM : VV, VL, LC, XD, LL, LM, IXC, CMC, XCL
  • IIXXCDVVLMILVDD : DD, VV, VL, VD, IL, LM, XXC, IXX, IIX, XCD
  • DDMIIXXCMCCDCMM : DD, DM, CMC, CMM, XXC, IXX, DCM, CDC, XCM, CCD, IIX
  • ```
  • ## Scoring
  • This is a [code golf challenge]. Your score is the number of bytes in your code. Lowest score for each language wins.
  • > Explanations are optional, but I'm more likely to upvote answers that have one.
  • [code golf challenge]: https://codegolf.codidact.com/categories/49/tags/4274 "The code-golf tag"
  • [^1]: This is a common modern set of rules, described as [Standard form](https://en.wikipedia.org/wiki/Roman_numerals#Standard_form) on Wikipedia. It does not reflect all usages during history, but will be the basis of this challenge, since otherwise the 50 substrings approach does not work.
#18: Post edited by user avatar trichoplax‭ · 2024-03-11T17:44:44Z (9 months ago)
Clarify reason for maximum representable value of 3999
  • Given a string of Roman numerals, decide whether it forms a valid Roman number. If not, output the substring that proves this, from the list of 50 strings described below.
  • ## Relevant fact
  • This challenge is based around the following fact:
  • > A string of Roman numerals is a valid Roman number if and only if it contains none of the following 50 strings as a substring:
  • >
  • > ```text
  • > "CCCC", "CCD", "CCM", "CDC", "CMC", "CMD", "CMM", "DCD", "DCM", "DD", "DM", "IC", "ID", "IIII", "IIV", "IIX", "IL", "IM", "IVI", "IXC", "IXI", "IXL", "IXV", "IXX", "LC", "LD", "LL", "LM", "LXC", "LXL", "MMMM", "VC", "VD", "VIV", "VIX", "VL", "VM", "VV", "VX", "XCC", "XCD", "XCL", "XCM", "XCX", "XD", "XLX", "XM", "XXC", "XXL", "XXXX"
  • > ```
  • This works for any length string, if a valid Roman number is defined as follows:
  • ## Valid Roman numbers[^1]
  • - Each numeral appears no more than 3 times consecutively.
  • - Each of `V` (5), `L` (50), and `D` (500) appears no more than once consecutively.
  • - A Roman number is constructed by concatenating the strings representing its ***thousands***, ***hundreds***, ***tens***, and ***units*** components.
  • Decimal | Thousands | Hundreds | Tens | Units
  • - | - | - | - | -
  • 1 | M | C | X | I
  • 2 | MM | CC | XX | II
  • 3 | MMM | CCC | XXX | III
  • 4 | | CD | XL | IV
  • 5 | | D | L | V
  • 6 | | DC | LX | VI
  • 7 | | DCC | LXX | VII
  • 8 | | DCCC | LXXX | VIII
  • 9 | | CM | XC | IX
  • So, for example, 2345 would be represented as the concatenation of `MM` (for 2000), `CCC` (for 300), `XL` (for 40), and `V` (for 5), or `MMCCCXLV`.
  • This defines a unique correct representation for each number from 1 to 3999 (4000 and above are not representable without breaking the rule that a numeral cannot appear more than 3 times consecutively). The 50 substrings method described above will identify each of these 3999 strings as valid, and all other strings of Roman numerals as invalid.
  • ## Input
  • - A string containing only Roman numerals (`I`, `V`, `X`, `L`, `C`, `D`, `M`).
  • - The string will have length at least 1.
  • - The string will have length at most 15 (this is the length of the longest valid string of Roman numerals).
  • ## Output
  • - If the input is a valid Roman number, output a consistent value indicating this.
  • - Consistent means that the value must be the same for all valid Roman numbers.
  • - The output for a valid Roman number must not be one of the strings from the list of 50.
  • - If the input is not a valid Roman number, output exactly 1 string from the list of 50.
  • - The output must be a substring of the input.
  • - If the input has 2 or more of the strings from the list of 50 as substrings, you may choose any 1 of them to be the output, but you must choose only 1 of them (you must not output 2 or more).
  • ## Examples
  • ### A valid Roman number
  • The input `MCMXCVI` is the unique correct representation of 1996. It contains none of the 50 strings.
  • ### A string that is not a valid Roman number
  • Although the input `MMXDIII` might be suspected of representing 2493, it is not the unique correct representation of this number (which is `MMCDXCIII`). Note that it has `XD` as a substring, identifying it as invalid. The only correct output is therefore `XD`.
  • ### An invalid string with more than 1 potential output
  • The input `MMCCMDXXV` has 2 substrings that make it invalid, so either `CCM` or `CMD` would be correct outputs. It would not be correct to output both of these, or to output their overlap `CCMD`, as this is not one of the 50 strings.
  • ## Test cases
  • Test cases are in the format `INPUT : VALID, OUTPUTS`. Note that only one of the valid outputs can be chosen - outputting 2 or more is incorrect.
  • ```text
  • I : VALID
  • V : VALID
  • X : VALID
  • L : VALID
  • C : VALID
  • D : VALID
  • M : VALID
  • II : VALID
  • VV : VV
  • XX : VALID
  • LL : LL
  • CC : VALID
  • DD : DD
  • MM : VALID
  • III : VALID
  • VII : VALID
  • IVI : IVI
  • IIV : IIV
  • CCI : VALID
  • CCV : VALID
  • CCX : VALID
  • CCL : VALID
  • CCC : VALID
  • CCD : CCD
  • CCM : CCM
  • IIII : IIII
  • MLDI : LD
  • MXXC : XXC
  • DCIIX : IIX
  • MCXXXX : XXXX
  • MCCCCXVI : CCCC
  • MMLXCVII : LXC
  • MMMCMXCIX : VALID
  • MMMDCCCLXXXVIII : VALID
  • MMCCCXLV : VALID
  • MCMXCVI : VALID
  • MMXDIII : XD
  • MMCDXCIII : VALID
  • MMCCMDXXV : CCM, CMD
  • XXX : VALID
  • CLLX : LL
  • DXXDMMV : DM, XD
  • CCDDDIMDD : DD, IM, CCD
  • VLCXIVXMCVXLC : VX, VL, LC, XM
  • DVLIILVCXVXVMLI : VX, VL, IL, VM, VC
  • VVDLMIVILXXDX : VV, VD, IL, XD, LM, IVI
  • DMXXCMILVCMLLMV : DM, IL, LL, VC, LM, XXC, XCM
  • CMXDVLCCDDLXLXC : DD, VL, LC, XD, XLX, LXC, CCD, LXL
  • XDIXCLLMVVLCMCM : VV, VL, LC, XD, LL, LM, IXC, CMC, XCL
  • IIXXCDVVLMILVDD : DD, VV, VL, VD, IL, LM, XXC, IXX, IIX, XCD
  • DDMIIXXCMCCDCMM : DD, DM, CMC, CMM, XXC, IXX, DCM, CDC, XCM, CCD, IIX
  • ```
  • ## Scoring
  • This is a [code golf challenge]. Your score is the number of bytes in your code. Lowest score for each language wins.
  • > Explanations are optional, but I'm more likely to upvote answers that have one.
  • [code golf challenge]: https://codegolf.codidact.com/categories/49/tags/4274 "The code-golf tag"
  • [^1]: This is a common modern set of rules, described as [Standard form](https://en.wikipedia.org/wiki/Roman_numerals#Standard_form) on Wikipedia. It does not reflect all usages during history, but will be the basis of this challenge, since otherwise the 50 substrings approach does not work.
  • Given a string of Roman numerals, decide whether it forms a valid Roman number. If not, output the substring that proves this, from the list of 50 strings described below.
  • ## Relevant fact
  • This challenge is based around the following fact:
  • > A string of Roman numerals is a valid Roman number if and only if it contains none of the following 50 strings as a substring:
  • >
  • > ```text
  • > "CCCC", "CCD", "CCM", "CDC", "CMC", "CMD", "CMM", "DCD", "DCM", "DD", "DM", "IC", "ID", "IIII", "IIV", "IIX", "IL", "IM", "IVI", "IXC", "IXI", "IXL", "IXV", "IXX", "LC", "LD", "LL", "LM", "LXC", "LXL", "MMMM", "VC", "VD", "VIV", "VIX", "VL", "VM", "VV", "VX", "XCC", "XCD", "XCL", "XCM", "XCX", "XD", "XLX", "XM", "XXC", "XXL", "XXXX"
  • > ```
  • This works for any length string, if a valid Roman number is defined as follows:
  • ## Valid Roman numbers[^1]
  • - Each numeral appears no more than 3 times consecutively.
  • - Each of `V` (5), `L` (50), and `D` (500) appears no more than once consecutively.
  • - A Roman number is constructed by concatenating the strings representing its ***thousands***, ***hundreds***, ***tens***, and ***units*** components.
  • Decimal | Thousands | Hundreds | Tens | Units
  • - | - | - | - | -
  • 1 | M | C | X | I
  • 2 | MM | CC | XX | II
  • 3 | MMM | CCC | XXX | III
  • 4 | | CD | XL | IV
  • 5 | | D | L | V
  • 6 | | DC | LX | VI
  • 7 | | DCC | LXX | VII
  • 8 | | DCCC | LXXX | VIII
  • 9 | | CM | XC | IX
  • So, for example, 2345 would be represented as the concatenation of `MM` (for 2000), `CCC` (for 300), `XL` (for 40), and `V` (for 5), or `MMCCCXLV`.
  • This defines a unique correct representation for each number from 1 to 3999 (4000 and above are not representable without breaking the first two rules). The 50 substrings method described above will identify each of these 3999 strings as valid, and all other strings of Roman numerals as invalid.
  • ## Input
  • - A string containing only Roman numerals (`I`, `V`, `X`, `L`, `C`, `D`, `M`).
  • - The string will have length at least 1.
  • - The string will have length at most 15 (this is the length of the longest valid string of Roman numerals).
  • ## Output
  • - If the input is a valid Roman number, output a consistent value indicating this.
  • - Consistent means that the value must be the same for all valid Roman numbers.
  • - The output for a valid Roman number must not be one of the strings from the list of 50.
  • - If the input is not a valid Roman number, output exactly 1 string from the list of 50.
  • - The output must be a substring of the input.
  • - If the input has 2 or more of the strings from the list of 50 as substrings, you may choose any 1 of them to be the output, but you must choose only 1 of them (you must not output 2 or more).
  • ## Examples
  • ### A valid Roman number
  • The input `MCMXCVI` is the unique correct representation of 1996. It contains none of the 50 strings.
  • ### A string that is not a valid Roman number
  • Although the input `MMXDIII` might be suspected of representing 2493, it is not the unique correct representation of this number (which is `MMCDXCIII`). Note that it has `XD` as a substring, identifying it as invalid. The only correct output is therefore `XD`.
  • ### An invalid string with more than 1 potential output
  • The input `MMCCMDXXV` has 2 substrings that make it invalid, so either `CCM` or `CMD` would be correct outputs. It would not be correct to output both of these, or to output their overlap `CCMD`, as this is not one of the 50 strings.
  • ## Test cases
  • Test cases are in the format `INPUT : VALID, OUTPUTS`. Note that only one of the valid outputs can be chosen - outputting 2 or more is incorrect.
  • ```text
  • I : VALID
  • V : VALID
  • X : VALID
  • L : VALID
  • C : VALID
  • D : VALID
  • M : VALID
  • II : VALID
  • VV : VV
  • XX : VALID
  • LL : LL
  • CC : VALID
  • DD : DD
  • MM : VALID
  • III : VALID
  • VII : VALID
  • IVI : IVI
  • IIV : IIV
  • CCI : VALID
  • CCV : VALID
  • CCX : VALID
  • CCL : VALID
  • CCC : VALID
  • CCD : CCD
  • CCM : CCM
  • IIII : IIII
  • MLDI : LD
  • MXXC : XXC
  • DCIIX : IIX
  • MCXXXX : XXXX
  • MCCCCXVI : CCCC
  • MMLXCVII : LXC
  • MMMCMXCIX : VALID
  • MMMDCCCLXXXVIII : VALID
  • MMCCCXLV : VALID
  • MCMXCVI : VALID
  • MMXDIII : XD
  • MMCDXCIII : VALID
  • MMCCMDXXV : CCM, CMD
  • XXX : VALID
  • CLLX : LL
  • DXXDMMV : DM, XD
  • CCDDDIMDD : DD, IM, CCD
  • VLCXIVXMCVXLC : VX, VL, LC, XM
  • DVLIILVCXVXVMLI : VX, VL, IL, VM, VC
  • VVDLMIVILXXDX : VV, VD, IL, XD, LM, IVI
  • DMXXCMILVCMLLMV : DM, IL, LL, VC, LM, XXC, XCM
  • CMXDVLCCDDLXLXC : DD, VL, LC, XD, XLX, LXC, CCD, LXL
  • XDIXCLLMVVLCMCM : VV, VL, LC, XD, LL, LM, IXC, CMC, XCL
  • IIXXCDVVLMILVDD : DD, VV, VL, VD, IL, LM, XXC, IXX, IIX, XCD
  • DDMIIXXCMCCDCMM : DD, DM, CMC, CMM, XXC, IXX, DCM, CDC, XCM, CCD, IIX
  • ```
  • ## Scoring
  • This is a [code golf challenge]. Your score is the number of bytes in your code. Lowest score for each language wins.
  • > Explanations are optional, but I'm more likely to upvote answers that have one.
  • [code golf challenge]: https://codegolf.codidact.com/categories/49/tags/4274 "The code-golf tag"
  • [^1]: This is a common modern set of rules, described as [Standard form](https://en.wikipedia.org/wiki/Roman_numerals#Standard_form) on Wikipedia. It does not reflect all usages during history, but will be the basis of this challenge, since otherwise the 50 substrings approach does not work.
#17: Post edited by user avatar trichoplax‭ · 2024-03-11T17:38:18Z (9 months ago)
Condense power of 10 columns into a single table
  • Given a string of Roman numerals, decide whether it forms a valid Roman number. If not, output the substring that proves this, from the list of 50 strings described below.
  • ## Relevant fact
  • This challenge is based around the following fact:
  • > A string of Roman numerals is a valid Roman number if and only if it contains none of the following 50 strings as a substring:
  • >
  • > ```text
  • > "CCCC", "CCD", "CCM", "CDC", "CMC", "CMD", "CMM", "DCD", "DCM", "DD", "DM", "IC", "ID", "IIII", "IIV", "IIX", "IL", "IM", "IVI", "IXC", "IXI", "IXL", "IXV", "IXX", "LC", "LD", "LL", "LM", "LXC", "LXL", "MMMM", "VC", "VD", "VIV", "VIX", "VL", "VM", "VV", "VX", "XCC", "XCD", "XCL", "XCM", "XCX", "XD", "XLX", "XM", "XXC", "XXL", "XXXX"
  • > ```
  • This works for any length string, if a valid Roman number is defined as follows:
  • ## Valid Roman numbers[^1]
  • - Each numeral appears no more than 3 times consecutively.
  • - Each of `V` (5), `L` (50), and `D` (500) appears no more than once consecutively.
  • - A Roman number is constructed by concatenating the strings representing its ***thousands***, ***hundreds***, ***tens***, and ***units*** components.
  • ### Thousands
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 1000 | "M"
  • 2000 | "MM"
  • 3000 | "MMM"
  • ### Hundreds
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 100 | "C"
  • 200 | "CC"
  • 300 | "CCC"
  • 400 | "CD"
  • 500 | "D"
  • 600 | "DC"
  • 700 | "DCC"
  • 800 | "DCCC"
  • 900 | "CM"
  • ### Tens
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 10 | "X"
  • 20 | "XX"
  • 30 | "XXX"
  • 40 | "XL"
  • 50 | "L"
  • 60 | "LX"
  • 70 | "LXX"
  • 80 | "LXXX"
  • 90 | "XC"
  • ### Units
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 1 | "I"
  • 2 | "II"
  • 3 | "III"
  • 4 | "IV"
  • 5 | "V"
  • 6 | "VI"
  • 7 | "VII"
  • 8 | "VIII"
  • 9 | "IX"
  • So, for example, 2345 would be represented as the concatenation of `MM` (for 2000), `CCC` (for 300), `XL` (for 40), and `V` (for 5), or `MMCCCXLV`.
  • This defines a unique correct representation for each number from 1 to 3999 (4000 and above are not representable without breaking the rule that a numeral cannot appear more than 3 times consecutively). The 50 substrings method described above will identify each of these 3999 strings as valid, and all other strings of Roman numerals as invalid.
  • ## Input
  • - A string containing only Roman numerals (`I`, `V`, `X`, `L`, `C`, `D`, `M`).
  • - The string will have length at least 1.
  • - The string will have length at most 15 (this is the length of the longest valid string of Roman numerals).
  • ## Output
  • - If the input is a valid Roman number, output a consistent value indicating this.
  • - Consistent means that the value must be the same for all valid Roman numbers.
  • - The output for a valid Roman number must not be one of the strings from the list of 50.
  • - If the input is not a valid Roman number, output exactly 1 string from the list of 50.
  • - The output must be a substring of the input.
  • - If the input has 2 or more of the strings from the list of 50 as substrings, you may choose any 1 of them to be the output, but you must choose only 1 of them (you must not output 2 or more).
  • ## Examples
  • ### A valid Roman number
  • The input `MCMXCVI` is the unique correct representation of 1996. It contains none of the 50 strings.
  • ### A string that is not a valid Roman number
  • Although the input `MMXDIII` might be suspected of representing 2493, it is not the unique correct representation of this number (which is `MMCDXCIII`). Note that it has `XD` as a substring, identifying it as invalid. The only correct output is therefore `XD`.
  • ### An invalid string with more than 1 potential output
  • The input `MMCCMDXXV` has 2 substrings that make it invalid, so either `CCM` or `CMD` would be correct outputs. It would not be correct to output both of these, or to output their overlap `CCMD`, as this is not one of the 50 strings.
  • ## Test cases
  • Test cases are in the format `INPUT : VALID, OUTPUTS`. Note that only one of the valid outputs can be chosen - outputting 2 or more is incorrect.
  • ```text
  • I : VALID
  • V : VALID
  • X : VALID
  • L : VALID
  • C : VALID
  • D : VALID
  • M : VALID
  • II : VALID
  • VV : VV
  • XX : VALID
  • LL : LL
  • CC : VALID
  • DD : DD
  • MM : VALID
  • III : VALID
  • VII : VALID
  • IVI : IVI
  • IIV : IIV
  • CCI : VALID
  • CCV : VALID
  • CCX : VALID
  • CCL : VALID
  • CCC : VALID
  • CCD : CCD
  • CCM : CCM
  • IIII : IIII
  • MLDI : LD
  • MXXC : XXC
  • DCIIX : IIX
  • MCXXXX : XXXX
  • MCCCCXVI : CCCC
  • MMLXCVII : LXC
  • MMMCMXCIX : VALID
  • MMMDCCCLXXXVIII : VALID
  • MMCCCXLV : VALID
  • MCMXCVI : VALID
  • MMXDIII : XD
  • MMCDXCIII : VALID
  • MMCCMDXXV : CCM, CMD
  • XXX : VALID
  • CLLX : LL
  • DXXDMMV : DM, XD
  • CCDDDIMDD : DD, IM, CCD
  • VLCXIVXMCVXLC : VX, VL, LC, XM
  • DVLIILVCXVXVMLI : VX, VL, IL, VM, VC
  • VVDLMIVILXXDX : VV, VD, IL, XD, LM, IVI
  • DMXXCMILVCMLLMV : DM, IL, LL, VC, LM, XXC, XCM
  • CMXDVLCCDDLXLXC : DD, VL, LC, XD, XLX, LXC, CCD, LXL
  • XDIXCLLMVVLCMCM : VV, VL, LC, XD, LL, LM, IXC, CMC, XCL
  • IIXXCDVVLMILVDD : DD, VV, VL, VD, IL, LM, XXC, IXX, IIX, XCD
  • DDMIIXXCMCCDCMM : DD, DM, CMC, CMM, XXC, IXX, DCM, CDC, XCM, CCD, IIX
  • ```
  • ## Scoring
  • This is a [code golf challenge]. Your score is the number of bytes in your code. Lowest score for each language wins.
  • > Explanations are optional, but I'm more likely to upvote answers that have one.
  • [code golf challenge]: https://codegolf.codidact.com/categories/49/tags/4274 "The code-golf tag"
  • [^1]: This is a common modern set of rules, described as [Standard form](https://en.wikipedia.org/wiki/Roman_numerals#Standard_form) on Wikipedia. It does not reflect all usages during history, but will be the basis of this challenge, since otherwise the 50 substrings approach does not work.
  • Given a string of Roman numerals, decide whether it forms a valid Roman number. If not, output the substring that proves this, from the list of 50 strings described below.
  • ## Relevant fact
  • This challenge is based around the following fact:
  • > A string of Roman numerals is a valid Roman number if and only if it contains none of the following 50 strings as a substring:
  • >
  • > ```text
  • > "CCCC", "CCD", "CCM", "CDC", "CMC", "CMD", "CMM", "DCD", "DCM", "DD", "DM", "IC", "ID", "IIII", "IIV", "IIX", "IL", "IM", "IVI", "IXC", "IXI", "IXL", "IXV", "IXX", "LC", "LD", "LL", "LM", "LXC", "LXL", "MMMM", "VC", "VD", "VIV", "VIX", "VL", "VM", "VV", "VX", "XCC", "XCD", "XCL", "XCM", "XCX", "XD", "XLX", "XM", "XXC", "XXL", "XXXX"
  • > ```
  • This works for any length string, if a valid Roman number is defined as follows:
  • ## Valid Roman numbers[^1]
  • - Each numeral appears no more than 3 times consecutively.
  • - Each of `V` (5), `L` (50), and `D` (500) appears no more than once consecutively.
  • - A Roman number is constructed by concatenating the strings representing its ***thousands***, ***hundreds***, ***tens***, and ***units*** components.
  • Decimal | Thousands | Hundreds | Tens | Units
  • - | - | - | - | -
  • 1 | M | C | X | I
  • 2 | MM | CC | XX | II
  • 3 | MMM | CCC | XXX | III
  • 4 | | CD | XL | IV
  • 5 | | D | L | V
  • 6 | | DC | LX | VI
  • 7 | | DCC | LXX | VII
  • 8 | | DCCC | LXXX | VIII
  • 9 | | CM | XC | IX
  • So, for example, 2345 would be represented as the concatenation of `MM` (for 2000), `CCC` (for 300), `XL` (for 40), and `V` (for 5), or `MMCCCXLV`.
  • This defines a unique correct representation for each number from 1 to 3999 (4000 and above are not representable without breaking the rule that a numeral cannot appear more than 3 times consecutively). The 50 substrings method described above will identify each of these 3999 strings as valid, and all other strings of Roman numerals as invalid.
  • ## Input
  • - A string containing only Roman numerals (`I`, `V`, `X`, `L`, `C`, `D`, `M`).
  • - The string will have length at least 1.
  • - The string will have length at most 15 (this is the length of the longest valid string of Roman numerals).
  • ## Output
  • - If the input is a valid Roman number, output a consistent value indicating this.
  • - Consistent means that the value must be the same for all valid Roman numbers.
  • - The output for a valid Roman number must not be one of the strings from the list of 50.
  • - If the input is not a valid Roman number, output exactly 1 string from the list of 50.
  • - The output must be a substring of the input.
  • - If the input has 2 or more of the strings from the list of 50 as substrings, you may choose any 1 of them to be the output, but you must choose only 1 of them (you must not output 2 or more).
  • ## Examples
  • ### A valid Roman number
  • The input `MCMXCVI` is the unique correct representation of 1996. It contains none of the 50 strings.
  • ### A string that is not a valid Roman number
  • Although the input `MMXDIII` might be suspected of representing 2493, it is not the unique correct representation of this number (which is `MMCDXCIII`). Note that it has `XD` as a substring, identifying it as invalid. The only correct output is therefore `XD`.
  • ### An invalid string with more than 1 potential output
  • The input `MMCCMDXXV` has 2 substrings that make it invalid, so either `CCM` or `CMD` would be correct outputs. It would not be correct to output both of these, or to output their overlap `CCMD`, as this is not one of the 50 strings.
  • ## Test cases
  • Test cases are in the format `INPUT : VALID, OUTPUTS`. Note that only one of the valid outputs can be chosen - outputting 2 or more is incorrect.
  • ```text
  • I : VALID
  • V : VALID
  • X : VALID
  • L : VALID
  • C : VALID
  • D : VALID
  • M : VALID
  • II : VALID
  • VV : VV
  • XX : VALID
  • LL : LL
  • CC : VALID
  • DD : DD
  • MM : VALID
  • III : VALID
  • VII : VALID
  • IVI : IVI
  • IIV : IIV
  • CCI : VALID
  • CCV : VALID
  • CCX : VALID
  • CCL : VALID
  • CCC : VALID
  • CCD : CCD
  • CCM : CCM
  • IIII : IIII
  • MLDI : LD
  • MXXC : XXC
  • DCIIX : IIX
  • MCXXXX : XXXX
  • MCCCCXVI : CCCC
  • MMLXCVII : LXC
  • MMMCMXCIX : VALID
  • MMMDCCCLXXXVIII : VALID
  • MMCCCXLV : VALID
  • MCMXCVI : VALID
  • MMXDIII : XD
  • MMCDXCIII : VALID
  • MMCCMDXXV : CCM, CMD
  • XXX : VALID
  • CLLX : LL
  • DXXDMMV : DM, XD
  • CCDDDIMDD : DD, IM, CCD
  • VLCXIVXMCVXLC : VX, VL, LC, XM
  • DVLIILVCXVXVMLI : VX, VL, IL, VM, VC
  • VVDLMIVILXXDX : VV, VD, IL, XD, LM, IVI
  • DMXXCMILVCMLLMV : DM, IL, LL, VC, LM, XXC, XCM
  • CMXDVLCCDDLXLXC : DD, VL, LC, XD, XLX, LXC, CCD, LXL
  • XDIXCLLMVVLCMCM : VV, VL, LC, XD, LL, LM, IXC, CMC, XCL
  • IIXXCDVVLMILVDD : DD, VV, VL, VD, IL, LM, XXC, IXX, IIX, XCD
  • DDMIIXXCMCCDCMM : DD, DM, CMC, CMM, XXC, IXX, DCM, CDC, XCM, CCD, IIX
  • ```
  • ## Scoring
  • This is a [code golf challenge]. Your score is the number of bytes in your code. Lowest score for each language wins.
  • > Explanations are optional, but I'm more likely to upvote answers that have one.
  • [code golf challenge]: https://codegolf.codidact.com/categories/49/tags/4274 "The code-golf tag"
  • [^1]: This is a common modern set of rules, described as [Standard form](https://en.wikipedia.org/wiki/Roman_numerals#Standard_form) on Wikipedia. It does not reflect all usages during history, but will be the basis of this challenge, since otherwise the 50 substrings approach does not work.
#16: Post edited by user avatar trichoplax‭ · 2024-03-11T17:30:15Z (9 months ago)
Add footnote linking to Standard form on Wikipedia
  • Given a string of Roman numerals, decide whether it forms a valid Roman number. If not, output the substring that proves this, from the list of 50 strings described below.
  • ## Relevant fact
  • This challenge is based around the following fact:
  • > A string of Roman numerals is a valid Roman number if and only if it contains none of the following 50 strings as a substring:
  • >
  • > ```text
  • > "CCCC", "CCD", "CCM", "CDC", "CMC", "CMD", "CMM", "DCD", "DCM", "DD", "DM", "IC", "ID", "IIII", "IIV", "IIX", "IL", "IM", "IVI", "IXC", "IXI", "IXL", "IXV", "IXX", "LC", "LD", "LL", "LM", "LXC", "LXL", "MMMM", "VC", "VD", "VIV", "VIX", "VL", "VM", "VV", "VX", "XCC", "XCD", "XCL", "XCM", "XCX", "XD", "XLX", "XM", "XXC", "XXL", "XXXX"
  • > ```
  • This works for any length string, if a valid Roman number is defined as follows:
  • ## Valid Roman numbers
  • - Each numeral appears no more than 3 times consecutively.
  • - Each of `V` (5), `L` (50), and `D` (500) appears no more than once consecutively.
  • - A Roman number is constructed by concatenating the strings representing its ***thousands***, ***hundreds***, ***tens***, and ***units*** components.
  • ### Thousands
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 1000 | "M"
  • 2000 | "MM"
  • 3000 | "MMM"
  • ### Hundreds
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 100 | "C"
  • 200 | "CC"
  • 300 | "CCC"
  • 400 | "CD"
  • 500 | "D"
  • 600 | "DC"
  • 700 | "DCC"
  • 800 | "DCCC"
  • 900 | "CM"
  • ### Tens
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 10 | "X"
  • 20 | "XX"
  • 30 | "XXX"
  • 40 | "XL"
  • 50 | "L"
  • 60 | "LX"
  • 70 | "LXX"
  • 80 | "LXXX"
  • 90 | "XC"
  • ### Units
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 1 | "I"
  • 2 | "II"
  • 3 | "III"
  • 4 | "IV"
  • 5 | "V"
  • 6 | "VI"
  • 7 | "VII"
  • 8 | "VIII"
  • 9 | "IX"
  • So, for example, 2345 would be represented as the concatenation of `MM` (for 2000), `CCC` (for 300), `XL` (for 40), and `V` (for 5), or `MMCCCXLV`.
  • This defines a unique correct representation for each number from 1 to 3999 (4000 and above are not representable without breaking the rule that a numeral cannot appear more than 3 times consecutively). The 50 substrings method described above will identify each of these 3999 strings as valid, and all other strings of Roman numerals as invalid.
  • ## Input
  • - A string containing only Roman numerals (`I`, `V`, `X`, `L`, `C`, `D`, `M`).
  • - The string will have length at least 1.
  • - The string will have length at most 15 (this is the length of the longest valid string of Roman numerals).
  • ## Output
  • - If the input is a valid Roman number, output a consistent value indicating this.
  • - Consistent means that the value must be the same for all valid Roman numbers.
  • - The output for a valid Roman number must not be one of the strings from the list of 50.
  • - If the input is not a valid Roman number, output exactly 1 string from the list of 50.
  • - The output must be a substring of the input.
  • - If the input has 2 or more of the strings from the list of 50 as substrings, you may choose any 1 of them to be the output, but you must choose only 1 of them (you must not output 2 or more).
  • ## Examples
  • ### A valid Roman number
  • The input `MCMXCVI` is the unique correct representation of 1996. It contains none of the 50 strings.
  • ### A string that is not a valid Roman number
  • Although the input `MMXDIII` might be suspected of representing 2493, it is not the unique correct representation of this number (which is `MMCDXCIII`). Note that it has `XD` as a substring, identifying it as invalid. The only correct output is therefore `XD`.
  • ### An invalid string with more than 1 potential output
  • The input `MMCCMDXXV` has 2 substrings that make it invalid, so either `CCM` or `CMD` would be correct outputs. It would not be correct to output both of these, or to output their overlap `CCMD`, as this is not one of the 50 strings.
  • ## Test cases
  • Test cases are in the format `INPUT : VALID, OUTPUTS`. Note that only one of the valid outputs can be chosen - outputting 2 or more is incorrect.
  • ```text
  • I : VALID
  • V : VALID
  • X : VALID
  • L : VALID
  • C : VALID
  • D : VALID
  • M : VALID
  • II : VALID
  • VV : VV
  • XX : VALID
  • LL : LL
  • CC : VALID
  • DD : DD
  • MM : VALID
  • III : VALID
  • VII : VALID
  • IVI : IVI
  • IIV : IIV
  • CCI : VALID
  • CCV : VALID
  • CCX : VALID
  • CCL : VALID
  • CCC : VALID
  • CCD : CCD
  • CCM : CCM
  • IIII : IIII
  • MLDI : LD
  • MXXC : XXC
  • DCIIX : IIX
  • MCXXXX : XXXX
  • MCCCCXVI : CCCC
  • MMLXCVII : LXC
  • MMMCMXCIX : VALID
  • MMMDCCCLXXXVIII : VALID
  • MMCCCXLV : VALID
  • MCMXCVI : VALID
  • MMXDIII : XD
  • MMCDXCIII : VALID
  • MMCCMDXXV : CCM, CMD
  • XXX : VALID
  • CLLX : LL
  • DXXDMMV : DM, XD
  • CCDDDIMDD : DD, IM, CCD
  • VLCXIVXMCVXLC : VX, VL, LC, XM
  • DVLIILVCXVXVMLI : VX, VL, IL, VM, VC
  • VVDLMIVILXXDX : VV, VD, IL, XD, LM, IVI
  • DMXXCMILVCMLLMV : DM, IL, LL, VC, LM, XXC, XCM
  • CMXDVLCCDDLXLXC : DD, VL, LC, XD, XLX, LXC, CCD, LXL
  • XDIXCLLMVVLCMCM : VV, VL, LC, XD, LL, LM, IXC, CMC, XCL
  • IIXXCDVVLMILVDD : DD, VV, VL, VD, IL, LM, XXC, IXX, IIX, XCD
  • DDMIIXXCMCCDCMM : DD, DM, CMC, CMM, XXC, IXX, DCM, CDC, XCM, CCD, IIX
  • ```
  • ## Scoring
  • This is a [code golf challenge]. Your score is the number of bytes in your code. Lowest score for each language wins.
  • > Explanations are optional, but I'm more likely to upvote answers that have one.
  • [code golf challenge]: https://codegolf.codidact.com/categories/49/tags/4274 "The code-golf tag"
  • Given a string of Roman numerals, decide whether it forms a valid Roman number. If not, output the substring that proves this, from the list of 50 strings described below.
  • ## Relevant fact
  • This challenge is based around the following fact:
  • > A string of Roman numerals is a valid Roman number if and only if it contains none of the following 50 strings as a substring:
  • >
  • > ```text
  • > "CCCC", "CCD", "CCM", "CDC", "CMC", "CMD", "CMM", "DCD", "DCM", "DD", "DM", "IC", "ID", "IIII", "IIV", "IIX", "IL", "IM", "IVI", "IXC", "IXI", "IXL", "IXV", "IXX", "LC", "LD", "LL", "LM", "LXC", "LXL", "MMMM", "VC", "VD", "VIV", "VIX", "VL", "VM", "VV", "VX", "XCC", "XCD", "XCL", "XCM", "XCX", "XD", "XLX", "XM", "XXC", "XXL", "XXXX"
  • > ```
  • This works for any length string, if a valid Roman number is defined as follows:
  • ## Valid Roman numbers[^1]
  • - Each numeral appears no more than 3 times consecutively.
  • - Each of `V` (5), `L` (50), and `D` (500) appears no more than once consecutively.
  • - A Roman number is constructed by concatenating the strings representing its ***thousands***, ***hundreds***, ***tens***, and ***units*** components.
  • ### Thousands
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 1000 | "M"
  • 2000 | "MM"
  • 3000 | "MMM"
  • ### Hundreds
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 100 | "C"
  • 200 | "CC"
  • 300 | "CCC"
  • 400 | "CD"
  • 500 | "D"
  • 600 | "DC"
  • 700 | "DCC"
  • 800 | "DCCC"
  • 900 | "CM"
  • ### Tens
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 10 | "X"
  • 20 | "XX"
  • 30 | "XXX"
  • 40 | "XL"
  • 50 | "L"
  • 60 | "LX"
  • 70 | "LXX"
  • 80 | "LXXX"
  • 90 | "XC"
  • ### Units
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 1 | "I"
  • 2 | "II"
  • 3 | "III"
  • 4 | "IV"
  • 5 | "V"
  • 6 | "VI"
  • 7 | "VII"
  • 8 | "VIII"
  • 9 | "IX"
  • So, for example, 2345 would be represented as the concatenation of `MM` (for 2000), `CCC` (for 300), `XL` (for 40), and `V` (for 5), or `MMCCCXLV`.
  • This defines a unique correct representation for each number from 1 to 3999 (4000 and above are not representable without breaking the rule that a numeral cannot appear more than 3 times consecutively). The 50 substrings method described above will identify each of these 3999 strings as valid, and all other strings of Roman numerals as invalid.
  • ## Input
  • - A string containing only Roman numerals (`I`, `V`, `X`, `L`, `C`, `D`, `M`).
  • - The string will have length at least 1.
  • - The string will have length at most 15 (this is the length of the longest valid string of Roman numerals).
  • ## Output
  • - If the input is a valid Roman number, output a consistent value indicating this.
  • - Consistent means that the value must be the same for all valid Roman numbers.
  • - The output for a valid Roman number must not be one of the strings from the list of 50.
  • - If the input is not a valid Roman number, output exactly 1 string from the list of 50.
  • - The output must be a substring of the input.
  • - If the input has 2 or more of the strings from the list of 50 as substrings, you may choose any 1 of them to be the output, but you must choose only 1 of them (you must not output 2 or more).
  • ## Examples
  • ### A valid Roman number
  • The input `MCMXCVI` is the unique correct representation of 1996. It contains none of the 50 strings.
  • ### A string that is not a valid Roman number
  • Although the input `MMXDIII` might be suspected of representing 2493, it is not the unique correct representation of this number (which is `MMCDXCIII`). Note that it has `XD` as a substring, identifying it as invalid. The only correct output is therefore `XD`.
  • ### An invalid string with more than 1 potential output
  • The input `MMCCMDXXV` has 2 substrings that make it invalid, so either `CCM` or `CMD` would be correct outputs. It would not be correct to output both of these, or to output their overlap `CCMD`, as this is not one of the 50 strings.
  • ## Test cases
  • Test cases are in the format `INPUT : VALID, OUTPUTS`. Note that only one of the valid outputs can be chosen - outputting 2 or more is incorrect.
  • ```text
  • I : VALID
  • V : VALID
  • X : VALID
  • L : VALID
  • C : VALID
  • D : VALID
  • M : VALID
  • II : VALID
  • VV : VV
  • XX : VALID
  • LL : LL
  • CC : VALID
  • DD : DD
  • MM : VALID
  • III : VALID
  • VII : VALID
  • IVI : IVI
  • IIV : IIV
  • CCI : VALID
  • CCV : VALID
  • CCX : VALID
  • CCL : VALID
  • CCC : VALID
  • CCD : CCD
  • CCM : CCM
  • IIII : IIII
  • MLDI : LD
  • MXXC : XXC
  • DCIIX : IIX
  • MCXXXX : XXXX
  • MCCCCXVI : CCCC
  • MMLXCVII : LXC
  • MMMCMXCIX : VALID
  • MMMDCCCLXXXVIII : VALID
  • MMCCCXLV : VALID
  • MCMXCVI : VALID
  • MMXDIII : XD
  • MMCDXCIII : VALID
  • MMCCMDXXV : CCM, CMD
  • XXX : VALID
  • CLLX : LL
  • DXXDMMV : DM, XD
  • CCDDDIMDD : DD, IM, CCD
  • VLCXIVXMCVXLC : VX, VL, LC, XM
  • DVLIILVCXVXVMLI : VX, VL, IL, VM, VC
  • VVDLMIVILXXDX : VV, VD, IL, XD, LM, IVI
  • DMXXCMILVCMLLMV : DM, IL, LL, VC, LM, XXC, XCM
  • CMXDVLCCDDLXLXC : DD, VL, LC, XD, XLX, LXC, CCD, LXL
  • XDIXCLLMVVLCMCM : VV, VL, LC, XD, LL, LM, IXC, CMC, XCL
  • IIXXCDVVLMILVDD : DD, VV, VL, VD, IL, LM, XXC, IXX, IIX, XCD
  • DDMIIXXCMCCDCMM : DD, DM, CMC, CMM, XXC, IXX, DCM, CDC, XCM, CCD, IIX
  • ```
  • ## Scoring
  • This is a [code golf challenge]. Your score is the number of bytes in your code. Lowest score for each language wins.
  • > Explanations are optional, but I'm more likely to upvote answers that have one.
  • [code golf challenge]: https://codegolf.codidact.com/categories/49/tags/4274 "The code-golf tag"
  • [^1]: This is a common modern set of rules, described as [Standard form](https://en.wikipedia.org/wiki/Roman_numerals#Standard_form) on Wikipedia. It does not reflect all usages during history, but will be the basis of this challenge, since otherwise the 50 substrings approach does not work.
#15: Post edited by user avatar trichoplax‭ · 2024-03-11T17:06:01Z (9 months ago)
Add test cases with various numbers of invalid substrings
  • Given a string of Roman numerals, decide whether it forms a valid Roman number. If not, output the substring that proves this, from the list of 50 strings described below.
  • ## Relevant fact
  • This challenge is based around the following fact:
  • > A string of Roman numerals is a valid Roman number if and only if it contains none of the following 50 strings as a substring:
  • >
  • > ```text
  • > "CCCC", "CCD", "CCM", "CDC", "CMC", "CMD", "CMM", "DCD", "DCM", "DD", "DM", "IC", "ID", "IIII", "IIV", "IIX", "IL", "IM", "IVI", "IXC", "IXI", "IXL", "IXV", "IXX", "LC", "LD", "LL", "LM", "LXC", "LXL", "MMMM", "VC", "VD", "VIV", "VIX", "VL", "VM", "VV", "VX", "XCC", "XCD", "XCL", "XCM", "XCX", "XD", "XLX", "XM", "XXC", "XXL", "XXXX"
  • > ```
  • This works for any length string, if a valid Roman number is defined as follows:
  • ## Valid Roman numbers
  • - Each numeral appears no more than 3 times consecutively.
  • - Each of `V` (5), `L` (50), and `D` (500) appears no more than once consecutively.
  • - A Roman number is constructed by concatenating the strings representing its ***thousands***, ***hundreds***, ***tens***, and ***units*** components.
  • ### Thousands
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 1000 | "M"
  • 2000 | "MM"
  • 3000 | "MMM"
  • ### Hundreds
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 100 | "C"
  • 200 | "CC"
  • 300 | "CCC"
  • 400 | "CD"
  • 500 | "D"
  • 600 | "DC"
  • 700 | "DCC"
  • 800 | "DCCC"
  • 900 | "CM"
  • ### Tens
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 10 | "X"
  • 20 | "XX"
  • 30 | "XXX"
  • 40 | "XL"
  • 50 | "L"
  • 60 | "LX"
  • 70 | "LXX"
  • 80 | "LXXX"
  • 90 | "XC"
  • ### Units
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 1 | "I"
  • 2 | "II"
  • 3 | "III"
  • 4 | "IV"
  • 5 | "V"
  • 6 | "VI"
  • 7 | "VII"
  • 8 | "VIII"
  • 9 | "IX"
  • So, for example, 2345 would be represented as the concatenation of `MM` (for 2000), `CCC` (for 300), `XL` (for 40), and `V` (for 5), or `MMCCCXLV`.
  • This defines a unique correct representation for each number from 1 to 3999 (4000 and above are not representable without breaking the rule that a numeral cannot appear more than 3 times consecutively). The 50 substrings method described above will identify each of these 3999 strings as valid, and all other strings of Roman numerals as invalid.
  • ## Input
  • - A string containing only Roman numerals (`I`, `V`, `X`, `L`, `C`, `D`, `M`).
  • - The string will have length at least 1.
  • - The string will have length at most 15 (this is the length of the longest valid string of Roman numerals).
  • ## Output
  • - If the input is a valid Roman number, output a consistent value indicating this.
  • - Consistent means that the value must be the same for all valid Roman numbers.
  • - The output for a valid Roman number must not be one of the strings from the list of 50.
  • - If the input is not a valid Roman number, output exactly 1 string from the list of 50.
  • - The output must be a substring of the input.
  • - If the input has 2 or more of the strings from the list of 50 as substrings, you may choose any 1 of them to be the output, but you must choose only 1 of them (you must not output 2 or more).
  • ## Examples
  • ### A valid Roman number
  • The input `MCMXCVI` is the unique correct representation of 1996. It contains none of the 50 strings.
  • ### A string that is not a valid Roman number
  • Although the input `MMXDIII` might be suspected of representing 2493, it is not the unique correct representation of this number (which is `MMCDXCIII`). Note that it has `XD` as a substring, identifying it as invalid. The only correct output is therefore `XD`.
  • ### An invalid string with more than 1 potential output
  • The input `MMCCMDXXV` has 2 substrings that make it invalid, so either `CCM` or `CMD` would be correct outputs. It would not be correct to output both of these, or to output their overlap `CCMD`, as this is not one of the 50 strings.
  • ## Test cases
  • Test cases are in the format `INPUT : VALID, OUTPUTS`. Note that only one of the valid outputs can be chosen - outputting 2 or more is incorrect.
  • ```text
  • I : VALID
  • V : VALID
  • X : VALID
  • L : VALID
  • C : VALID
  • D : VALID
  • M : VALID
  • II : VALID
  • VV : VV
  • XX : VALID
  • LL : LL
  • CC : VALID
  • DD : DD
  • MM : VALID
  • III : VALID
  • VII : VALID
  • IVI : IVI
  • IIV : IIV
  • CCI : VALID
  • CCV : VALID
  • CCX : VALID
  • CCL : VALID
  • CCC : VALID
  • CCD : CCD
  • CCM : CCM
  • IIII : IIII
  • MLDI : LD
  • MXXC : XXC
  • DCIIX : IIX
  • MCXXXX : XXXX
  • MCCCCXVI : CCCC
  • MMLXCVII : LXC
  • MMMCMXCIX : VALID
  • MMMDCCCLXXXVIII : VALID
  • MMCCCXLV : VALID
  • MCMXCVI : VALID
  • MMXDIII : XD
  • MMCDXCIII : VALID
  • MMCCMDXXV : CCM, CMD
  • ```
  • ## Scoring
  • This is a [code golf challenge]. Your score is the number of bytes in your code. Lowest score for each language wins.
  • > Explanations are optional, but I'm more likely to upvote answers that have one.
  • [code golf challenge]: https://codegolf.codidact.com/categories/49/tags/4274 "The code-golf tag"
  • Given a string of Roman numerals, decide whether it forms a valid Roman number. If not, output the substring that proves this, from the list of 50 strings described below.
  • ## Relevant fact
  • This challenge is based around the following fact:
  • > A string of Roman numerals is a valid Roman number if and only if it contains none of the following 50 strings as a substring:
  • >
  • > ```text
  • > "CCCC", "CCD", "CCM", "CDC", "CMC", "CMD", "CMM", "DCD", "DCM", "DD", "DM", "IC", "ID", "IIII", "IIV", "IIX", "IL", "IM", "IVI", "IXC", "IXI", "IXL", "IXV", "IXX", "LC", "LD", "LL", "LM", "LXC", "LXL", "MMMM", "VC", "VD", "VIV", "VIX", "VL", "VM", "VV", "VX", "XCC", "XCD", "XCL", "XCM", "XCX", "XD", "XLX", "XM", "XXC", "XXL", "XXXX"
  • > ```
  • This works for any length string, if a valid Roman number is defined as follows:
  • ## Valid Roman numbers
  • - Each numeral appears no more than 3 times consecutively.
  • - Each of `V` (5), `L` (50), and `D` (500) appears no more than once consecutively.
  • - A Roman number is constructed by concatenating the strings representing its ***thousands***, ***hundreds***, ***tens***, and ***units*** components.
  • ### Thousands
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 1000 | "M"
  • 2000 | "MM"
  • 3000 | "MMM"
  • ### Hundreds
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 100 | "C"
  • 200 | "CC"
  • 300 | "CCC"
  • 400 | "CD"
  • 500 | "D"
  • 600 | "DC"
  • 700 | "DCC"
  • 800 | "DCCC"
  • 900 | "CM"
  • ### Tens
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 10 | "X"
  • 20 | "XX"
  • 30 | "XXX"
  • 40 | "XL"
  • 50 | "L"
  • 60 | "LX"
  • 70 | "LXX"
  • 80 | "LXXX"
  • 90 | "XC"
  • ### Units
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 1 | "I"
  • 2 | "II"
  • 3 | "III"
  • 4 | "IV"
  • 5 | "V"
  • 6 | "VI"
  • 7 | "VII"
  • 8 | "VIII"
  • 9 | "IX"
  • So, for example, 2345 would be represented as the concatenation of `MM` (for 2000), `CCC` (for 300), `XL` (for 40), and `V` (for 5), or `MMCCCXLV`.
  • This defines a unique correct representation for each number from 1 to 3999 (4000 and above are not representable without breaking the rule that a numeral cannot appear more than 3 times consecutively). The 50 substrings method described above will identify each of these 3999 strings as valid, and all other strings of Roman numerals as invalid.
  • ## Input
  • - A string containing only Roman numerals (`I`, `V`, `X`, `L`, `C`, `D`, `M`).
  • - The string will have length at least 1.
  • - The string will have length at most 15 (this is the length of the longest valid string of Roman numerals).
  • ## Output
  • - If the input is a valid Roman number, output a consistent value indicating this.
  • - Consistent means that the value must be the same for all valid Roman numbers.
  • - The output for a valid Roman number must not be one of the strings from the list of 50.
  • - If the input is not a valid Roman number, output exactly 1 string from the list of 50.
  • - The output must be a substring of the input.
  • - If the input has 2 or more of the strings from the list of 50 as substrings, you may choose any 1 of them to be the output, but you must choose only 1 of them (you must not output 2 or more).
  • ## Examples
  • ### A valid Roman number
  • The input `MCMXCVI` is the unique correct representation of 1996. It contains none of the 50 strings.
  • ### A string that is not a valid Roman number
  • Although the input `MMXDIII` might be suspected of representing 2493, it is not the unique correct representation of this number (which is `MMCDXCIII`). Note that it has `XD` as a substring, identifying it as invalid. The only correct output is therefore `XD`.
  • ### An invalid string with more than 1 potential output
  • The input `MMCCMDXXV` has 2 substrings that make it invalid, so either `CCM` or `CMD` would be correct outputs. It would not be correct to output both of these, or to output their overlap `CCMD`, as this is not one of the 50 strings.
  • ## Test cases
  • Test cases are in the format `INPUT : VALID, OUTPUTS`. Note that only one of the valid outputs can be chosen - outputting 2 or more is incorrect.
  • ```text
  • I : VALID
  • V : VALID
  • X : VALID
  • L : VALID
  • C : VALID
  • D : VALID
  • M : VALID
  • II : VALID
  • VV : VV
  • XX : VALID
  • LL : LL
  • CC : VALID
  • DD : DD
  • MM : VALID
  • III : VALID
  • VII : VALID
  • IVI : IVI
  • IIV : IIV
  • CCI : VALID
  • CCV : VALID
  • CCX : VALID
  • CCL : VALID
  • CCC : VALID
  • CCD : CCD
  • CCM : CCM
  • IIII : IIII
  • MLDI : LD
  • MXXC : XXC
  • DCIIX : IIX
  • MCXXXX : XXXX
  • MCCCCXVI : CCCC
  • MMLXCVII : LXC
  • MMMCMXCIX : VALID
  • MMMDCCCLXXXVIII : VALID
  • MMCCCXLV : VALID
  • MCMXCVI : VALID
  • MMXDIII : XD
  • MMCDXCIII : VALID
  • MMCCMDXXV : CCM, CMD
  • XXX : VALID
  • CLLX : LL
  • DXXDMMV : DM, XD
  • CCDDDIMDD : DD, IM, CCD
  • VLCXIVXMCVXLC : VX, VL, LC, XM
  • DVLIILVCXVXVMLI : VX, VL, IL, VM, VC
  • VVDLMIVILXXDX : VV, VD, IL, XD, LM, IVI
  • DMXXCMILVCMLLMV : DM, IL, LL, VC, LM, XXC, XCM
  • CMXDVLCCDDLXLXC : DD, VL, LC, XD, XLX, LXC, CCD, LXL
  • XDIXCLLMVVLCMCM : VV, VL, LC, XD, LL, LM, IXC, CMC, XCL
  • IIXXCDVVLMILVDD : DD, VV, VL, VD, IL, LM, XXC, IXX, IIX, XCD
  • DDMIIXXCMCCDCMM : DD, DM, CMC, CMM, XXC, IXX, DCM, CDC, XCM, CCD, IIX
  • ```
  • ## Scoring
  • This is a [code golf challenge]. Your score is the number of bytes in your code. Lowest score for each language wins.
  • > Explanations are optional, but I'm more likely to upvote answers that have one.
  • [code golf challenge]: https://codegolf.codidact.com/categories/49/tags/4274 "The code-golf tag"
#14: Post edited by user avatar trichoplax‭ · 2024-03-06T23:43:14Z (9 months ago)
Tidying
  • Given a string of Roman numerals, decide whether it forms a valid Roman number. If not, output the substring that proves this, from the list of 50 strings described below.
  • This challenge is based around the following fact:
  • > A string of Roman numerals is a valid Roman number if and only if it contains none of the following 50 strings as a substring:
  • >
  • > ```text
  • > "CCCC", "CCD", "CCM", "CDC", "CMC", "CMD", "CMM", "DCD", "DCM", "DD", "DM", "IC", "ID", "IIII", "IIV", "IIX", "IL", "IM", "IVI", "IXC", "IXI", "IXL", "IXV", "IXX", "LC", "LD", "LL", "LM", "LXC", "LXL", "MMMM", "VC", "VD", "VIV", "VIX", "VL", "VM", "VV", "VX", "XCC", "XCD", "XCL", "XCM", "XCX", "XD", "XLX", "XM", "XXC", "XXL", "XXXX"
  • > ```
  • This works for any length string, if a valid Roman number is defined as follows:
  • ## Valid Roman numbers
  • - Each numeral appears no more than 3 times consecutively.
  • - Each of `V` (5), `L` (50), and `D` (500) appears no more than once consecutively.
  • - A Roman number is constructed by concatenating the strings representing its ***thousands***, ***hundreds***, ***tens***, and ***units*** components.
  • ### Thousands
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 1000 | "M"
  • 2000 | "MM"
  • 3000 | "MMM"
  • ### Hundreds
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 100 | "C"
  • 200 | "CC"
  • 300 | "CCC"
  • 400 | "CD"
  • 500 | "D"
  • 600 | "DC"
  • 700 | "DCC"
  • 800 | "DCCC"
  • 900 | "CM"
  • ### Tens
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 10 | "X"
  • 20 | "XX"
  • 30 | "XXX"
  • 40 | "XL"
  • 50 | "L"
  • 60 | "LX"
  • 70 | "LXX"
  • 80 | "LXXX"
  • 90 | "XC"
  • ### Units
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 1 | "I"
  • 2 | "II"
  • 3 | "III"
  • 4 | "IV"
  • 5 | "V"
  • 6 | "VI"
  • 7 | "VII"
  • 8 | "VIII"
  • 9 | "IX"
  • So, for example, 2345 would be represented as the concatenation of `MM` (for 2000), `CCC` (for 300), `XL` (for 40), and `V` (for 5), or `MMCCCXLV`.
  • This defines a unique correct representation for each number from 1 to 3999 (4000 and above are not representable without breaking the rule that a numeral cannot appear more than 3 times consecutively). The 50 substrings method described above will identify each of these 3999 strings as valid, and all other strings of Roman numerals as invalid.
  • ## Input
  • - A string containing only Roman numerals (`I`, `V`, `X`, `L`, `C`, `D`, `M`).
  • - The string will have length at least 1.
  • - The string will have length at most 15 (this is the length of the longest valid string of Roman numerals).
  • ## Output
  • - If the input is a valid Roman number, output a consistent value indicating this.
  • - Consistent means that the value must be the same for all valid Roman numbers.
  • - The output for a valid Roman number must not be one of the strings from the list of 50.
  • - If the input is not a valid Roman number, output exactly 1 string from the list of 50.
  • - The output must be a substring of the input.
  • - If the input has 2 or more of the strings from the list of 50 as substrings, you may choose any 1 of them to be the output, but you must choose only 1 of them (you must not output 2 or more).
  • ## Examples
  • ### A valid Roman number
  • The input `MCMXCVI` is the unique correct representation of 1996. It contains none of the 50 substrings.
  • ### A string that is not a valid Roman number
  • Although the input `MMXDIII` might be suspected of representing 2493, it is not the unique correct representation of this number (which is `MMCDXCIII`). Note that it contains the substring `XD`, identifying it as invalid. The only correct output is therefore `XD`.
  • ### An invalid string with more than 1 potential output
  • The input `MMCCMDXXV` contains 2 substrings that make it invalid, so either `CCM` or `CMD` would be correct outputs. It would not be correct to output both of them, or to output their overlap `CCMD`, as this is not one of the 50 strings.
  • ## Test cases
  • Test cases are in the format `INPUT : VALID, OUTPUTS`. Note that only one of the valid outputs can be chosen - outputting 2 or more is incorrect.
  • ```text
  • I : VALID
  • V : VALID
  • X : VALID
  • L : VALID
  • C : VALID
  • D : VALID
  • M : VALID
  • II : VALID
  • VV : VV
  • XX : VALID
  • LL : LL
  • CC : VALID
  • DD : DD
  • MM : VALID
  • III : VALID
  • VII : VALID
  • IVI : IVI
  • IIV : IIV
  • CCI : VALID
  • CCV : VALID
  • CCX : VALID
  • CCL : VALID
  • CCC : VALID
  • CCD : CCD
  • CCM : CCM
  • IIII : IIII
  • MLDI : LD
  • MXXC : XXC
  • DCIIX : IIX
  • MCXXXX : XXXX
  • MCCCCXVI : CCCC
  • MMLXCVII : LXC
  • MMMCMXCIX : VALID
  • MMMDCCCLXXXVIII : VALID
  • MMCCCXLV : VALID
  • MCMXCVI : VALID
  • MMXDIII : XD
  • MMCDXCIII : VALID
  • MMCCMDXXV : CCM, CMD
  • ```
  • ## Scoring
  • This is a [code golf challenge]. Your score is the number of bytes in your code. Lowest score for each language wins.
  • > Explanations are optional, but I'm more likely to upvote answers that have one.
  • [code golf challenge]: https://codegolf.codidact.com/categories/49/tags/4274 "The code-golf tag"
  • Given a string of Roman numerals, decide whether it forms a valid Roman number. If not, output the substring that proves this, from the list of 50 strings described below.
  • ## Relevant fact
  • This challenge is based around the following fact:
  • > A string of Roman numerals is a valid Roman number if and only if it contains none of the following 50 strings as a substring:
  • >
  • > ```text
  • > "CCCC", "CCD", "CCM", "CDC", "CMC", "CMD", "CMM", "DCD", "DCM", "DD", "DM", "IC", "ID", "IIII", "IIV", "IIX", "IL", "IM", "IVI", "IXC", "IXI", "IXL", "IXV", "IXX", "LC", "LD", "LL", "LM", "LXC", "LXL", "MMMM", "VC", "VD", "VIV", "VIX", "VL", "VM", "VV", "VX", "XCC", "XCD", "XCL", "XCM", "XCX", "XD", "XLX", "XM", "XXC", "XXL", "XXXX"
  • > ```
  • This works for any length string, if a valid Roman number is defined as follows:
  • ## Valid Roman numbers
  • - Each numeral appears no more than 3 times consecutively.
  • - Each of `V` (5), `L` (50), and `D` (500) appears no more than once consecutively.
  • - A Roman number is constructed by concatenating the strings representing its ***thousands***, ***hundreds***, ***tens***, and ***units*** components.
  • ### Thousands
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 1000 | "M"
  • 2000 | "MM"
  • 3000 | "MMM"
  • ### Hundreds
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 100 | "C"
  • 200 | "CC"
  • 300 | "CCC"
  • 400 | "CD"
  • 500 | "D"
  • 600 | "DC"
  • 700 | "DCC"
  • 800 | "DCCC"
  • 900 | "CM"
  • ### Tens
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 10 | "X"
  • 20 | "XX"
  • 30 | "XXX"
  • 40 | "XL"
  • 50 | "L"
  • 60 | "LX"
  • 70 | "LXX"
  • 80 | "LXXX"
  • 90 | "XC"
  • ### Units
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 1 | "I"
  • 2 | "II"
  • 3 | "III"
  • 4 | "IV"
  • 5 | "V"
  • 6 | "VI"
  • 7 | "VII"
  • 8 | "VIII"
  • 9 | "IX"
  • So, for example, 2345 would be represented as the concatenation of `MM` (for 2000), `CCC` (for 300), `XL` (for 40), and `V` (for 5), or `MMCCCXLV`.
  • This defines a unique correct representation for each number from 1 to 3999 (4000 and above are not representable without breaking the rule that a numeral cannot appear more than 3 times consecutively). The 50 substrings method described above will identify each of these 3999 strings as valid, and all other strings of Roman numerals as invalid.
  • ## Input
  • - A string containing only Roman numerals (`I`, `V`, `X`, `L`, `C`, `D`, `M`).
  • - The string will have length at least 1.
  • - The string will have length at most 15 (this is the length of the longest valid string of Roman numerals).
  • ## Output
  • - If the input is a valid Roman number, output a consistent value indicating this.
  • - Consistent means that the value must be the same for all valid Roman numbers.
  • - The output for a valid Roman number must not be one of the strings from the list of 50.
  • - If the input is not a valid Roman number, output exactly 1 string from the list of 50.
  • - The output must be a substring of the input.
  • - If the input has 2 or more of the strings from the list of 50 as substrings, you may choose any 1 of them to be the output, but you must choose only 1 of them (you must not output 2 or more).
  • ## Examples
  • ### A valid Roman number
  • The input `MCMXCVI` is the unique correct representation of 1996. It contains none of the 50 strings.
  • ### A string that is not a valid Roman number
  • Although the input `MMXDIII` might be suspected of representing 2493, it is not the unique correct representation of this number (which is `MMCDXCIII`). Note that it has `XD` as a substring, identifying it as invalid. The only correct output is therefore `XD`.
  • ### An invalid string with more than 1 potential output
  • The input `MMCCMDXXV` has 2 substrings that make it invalid, so either `CCM` or `CMD` would be correct outputs. It would not be correct to output both of these, or to output their overlap `CCMD`, as this is not one of the 50 strings.
  • ## Test cases
  • Test cases are in the format `INPUT : VALID, OUTPUTS`. Note that only one of the valid outputs can be chosen - outputting 2 or more is incorrect.
  • ```text
  • I : VALID
  • V : VALID
  • X : VALID
  • L : VALID
  • C : VALID
  • D : VALID
  • M : VALID
  • II : VALID
  • VV : VV
  • XX : VALID
  • LL : LL
  • CC : VALID
  • DD : DD
  • MM : VALID
  • III : VALID
  • VII : VALID
  • IVI : IVI
  • IIV : IIV
  • CCI : VALID
  • CCV : VALID
  • CCX : VALID
  • CCL : VALID
  • CCC : VALID
  • CCD : CCD
  • CCM : CCM
  • IIII : IIII
  • MLDI : LD
  • MXXC : XXC
  • DCIIX : IIX
  • MCXXXX : XXXX
  • MCCCCXVI : CCCC
  • MMLXCVII : LXC
  • MMMCMXCIX : VALID
  • MMMDCCCLXXXVIII : VALID
  • MMCCCXLV : VALID
  • MCMXCVI : VALID
  • MMXDIII : XD
  • MMCDXCIII : VALID
  • MMCCMDXXV : CCM, CMD
  • ```
  • ## Scoring
  • This is a [code golf challenge]. Your score is the number of bytes in your code. Lowest score for each language wins.
  • > Explanations are optional, but I'm more likely to upvote answers that have one.
  • [code golf challenge]: https://codegolf.codidact.com/categories/49/tags/4274 "The code-golf tag"
#13: Post edited by user avatar trichoplax‭ · 2024-03-06T23:26:36Z (9 months ago)
Change output requirements to require use of the 50 strings
  • Given a string of Roman numerals, decide whether it forms a valid Roman number.
  • ## Methods
  • - You could do this by constructing all of the valid Roman numbers, and seeing if the input string of Roman numerals is one of them.
  • - Alternatively, you could use the following trick that does not involve any addition or subtraction, only checking for substrings:
  • **A string of Roman numerals is a valid Roman number if and only if it contains none of the following 50 strings as a substring:**
  • ```text
  • CCCC
  • CCD
  • CCM
  • CDC
  • CMC
  • CMD
  • CMM
  • DCD
  • DCM
  • DD
  • DM
  • IC
  • ID
  • IIII
  • IIV
  • IIX
  • IL
  • IM
  • IVI
  • IXC
  • IXI
  • IXL
  • IXV
  • IXX
  • LC
  • LD
  • LL
  • LM
  • LXC
  • LXL
  • MMMM
  • VC
  • VD
  • VIV
  • VIX
  • VL
  • VM
  • VV
  • VX
  • XCC
  • XCD
  • XCL
  • XCM
  • XCX
  • XD
  • XLX
  • XM
  • XXC
  • XXL
  • XXXX
  • ```
  • This works for any length string, if a valid Roman number is defined as follows:
  • ## Valid Roman numbers
  • - Each numeral appears no more than 3 times consecutively.
  • - Each of `V` (5), `L` (50), and `D` (500) appears no more than once consecutively.
  • - A Roman number is constructed by concatenating the strings representing its ***thousands***, ***hundreds***, ***tens***, and ***units*** components:
  • - ***Thousands***
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 1000 | "M"
  • 2000 | "MM"
  • 3000 | "MMM"
  • - ***Hundreds***
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 100 | "C"
  • 200 | "CC"
  • 300 | "CCC"
  • 400 | "CD"
  • 500 | "D"
  • 600 | "DC"
  • 700 | "DCC"
  • 800 | "DCCC"
  • 900 | "CM"
  • - ***Tens***
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 10 | "X"
  • 20 | "XX"
  • 30 | "XXX"
  • 40 | "XL"
  • 50 | "L"
  • 60 | "LX"
  • 70 | "LXX"
  • 80 | "LXXX"
  • 90 | "XC"
  • - ***Units***
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 1 | "I"
  • 2 | "II"
  • 3 | "III"
  • 4 | "IV"
  • 5 | "V"
  • 6 | "VI"
  • 7 | "VII"
  • 8 | "VIII"
  • 9 | "IX"
  • So for example, 2345 would be represented as the concatenation of `MM` (for 2000), `CCC` (for 300), `XL` (for 40), and `V` (for 5), or `MMCCCXLV`.
  • This defines a unique correct representation for each number from 1 to 3999 (4000 and above are not representable without breaking the rule that a numeral cannot appear more than 3 times consecutively). The 50 substrings method described above will identify each of these 3999 strings as valid, and all other strings of Roman numerals as invalid.
  • ## Input
  • - A string containing only Roman numerals (`I`, `V`, `X`, `L`, `C`, `D`, `M`).
  • - The string will have length at least 1.
  • - The string will have length at most 15 (this is the length of the longest valid string of Roman numerals).
  • ## Output
  • - One of 2 distinct values indicating whether the input is one of the 3999 valid Roman numeral strings.
  • - This may be calculated however you choose - you do not need to use either of the methods mentioned above.
  • - The results must be consistent with both methods mentioned above.
  • ## Examples
  • ### A valid Roman number
  • The input `MCMXCVI` is the unique correct representation of 1996. It contains none of the 50 substrings.
  • ### A string that is not a valid Roman number
  • Although the input `MMXDIII` might be suspected of representing 2493, it is not the unique correct representation of this number (which is `MMCDXCIII`). Note that it contains the substring `XD`, identifying it as invalid.
  • ## Test cases
  • Test cases are in the format `INPUT : output`.
  • ```text
  • I : valid
  • V : valid
  • X : valid
  • L : valid
  • C : valid
  • D : valid
  • M : valid
  • II : valid
  • VV : invalid
  • XX : valid
  • LL : invalid
  • CC : valid
  • DD : invalid
  • MM : valid
  • III : valid
  • VII : valid
  • IVI : invalid
  • IIV : invalid
  • CCI : valid
  • CCV : valid
  • CCX : valid
  • CCL : valid
  • CCC : valid
  • CCD : invalid
  • CCM : invalid
  • IIII : invalid
  • MLDI : invalid
  • MXXC : invalid
  • DCIIX : invalid
  • MCXXXX : invalid
  • MCCCCXVI : invalid
  • MMLXCVII : invalid
  • MMMCMXCIX : valid
  • MMMDCCCLXXXVIII : valid
  • MMCCCXLV : valid
  • MCMXCVI : valid
  • MMXDIII : invalid
  • MMCDXCIII : valid
  • ```
  • ## Scoring
  • This is a [code golf challenge]. Your score is the number of bytes in your code. Lowest score for each language wins.
  • I'm interested to see which approach gives the best score in each language. 50 substrings are a lot to check for, but are the repetitive patterns in them sufficient to make the approach competitive?
  • > Explanations are optional, but I'm more likely to upvote answers that have one.
  • [code golf challenge]: https://codegolf.codidact.com/categories/49/tags/4274 "The code-golf tag"
  • Given a string of Roman numerals, decide whether it forms a valid Roman number. If not, output the substring that proves this, from the list of 50 strings described below.
  • This challenge is based around the following fact:
  • > A string of Roman numerals is a valid Roman number if and only if it contains none of the following 50 strings as a substring:
  • >
  • > ```text
  • > "CCCC", "CCD", "CCM", "CDC", "CMC", "CMD", "CMM", "DCD", "DCM", "DD", "DM", "IC", "ID", "IIII", "IIV", "IIX", "IL", "IM", "IVI", "IXC", "IXI", "IXL", "IXV", "IXX", "LC", "LD", "LL", "LM", "LXC", "LXL", "MMMM", "VC", "VD", "VIV", "VIX", "VL", "VM", "VV", "VX", "XCC", "XCD", "XCL", "XCM", "XCX", "XD", "XLX", "XM", "XXC", "XXL", "XXXX"
  • > ```
  • This works for any length string, if a valid Roman number is defined as follows:
  • ## Valid Roman numbers
  • - Each numeral appears no more than 3 times consecutively.
  • - Each of `V` (5), `L` (50), and `D` (500) appears no more than once consecutively.
  • - A Roman number is constructed by concatenating the strings representing its ***thousands***, ***hundreds***, ***tens***, and ***units*** components.
  • ### Thousands
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 1000 | "M"
  • 2000 | "MM"
  • 3000 | "MMM"
  • ### Hundreds
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 100 | "C"
  • 200 | "CC"
  • 300 | "CCC"
  • 400 | "CD"
  • 500 | "D"
  • 600 | "DC"
  • 700 | "DCC"
  • 800 | "DCCC"
  • 900 | "CM"
  • ### Tens
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 10 | "X"
  • 20 | "XX"
  • 30 | "XXX"
  • 40 | "XL"
  • 50 | "L"
  • 60 | "LX"
  • 70 | "LXX"
  • 80 | "LXXX"
  • 90 | "XC"
  • ### Units
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 1 | "I"
  • 2 | "II"
  • 3 | "III"
  • 4 | "IV"
  • 5 | "V"
  • 6 | "VI"
  • 7 | "VII"
  • 8 | "VIII"
  • 9 | "IX"
  • So, for example, 2345 would be represented as the concatenation of `MM` (for 2000), `CCC` (for 300), `XL` (for 40), and `V` (for 5), or `MMCCCXLV`.
  • This defines a unique correct representation for each number from 1 to 3999 (4000 and above are not representable without breaking the rule that a numeral cannot appear more than 3 times consecutively). The 50 substrings method described above will identify each of these 3999 strings as valid, and all other strings of Roman numerals as invalid.
  • ## Input
  • - A string containing only Roman numerals (`I`, `V`, `X`, `L`, `C`, `D`, `M`).
  • - The string will have length at least 1.
  • - The string will have length at most 15 (this is the length of the longest valid string of Roman numerals).
  • ## Output
  • - If the input is a valid Roman number, output a consistent value indicating this.
  • - Consistent means that the value must be the same for all valid Roman numbers.
  • - The output for a valid Roman number must not be one of the strings from the list of 50.
  • - If the input is not a valid Roman number, output exactly 1 string from the list of 50.
  • - The output must be a substring of the input.
  • - If the input has 2 or more of the strings from the list of 50 as substrings, you may choose any 1 of them to be the output, but you must choose only 1 of them (you must not output 2 or more).
  • ## Examples
  • ### A valid Roman number
  • The input `MCMXCVI` is the unique correct representation of 1996. It contains none of the 50 substrings.
  • ### A string that is not a valid Roman number
  • Although the input `MMXDIII` might be suspected of representing 2493, it is not the unique correct representation of this number (which is `MMCDXCIII`). Note that it contains the substring `XD`, identifying it as invalid. The only correct output is therefore `XD`.
  • ### An invalid string with more than 1 potential output
  • The input `MMCCMDXXV` contains 2 substrings that make it invalid, so either `CCM` or `CMD` would be correct outputs. It would not be correct to output both of them, or to output their overlap `CCMD`, as this is not one of the 50 strings.
  • ## Test cases
  • Test cases are in the format `INPUT : VALID, OUTPUTS`. Note that only one of the valid outputs can be chosen - outputting 2 or more is incorrect.
  • ```text
  • I : VALID
  • V : VALID
  • X : VALID
  • L : VALID
  • C : VALID
  • D : VALID
  • M : VALID
  • II : VALID
  • VV : VV
  • XX : VALID
  • LL : LL
  • CC : VALID
  • DD : DD
  • MM : VALID
  • III : VALID
  • VII : VALID
  • IVI : IVI
  • IIV : IIV
  • CCI : VALID
  • CCV : VALID
  • CCX : VALID
  • CCL : VALID
  • CCC : VALID
  • CCD : CCD
  • CCM : CCM
  • IIII : IIII
  • MLDI : LD
  • MXXC : XXC
  • DCIIX : IIX
  • MCXXXX : XXXX
  • MCCCCXVI : CCCC
  • MMLXCVII : LXC
  • MMMCMXCIX : VALID
  • MMMDCCCLXXXVIII : VALID
  • MMCCCXLV : VALID
  • MCMXCVI : VALID
  • MMXDIII : XD
  • MMCDXCIII : VALID
  • MMCCMDXXV : CCM, CMD
  • ```
  • ## Scoring
  • This is a [code golf challenge]. Your score is the number of bytes in your code. Lowest score for each language wins.
  • > Explanations are optional, but I'm more likely to upvote answers that have one.
  • [code golf challenge]: https://codegolf.codidact.com/categories/49/tags/4274 "The code-golf tag"
#12: Post edited by user avatar trichoplax‭ · 2024-03-06T10:42:09Z (9 months ago)
Rearrange wording of valid Roman numerals section for clarity
  • Given a string of Roman numerals, decide whether it forms a valid Roman number.
  • ## Methods
  • - You could do this by constructing all of the valid Roman numbers, and seeing if the input string of Roman numerals is one of them.
  • - Alternatively, you could use the following trick that does not involve any addition or subtraction, only checking for substrings:
  • **A string of Roman numerals is a valid Roman number if and only if it contains none of the following 50 strings as a substring:**
  • ```text
  • CCCC
  • CCD
  • CCM
  • CDC
  • CMC
  • CMD
  • CMM
  • DCD
  • DCM
  • DD
  • DM
  • IC
  • ID
  • IIII
  • IIV
  • IIX
  • IL
  • IM
  • IVI
  • IXC
  • IXI
  • IXL
  • IXV
  • IXX
  • LC
  • LD
  • LL
  • LM
  • LXC
  • LXL
  • MMMM
  • VC
  • VD
  • VIV
  • VIX
  • VL
  • VM
  • VV
  • VX
  • XCC
  • XCD
  • XCL
  • XCM
  • XCX
  • XD
  • XLX
  • XM
  • XXC
  • XXL
  • XXXX
  • ```
  • This works for any length string, if a valid Roman number is defined as follows:
  • ## Valid Roman numbers
  • - No numeral appears more than 3 times consecutively.
  • - Each of `V` (5), `L` (50), and `D` (500) appear only once.
  • - A Roman number is constructed by concatenating the strings representing its ***thousands***, ***hundreds***, ***tens***, and ***units*** components:
  • - ***Units***
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 1 | "I"
  • 2 | "II"
  • 3 | "III"
  • 4 | "IV"
  • 5 | "V"
  • 6 | "VI"
  • 7 | "VII"
  • 8 | "VIII"
  • 9 | "IX"
  • - ***Tens***
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 10 | "X"
  • 20 | "XX"
  • 30 | "XXX"
  • 40 | "XL"
  • 50 | "L"
  • 60 | "LX"
  • 70 | "LXX"
  • 80 | "LXXX"
  • 90 | "XC"
  • - ***Hundreds***
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 100 | "C"
  • 200 | "CC"
  • 300 | "CCC"
  • 400 | "CD"
  • 500 | "D"
  • 600 | "DC"
  • 700 | "DCC"
  • 800 | "DCCC"
  • 900 | "CM"
  • - ***Thousands***
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 1000 | "M"
  • 2000 | "MM"
  • 3000 | "MMM"
  • So for example, 2345 would be represented as the concatenation of `MM` (for 2000), `CCC` (for 300), `XL` (for 40), and `V` (for 5), or `MMCCCXLV`.
  • This defines a unique correct representation for each number from 1 to 3999 (4000 and above are not representable without breaking the rule that a numeral cannot appear more than 3 times consecutively). The 50 substrings method described above will identify each of these 3999 strings as valid, and all other strings of Roman numerals as invalid.
  • ## Input
  • - A string containing only Roman numerals (`I`, `V`, `X`, `L`, `C`, `D`, `M`).
  • - The string will have length at least 1.
  • - The string will have length at most 15 (this is the length of the longest valid string of Roman numerals).
  • ## Output
  • - One of 2 distinct values indicating whether the input is one of the 3999 valid Roman numeral strings.
  • - This may be calculated however you choose - you do not need to use either of the methods mentioned above.
  • - The results must be consistent with both methods mentioned above.
  • ## Examples
  • ### A valid Roman number
  • The input `MCMXCVI` is the unique correct representation of 1996. It contains none of the 50 substrings.
  • ### A string that is not a valid Roman number
  • Although the input `MMXDIII` might be suspected of representing 2493, it is not the unique correct representation of this number (which is `MMCDXCIII`). Note that it contains the substring `XD`, identifying it as invalid.
  • ## Test cases
  • Test cases are in the format `INPUT : output`.
  • ```text
  • I : valid
  • V : valid
  • X : valid
  • L : valid
  • C : valid
  • D : valid
  • M : valid
  • II : valid
  • VV : invalid
  • XX : valid
  • LL : invalid
  • CC : valid
  • DD : invalid
  • MM : valid
  • III : valid
  • VII : valid
  • IVI : invalid
  • IIV : invalid
  • CCI : valid
  • CCV : valid
  • CCX : valid
  • CCL : valid
  • CCC : valid
  • CCD : invalid
  • CCM : invalid
  • IIII : invalid
  • MLDI : invalid
  • MXXC : invalid
  • DCIIX : invalid
  • MCXXXX : invalid
  • MCCCCXVI : invalid
  • MMLXCVII : invalid
  • MMMCMXCIX : valid
  • MMMDCCCLXXXVIII : valid
  • MMCCCXLV : valid
  • MCMXCVI : valid
  • MMXDIII : invalid
  • MMCDXCIII : valid
  • ```
  • ## Scoring
  • This is a [code golf challenge]. Your score is the number of bytes in your code. Lowest score for each language wins.
  • I'm interested to see which approach gives the best score in each language. 50 substrings are a lot to check for, but are the repetitive patterns in them sufficient to make the approach competitive?
  • > Explanations are optional, but I'm more likely to upvote answers that have one.
  • [code golf challenge]: https://codegolf.codidact.com/categories/49/tags/4274 "The code-golf tag"
  • Given a string of Roman numerals, decide whether it forms a valid Roman number.
  • ## Methods
  • - You could do this by constructing all of the valid Roman numbers, and seeing if the input string of Roman numerals is one of them.
  • - Alternatively, you could use the following trick that does not involve any addition or subtraction, only checking for substrings:
  • **A string of Roman numerals is a valid Roman number if and only if it contains none of the following 50 strings as a substring:**
  • ```text
  • CCCC
  • CCD
  • CCM
  • CDC
  • CMC
  • CMD
  • CMM
  • DCD
  • DCM
  • DD
  • DM
  • IC
  • ID
  • IIII
  • IIV
  • IIX
  • IL
  • IM
  • IVI
  • IXC
  • IXI
  • IXL
  • IXV
  • IXX
  • LC
  • LD
  • LL
  • LM
  • LXC
  • LXL
  • MMMM
  • VC
  • VD
  • VIV
  • VIX
  • VL
  • VM
  • VV
  • VX
  • XCC
  • XCD
  • XCL
  • XCM
  • XCX
  • XD
  • XLX
  • XM
  • XXC
  • XXL
  • XXXX
  • ```
  • This works for any length string, if a valid Roman number is defined as follows:
  • ## Valid Roman numbers
  • - Each numeral appears no more than 3 times consecutively.
  • - Each of `V` (5), `L` (50), and `D` (500) appears no more than once consecutively.
  • - A Roman number is constructed by concatenating the strings representing its ***thousands***, ***hundreds***, ***tens***, and ***units*** components:
  • - ***Thousands***
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 1000 | "M"
  • 2000 | "MM"
  • 3000 | "MMM"
  • - ***Hundreds***
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 100 | "C"
  • 200 | "CC"
  • 300 | "CCC"
  • 400 | "CD"
  • 500 | "D"
  • 600 | "DC"
  • 700 | "DCC"
  • 800 | "DCCC"
  • 900 | "CM"
  • - ***Tens***
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 10 | "X"
  • 20 | "XX"
  • 30 | "XXX"
  • 40 | "XL"
  • 50 | "L"
  • 60 | "LX"
  • 70 | "LXX"
  • 80 | "LXXX"
  • 90 | "XC"
  • - ***Units***
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 1 | "I"
  • 2 | "II"
  • 3 | "III"
  • 4 | "IV"
  • 5 | "V"
  • 6 | "VI"
  • 7 | "VII"
  • 8 | "VIII"
  • 9 | "IX"
  • So for example, 2345 would be represented as the concatenation of `MM` (for 2000), `CCC` (for 300), `XL` (for 40), and `V` (for 5), or `MMCCCXLV`.
  • This defines a unique correct representation for each number from 1 to 3999 (4000 and above are not representable without breaking the rule that a numeral cannot appear more than 3 times consecutively). The 50 substrings method described above will identify each of these 3999 strings as valid, and all other strings of Roman numerals as invalid.
  • ## Input
  • - A string containing only Roman numerals (`I`, `V`, `X`, `L`, `C`, `D`, `M`).
  • - The string will have length at least 1.
  • - The string will have length at most 15 (this is the length of the longest valid string of Roman numerals).
  • ## Output
  • - One of 2 distinct values indicating whether the input is one of the 3999 valid Roman numeral strings.
  • - This may be calculated however you choose - you do not need to use either of the methods mentioned above.
  • - The results must be consistent with both methods mentioned above.
  • ## Examples
  • ### A valid Roman number
  • The input `MCMXCVI` is the unique correct representation of 1996. It contains none of the 50 substrings.
  • ### A string that is not a valid Roman number
  • Although the input `MMXDIII` might be suspected of representing 2493, it is not the unique correct representation of this number (which is `MMCDXCIII`). Note that it contains the substring `XD`, identifying it as invalid.
  • ## Test cases
  • Test cases are in the format `INPUT : output`.
  • ```text
  • I : valid
  • V : valid
  • X : valid
  • L : valid
  • C : valid
  • D : valid
  • M : valid
  • II : valid
  • VV : invalid
  • XX : valid
  • LL : invalid
  • CC : valid
  • DD : invalid
  • MM : valid
  • III : valid
  • VII : valid
  • IVI : invalid
  • IIV : invalid
  • CCI : valid
  • CCV : valid
  • CCX : valid
  • CCL : valid
  • CCC : valid
  • CCD : invalid
  • CCM : invalid
  • IIII : invalid
  • MLDI : invalid
  • MXXC : invalid
  • DCIIX : invalid
  • MCXXXX : invalid
  • MCCCCXVI : invalid
  • MMLXCVII : invalid
  • MMMCMXCIX : valid
  • MMMDCCCLXXXVIII : valid
  • MMCCCXLV : valid
  • MCMXCVI : valid
  • MMXDIII : invalid
  • MMCDXCIII : valid
  • ```
  • ## Scoring
  • This is a [code golf challenge]. Your score is the number of bytes in your code. Lowest score for each language wins.
  • I'm interested to see which approach gives the best score in each language. 50 substrings are a lot to check for, but are the repetitive patterns in them sufficient to make the approach competitive?
  • > Explanations are optional, but I'm more likely to upvote answers that have one.
  • [code golf challenge]: https://codegolf.codidact.com/categories/49/tags/4274 "The code-golf tag"
#11: Post edited by user avatar trichoplax‭ · 2024-03-06T01:33:01Z (9 months ago)
Include all examples in the test cases
  • Given a string of Roman numerals, decide whether it forms a valid Roman number.
  • ## Methods
  • - You could do this by constructing all of the valid Roman numbers, and seeing if the input string of Roman numerals is one of them.
  • - Alternatively, you could use the following trick that does not involve any addition or subtraction, only checking for substrings:
  • **A string of Roman numerals is a valid Roman number if and only if it contains none of the following 50 strings as a substring:**
  • ```text
  • CCCC
  • CCD
  • CCM
  • CDC
  • CMC
  • CMD
  • CMM
  • DCD
  • DCM
  • DD
  • DM
  • IC
  • ID
  • IIII
  • IIV
  • IIX
  • IL
  • IM
  • IVI
  • IXC
  • IXI
  • IXL
  • IXV
  • IXX
  • LC
  • LD
  • LL
  • LM
  • LXC
  • LXL
  • MMMM
  • VC
  • VD
  • VIV
  • VIX
  • VL
  • VM
  • VV
  • VX
  • XCC
  • XCD
  • XCL
  • XCM
  • XCX
  • XD
  • XLX
  • XM
  • XXC
  • XXL
  • XXXX
  • ```
  • This works for any length string, if a valid Roman number is defined as follows:
  • ## Valid Roman numbers
  • - No numeral appears more than 3 times consecutively.
  • - Each of `V` (5), `L` (50), and `D` (500) appear only once.
  • - A Roman number is constructed by concatenating the strings representing its ***thousands***, ***hundreds***, ***tens***, and ***units*** components:
  • - ***Units***
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 1 | "I"
  • 2 | "II"
  • 3 | "III"
  • 4 | "IV"
  • 5 | "V"
  • 6 | "VI"
  • 7 | "VII"
  • 8 | "VIII"
  • 9 | "IX"
  • - ***Tens***
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 10 | "X"
  • 20 | "XX"
  • 30 | "XXX"
  • 40 | "XL"
  • 50 | "L"
  • 60 | "LX"
  • 70 | "LXX"
  • 80 | "LXXX"
  • 90 | "XC"
  • - ***Hundreds***
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 100 | "C"
  • 200 | "CC"
  • 300 | "CCC"
  • 400 | "CD"
  • 500 | "D"
  • 600 | "DC"
  • 700 | "DCC"
  • 800 | "DCCC"
  • 900 | "CM"
  • - ***Thousands***
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 1000 | "M"
  • 2000 | "MM"
  • 3000 | "MMM"
  • So for example, 2345 would be represented as the concatenation of `MM` (for 2000), `CCC` (for 300), `XL` (for 40), and `V` (for 5), or `MMCCCXLV`.
  • This defines a unique correct representation for each number from 1 to 3999 (4000 and above are not representable without breaking the rule that a numeral cannot appear more than 3 times consecutively). The 50 substrings method described above will identify each of these 3999 strings as valid, and all other strings of Roman numerals as invalid.
  • ## Input
  • - A string containing only Roman numerals (`I`, `V`, `X`, `L`, `C`, `D`, `M`).
  • - The string will have length at least 1.
  • - The string will have length at most 15 (this is the length of the longest valid string of Roman numerals).
  • ## Output
  • - One of 2 distinct values indicating whether the input is one of the 3999 valid Roman numeral strings.
  • - This may be calculated however you choose - you do not need to use either of the methods mentioned above.
  • - The results must be consistent with both methods mentioned above.
  • ## Examples
  • ### A valid Roman number
  • The input `MCMXCVI` is the unique correct representation of 1996. It contains none of the 50 substrings.
  • ### A string that is not a valid Roman number
  • Although the input `MMXDIII` might be suspected of representing 2493, it is not the unique correct representation of this number (which is `MMCDXCIII`). Note that it contains the substring `XD`, identifying it as invalid.
  • ## Test cases
  • Test cases are in the format `INPUT : output`.
  • ```text
  • I : valid
  • V : valid
  • X : valid
  • L : valid
  • C : valid
  • D : valid
  • M : valid
  • II : valid
  • VV : invalid
  • XX : valid
  • LL : invalid
  • CC : valid
  • DD : invalid
  • MM : valid
  • III : valid
  • VII : valid
  • IVI : invalid
  • IIV : invalid
  • CCI : valid
  • CCV : valid
  • CCX : valid
  • CCL : valid
  • CCC : valid
  • CCD : invalid
  • CCM : invalid
  • IIII : invalid
  • MLDI : invalid
  • MXXC : invalid
  • DCIIX : invalid
  • MCXXXX : invalid
  • MCCCCXVI : invalid
  • MMLXCVII : invalid
  • MMMCMXCIX : valid
  • MMMDCCCLXXXVIII : valid
  • ```
  • ## Scoring
  • This is a [code golf challenge]. Your score is the number of bytes in your code. Lowest score for each language wins.
  • I'm interested to see which approach gives the best score in each language. 50 substrings are a lot to check for, but are the repetitive patterns in them sufficient to make the approach competitive?
  • > Explanations are optional, but I'm more likely to upvote answers that have one.
  • [code golf challenge]: https://codegolf.codidact.com/categories/49/tags/4274 "The code-golf tag"
  • Given a string of Roman numerals, decide whether it forms a valid Roman number.
  • ## Methods
  • - You could do this by constructing all of the valid Roman numbers, and seeing if the input string of Roman numerals is one of them.
  • - Alternatively, you could use the following trick that does not involve any addition or subtraction, only checking for substrings:
  • **A string of Roman numerals is a valid Roman number if and only if it contains none of the following 50 strings as a substring:**
  • ```text
  • CCCC
  • CCD
  • CCM
  • CDC
  • CMC
  • CMD
  • CMM
  • DCD
  • DCM
  • DD
  • DM
  • IC
  • ID
  • IIII
  • IIV
  • IIX
  • IL
  • IM
  • IVI
  • IXC
  • IXI
  • IXL
  • IXV
  • IXX
  • LC
  • LD
  • LL
  • LM
  • LXC
  • LXL
  • MMMM
  • VC
  • VD
  • VIV
  • VIX
  • VL
  • VM
  • VV
  • VX
  • XCC
  • XCD
  • XCL
  • XCM
  • XCX
  • XD
  • XLX
  • XM
  • XXC
  • XXL
  • XXXX
  • ```
  • This works for any length string, if a valid Roman number is defined as follows:
  • ## Valid Roman numbers
  • - No numeral appears more than 3 times consecutively.
  • - Each of `V` (5), `L` (50), and `D` (500) appear only once.
  • - A Roman number is constructed by concatenating the strings representing its ***thousands***, ***hundreds***, ***tens***, and ***units*** components:
  • - ***Units***
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 1 | "I"
  • 2 | "II"
  • 3 | "III"
  • 4 | "IV"
  • 5 | "V"
  • 6 | "VI"
  • 7 | "VII"
  • 8 | "VIII"
  • 9 | "IX"
  • - ***Tens***
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 10 | "X"
  • 20 | "XX"
  • 30 | "XXX"
  • 40 | "XL"
  • 50 | "L"
  • 60 | "LX"
  • 70 | "LXX"
  • 80 | "LXXX"
  • 90 | "XC"
  • - ***Hundreds***
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 100 | "C"
  • 200 | "CC"
  • 300 | "CCC"
  • 400 | "CD"
  • 500 | "D"
  • 600 | "DC"
  • 700 | "DCC"
  • 800 | "DCCC"
  • 900 | "CM"
  • - ***Thousands***
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 1000 | "M"
  • 2000 | "MM"
  • 3000 | "MMM"
  • So for example, 2345 would be represented as the concatenation of `MM` (for 2000), `CCC` (for 300), `XL` (for 40), and `V` (for 5), or `MMCCCXLV`.
  • This defines a unique correct representation for each number from 1 to 3999 (4000 and above are not representable without breaking the rule that a numeral cannot appear more than 3 times consecutively). The 50 substrings method described above will identify each of these 3999 strings as valid, and all other strings of Roman numerals as invalid.
  • ## Input
  • - A string containing only Roman numerals (`I`, `V`, `X`, `L`, `C`, `D`, `M`).
  • - The string will have length at least 1.
  • - The string will have length at most 15 (this is the length of the longest valid string of Roman numerals).
  • ## Output
  • - One of 2 distinct values indicating whether the input is one of the 3999 valid Roman numeral strings.
  • - This may be calculated however you choose - you do not need to use either of the methods mentioned above.
  • - The results must be consistent with both methods mentioned above.
  • ## Examples
  • ### A valid Roman number
  • The input `MCMXCVI` is the unique correct representation of 1996. It contains none of the 50 substrings.
  • ### A string that is not a valid Roman number
  • Although the input `MMXDIII` might be suspected of representing 2493, it is not the unique correct representation of this number (which is `MMCDXCIII`). Note that it contains the substring `XD`, identifying it as invalid.
  • ## Test cases
  • Test cases are in the format `INPUT : output`.
  • ```text
  • I : valid
  • V : valid
  • X : valid
  • L : valid
  • C : valid
  • D : valid
  • M : valid
  • II : valid
  • VV : invalid
  • XX : valid
  • LL : invalid
  • CC : valid
  • DD : invalid
  • MM : valid
  • III : valid
  • VII : valid
  • IVI : invalid
  • IIV : invalid
  • CCI : valid
  • CCV : valid
  • CCX : valid
  • CCL : valid
  • CCC : valid
  • CCD : invalid
  • CCM : invalid
  • IIII : invalid
  • MLDI : invalid
  • MXXC : invalid
  • DCIIX : invalid
  • MCXXXX : invalid
  • MCCCCXVI : invalid
  • MMLXCVII : invalid
  • MMMCMXCIX : valid
  • MMMDCCCLXXXVIII : valid
  • MMCCCXLV : valid
  • MCMXCVI : valid
  • MMXDIII : invalid
  • MMCDXCIII : valid
  • ```
  • ## Scoring
  • This is a [code golf challenge]. Your score is the number of bytes in your code. Lowest score for each language wins.
  • I'm interested to see which approach gives the best score in each language. 50 substrings are a lot to check for, but are the repetitive patterns in them sufficient to make the approach competitive?
  • > Explanations are optional, but I'm more likely to upvote answers that have one.
  • [code golf challenge]: https://codegolf.codidact.com/categories/49/tags/4274 "The code-golf tag"
#10: Post edited by user avatar trichoplax‭ · 2024-03-06T01:25:01Z (9 months ago)
Clarify input section
  • Given a string of Roman numerals, decide whether it forms a valid Roman number.
  • ## Methods
  • - You could do this by constructing all of the valid Roman numbers, and seeing if the input string of Roman numerals is one of them.
  • - Alternatively, you could use the following trick that does not involve any addition or subtraction, only checking for substrings:
  • **A string of Roman numerals is a valid Roman number if and only if it contains none of the following 50 strings as a substring:**
  • ```text
  • CCCC
  • CCD
  • CCM
  • CDC
  • CMC
  • CMD
  • CMM
  • DCD
  • DCM
  • DD
  • DM
  • IC
  • ID
  • IIII
  • IIV
  • IIX
  • IL
  • IM
  • IVI
  • IXC
  • IXI
  • IXL
  • IXV
  • IXX
  • LC
  • LD
  • LL
  • LM
  • LXC
  • LXL
  • MMMM
  • VC
  • VD
  • VIV
  • VIX
  • VL
  • VM
  • VV
  • VX
  • XCC
  • XCD
  • XCL
  • XCM
  • XCX
  • XD
  • XLX
  • XM
  • XXC
  • XXL
  • XXXX
  • ```
  • This works for any length string, if a valid Roman number is defined as follows:
  • ## Valid Roman numbers
  • - No numeral appears more than 3 times consecutively.
  • - Each of `V` (5), `L` (50), and `D` (500) appear only once.
  • - A Roman number is constructed by concatenating the strings representing its ***thousands***, ***hundreds***, ***tens***, and ***units*** components:
  • - ***Units***
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 1 | "I"
  • 2 | "II"
  • 3 | "III"
  • 4 | "IV"
  • 5 | "V"
  • 6 | "VI"
  • 7 | "VII"
  • 8 | "VIII"
  • 9 | "IX"
  • - ***Tens***
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 10 | "X"
  • 20 | "XX"
  • 30 | "XXX"
  • 40 | "XL"
  • 50 | "L"
  • 60 | "LX"
  • 70 | "LXX"
  • 80 | "LXXX"
  • 90 | "XC"
  • - ***Hundreds***
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 100 | "C"
  • 200 | "CC"
  • 300 | "CCC"
  • 400 | "CD"
  • 500 | "D"
  • 600 | "DC"
  • 700 | "DCC"
  • 800 | "DCCC"
  • 900 | "CM"
  • - ***Thousands***
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 1000 | "M"
  • 2000 | "MM"
  • 3000 | "MMM"
  • So for example, 2345 would be represented as the concatenation of `MM` (for 2000), `CCC` (for 300), `XL` (for 40), and `V` (for 5), or `MMCCCXLV`.
  • This defines a unique correct representation for each number from 1 to 3999 (4000 and above are not representable without breaking the rule that a numeral cannot appear more than 3 times consecutively). The 50 substrings method described above will identify each of these 3999 strings as valid, and all other strings of Roman numerals as invalid.
  • ## Input
  • - A string containing only Roman numerals (`I`, `V`, `X`, `L`, `C`, `D`, `M`).
  • - The string will have length at least 1.
  • - The string will have length at most 15 (this is the length of the longest valid Roman numerals string).
  • ## Output
  • - One of 2 distinct values indicating whether the input is one of the 3999 valid Roman numeral strings.
  • - This may be calculated however you choose - you do not need to use either of the methods mentioned above.
  • - The results must be consistent with both methods mentioned above.
  • ## Examples
  • ### A valid Roman number
  • The input `MCMXCVI` is the unique correct representation of 1996. It contains none of the 50 substrings.
  • ### A string that is not a valid Roman number
  • Although the input `MMXDIII` might be suspected of representing 2493, it is not the unique correct representation of this number (which is `MMCDXCIII`). Note that it contains the substring `XD`, identifying it as invalid.
  • ## Test cases
  • Test cases are in the format `INPUT : output`.
  • ```text
  • I : valid
  • V : valid
  • X : valid
  • L : valid
  • C : valid
  • D : valid
  • M : valid
  • II : valid
  • VV : invalid
  • XX : valid
  • LL : invalid
  • CC : valid
  • DD : invalid
  • MM : valid
  • III : valid
  • VII : valid
  • IVI : invalid
  • IIV : invalid
  • CCI : valid
  • CCV : valid
  • CCX : valid
  • CCL : valid
  • CCC : valid
  • CCD : invalid
  • CCM : invalid
  • IIII : invalid
  • MLDI : invalid
  • MXXC : invalid
  • DCIIX : invalid
  • MCXXXX : invalid
  • MCCCCXVI : invalid
  • MMLXCVII : invalid
  • MMMCMXCIX : valid
  • MMMDCCCLXXXVIII : valid
  • ```
  • ## Scoring
  • This is a [code golf challenge]. Your score is the number of bytes in your code. Lowest score for each language wins.
  • I'm interested to see which approach gives the best score in each language. 50 substrings are a lot to check for, but are the repetitive patterns in them sufficient to make the approach competitive?
  • > Explanations are optional, but I'm more likely to upvote answers that have one.
  • [code golf challenge]: https://codegolf.codidact.com/categories/49/tags/4274 "The code-golf tag"
  • Given a string of Roman numerals, decide whether it forms a valid Roman number.
  • ## Methods
  • - You could do this by constructing all of the valid Roman numbers, and seeing if the input string of Roman numerals is one of them.
  • - Alternatively, you could use the following trick that does not involve any addition or subtraction, only checking for substrings:
  • **A string of Roman numerals is a valid Roman number if and only if it contains none of the following 50 strings as a substring:**
  • ```text
  • CCCC
  • CCD
  • CCM
  • CDC
  • CMC
  • CMD
  • CMM
  • DCD
  • DCM
  • DD
  • DM
  • IC
  • ID
  • IIII
  • IIV
  • IIX
  • IL
  • IM
  • IVI
  • IXC
  • IXI
  • IXL
  • IXV
  • IXX
  • LC
  • LD
  • LL
  • LM
  • LXC
  • LXL
  • MMMM
  • VC
  • VD
  • VIV
  • VIX
  • VL
  • VM
  • VV
  • VX
  • XCC
  • XCD
  • XCL
  • XCM
  • XCX
  • XD
  • XLX
  • XM
  • XXC
  • XXL
  • XXXX
  • ```
  • This works for any length string, if a valid Roman number is defined as follows:
  • ## Valid Roman numbers
  • - No numeral appears more than 3 times consecutively.
  • - Each of `V` (5), `L` (50), and `D` (500) appear only once.
  • - A Roman number is constructed by concatenating the strings representing its ***thousands***, ***hundreds***, ***tens***, and ***units*** components:
  • - ***Units***
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 1 | "I"
  • 2 | "II"
  • 3 | "III"
  • 4 | "IV"
  • 5 | "V"
  • 6 | "VI"
  • 7 | "VII"
  • 8 | "VIII"
  • 9 | "IX"
  • - ***Tens***
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 10 | "X"
  • 20 | "XX"
  • 30 | "XXX"
  • 40 | "XL"
  • 50 | "L"
  • 60 | "LX"
  • 70 | "LXX"
  • 80 | "LXXX"
  • 90 | "XC"
  • - ***Hundreds***
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 100 | "C"
  • 200 | "CC"
  • 300 | "CCC"
  • 400 | "CD"
  • 500 | "D"
  • 600 | "DC"
  • 700 | "DCC"
  • 800 | "DCCC"
  • 900 | "CM"
  • - ***Thousands***
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 1000 | "M"
  • 2000 | "MM"
  • 3000 | "MMM"
  • So for example, 2345 would be represented as the concatenation of `MM` (for 2000), `CCC` (for 300), `XL` (for 40), and `V` (for 5), or `MMCCCXLV`.
  • This defines a unique correct representation for each number from 1 to 3999 (4000 and above are not representable without breaking the rule that a numeral cannot appear more than 3 times consecutively). The 50 substrings method described above will identify each of these 3999 strings as valid, and all other strings of Roman numerals as invalid.
  • ## Input
  • - A string containing only Roman numerals (`I`, `V`, `X`, `L`, `C`, `D`, `M`).
  • - The string will have length at least 1.
  • - The string will have length at most 15 (this is the length of the longest valid string of Roman numerals).
  • ## Output
  • - One of 2 distinct values indicating whether the input is one of the 3999 valid Roman numeral strings.
  • - This may be calculated however you choose - you do not need to use either of the methods mentioned above.
  • - The results must be consistent with both methods mentioned above.
  • ## Examples
  • ### A valid Roman number
  • The input `MCMXCVI` is the unique correct representation of 1996. It contains none of the 50 substrings.
  • ### A string that is not a valid Roman number
  • Although the input `MMXDIII` might be suspected of representing 2493, it is not the unique correct representation of this number (which is `MMCDXCIII`). Note that it contains the substring `XD`, identifying it as invalid.
  • ## Test cases
  • Test cases are in the format `INPUT : output`.
  • ```text
  • I : valid
  • V : valid
  • X : valid
  • L : valid
  • C : valid
  • D : valid
  • M : valid
  • II : valid
  • VV : invalid
  • XX : valid
  • LL : invalid
  • CC : valid
  • DD : invalid
  • MM : valid
  • III : valid
  • VII : valid
  • IVI : invalid
  • IIV : invalid
  • CCI : valid
  • CCV : valid
  • CCX : valid
  • CCL : valid
  • CCC : valid
  • CCD : invalid
  • CCM : invalid
  • IIII : invalid
  • MLDI : invalid
  • MXXC : invalid
  • DCIIX : invalid
  • MCXXXX : invalid
  • MCCCCXVI : invalid
  • MMLXCVII : invalid
  • MMMCMXCIX : valid
  • MMMDCCCLXXXVIII : valid
  • ```
  • ## Scoring
  • This is a [code golf challenge]. Your score is the number of bytes in your code. Lowest score for each language wins.
  • I'm interested to see which approach gives the best score in each language. 50 substrings are a lot to check for, but are the repetitive patterns in them sufficient to make the approach competitive?
  • > Explanations are optional, but I'm more likely to upvote answers that have one.
  • [code golf challenge]: https://codegolf.codidact.com/categories/49/tags/4274 "The code-golf tag"
#9: Post edited by user avatar trichoplax‭ · 2024-03-06T01:24:06Z (9 months ago)
Specify V, L, and D appear only once
  • Given a string of Roman numerals, decide whether it forms a valid Roman number.
  • ## Methods
  • - You could do this by constructing all of the valid Roman numbers, and seeing if the input string of Roman numerals is one of them.
  • - Alternatively, you could use the following trick that does not involve any addition or subtraction, only checking for substrings:
  • **A string of Roman numerals is a valid Roman number if and only if it contains none of the following 50 strings as a substring:**
  • ```text
  • CCCC
  • CCD
  • CCM
  • CDC
  • CMC
  • CMD
  • CMM
  • DCD
  • DCM
  • DD
  • DM
  • IC
  • ID
  • IIII
  • IIV
  • IIX
  • IL
  • IM
  • IVI
  • IXC
  • IXI
  • IXL
  • IXV
  • IXX
  • LC
  • LD
  • LL
  • LM
  • LXC
  • LXL
  • MMMM
  • VC
  • VD
  • VIV
  • VIX
  • VL
  • VM
  • VV
  • VX
  • XCC
  • XCD
  • XCL
  • XCM
  • XCX
  • XD
  • XLX
  • XM
  • XXC
  • XXL
  • XXXX
  • ```
  • This works for any length string, if a valid Roman number is defined as follows:
  • ## Valid Roman numbers
  • - No numeral appears more than 3 times consecutively.
  • - A Roman number is constructed by concatenating the strings representing its ***thousands***, ***hundreds***, ***tens***, and ***units*** components:
  • - ***Units***
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 1 | "I"
  • 2 | "II"
  • 3 | "III"
  • 4 | "IV"
  • 5 | "V"
  • 6 | "VI"
  • 7 | "VII"
  • 8 | "VIII"
  • 9 | "IX"
  • - ***Tens***
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 10 | "X"
  • 20 | "XX"
  • 30 | "XXX"
  • 40 | "XL"
  • 50 | "L"
  • 60 | "LX"
  • 70 | "LXX"
  • 80 | "LXXX"
  • 90 | "XC"
  • - ***Hundreds***
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 100 | "C"
  • 200 | "CC"
  • 300 | "CCC"
  • 400 | "CD"
  • 500 | "D"
  • 600 | "DC"
  • 700 | "DCC"
  • 800 | "DCCC"
  • 900 | "CM"
  • - ***Thousands***
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 1000 | "M"
  • 2000 | "MM"
  • 3000 | "MMM"
  • So for example, 2345 would be represented as the concatenation of `MM` (for 2000), `CCC` (for 300), `XL` (for 40), and `V` (for 5), or `MMCCCXLV`.
  • This defines a unique correct representation for each number from 1 to 3999 (4000 and above are not representable without breaking the rule that a numeral cannot appear more than 3 times consecutively). The 50 substrings method described above will identify each of these 3999 strings as valid, and all other strings of Roman numerals as invalid.
  • ## Input
  • - A string containing only Roman numerals (`I`, `V`, `X`, `L`, `C`, `D`, `M`).
  • - The string will have length at least 1.
  • - The string will have length at most 15 (this is the length of the longest valid Roman numerals string).
  • ## Output
  • - One of 2 distinct values indicating whether the input is one of the 3999 valid Roman numeral strings.
  • - This may be calculated however you choose - you do not need to use either of the methods mentioned above.
  • - The results must be consistent with both methods mentioned above.
  • ## Examples
  • ### A valid Roman number
  • The input `MCMXCVI` is the unique correct representation of 1996. It contains none of the 50 substrings.
  • ### A string that is not a valid Roman number
  • Although the input `MMXDIII` might be suspected of representing 2493, it is not the unique correct representation of this number (which is `MMCDXCIII`). Note that it contains the substring `XD`, identifying it as invalid.
  • ## Test cases
  • Test cases are in the format `INPUT : output`.
  • ```text
  • I : valid
  • V : valid
  • X : valid
  • L : valid
  • C : valid
  • D : valid
  • M : valid
  • II : valid
  • VV : invalid
  • XX : valid
  • LL : invalid
  • CC : valid
  • DD : invalid
  • MM : valid
  • III : valid
  • VII : valid
  • IVI : invalid
  • IIV : invalid
  • CCI : valid
  • CCV : valid
  • CCX : valid
  • CCL : valid
  • CCC : valid
  • CCD : invalid
  • CCM : invalid
  • IIII : invalid
  • MLDI : invalid
  • MXXC : invalid
  • DCIIX : invalid
  • MCXXXX : invalid
  • MCCCCXVI : invalid
  • MMLXCVII : invalid
  • MMMCMXCIX : valid
  • MMMDCCCLXXXVIII : valid
  • ```
  • ## Scoring
  • This is a [code golf challenge]. Your score is the number of bytes in your code. Lowest score for each language wins.
  • I'm interested to see which approach gives the best score in each language. 50 substrings are a lot to check for, but are the repetitive patterns in them sufficient to make the approach competitive?
  • > Explanations are optional, but I'm more likely to upvote answers that have one.
  • [code golf challenge]: https://codegolf.codidact.com/categories/49/tags/4274 "The code-golf tag"
  • Given a string of Roman numerals, decide whether it forms a valid Roman number.
  • ## Methods
  • - You could do this by constructing all of the valid Roman numbers, and seeing if the input string of Roman numerals is one of them.
  • - Alternatively, you could use the following trick that does not involve any addition or subtraction, only checking for substrings:
  • **A string of Roman numerals is a valid Roman number if and only if it contains none of the following 50 strings as a substring:**
  • ```text
  • CCCC
  • CCD
  • CCM
  • CDC
  • CMC
  • CMD
  • CMM
  • DCD
  • DCM
  • DD
  • DM
  • IC
  • ID
  • IIII
  • IIV
  • IIX
  • IL
  • IM
  • IVI
  • IXC
  • IXI
  • IXL
  • IXV
  • IXX
  • LC
  • LD
  • LL
  • LM
  • LXC
  • LXL
  • MMMM
  • VC
  • VD
  • VIV
  • VIX
  • VL
  • VM
  • VV
  • VX
  • XCC
  • XCD
  • XCL
  • XCM
  • XCX
  • XD
  • XLX
  • XM
  • XXC
  • XXL
  • XXXX
  • ```
  • This works for any length string, if a valid Roman number is defined as follows:
  • ## Valid Roman numbers
  • - No numeral appears more than 3 times consecutively.
  • - Each of `V` (5), `L` (50), and `D` (500) appear only once.
  • - A Roman number is constructed by concatenating the strings representing its ***thousands***, ***hundreds***, ***tens***, and ***units*** components:
  • - ***Units***
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 1 | "I"
  • 2 | "II"
  • 3 | "III"
  • 4 | "IV"
  • 5 | "V"
  • 6 | "VI"
  • 7 | "VII"
  • 8 | "VIII"
  • 9 | "IX"
  • - ***Tens***
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 10 | "X"
  • 20 | "XX"
  • 30 | "XXX"
  • 40 | "XL"
  • 50 | "L"
  • 60 | "LX"
  • 70 | "LXX"
  • 80 | "LXXX"
  • 90 | "XC"
  • - ***Hundreds***
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 100 | "C"
  • 200 | "CC"
  • 300 | "CCC"
  • 400 | "CD"
  • 500 | "D"
  • 600 | "DC"
  • 700 | "DCC"
  • 800 | "DCCC"
  • 900 | "CM"
  • - ***Thousands***
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 1000 | "M"
  • 2000 | "MM"
  • 3000 | "MMM"
  • So for example, 2345 would be represented as the concatenation of `MM` (for 2000), `CCC` (for 300), `XL` (for 40), and `V` (for 5), or `MMCCCXLV`.
  • This defines a unique correct representation for each number from 1 to 3999 (4000 and above are not representable without breaking the rule that a numeral cannot appear more than 3 times consecutively). The 50 substrings method described above will identify each of these 3999 strings as valid, and all other strings of Roman numerals as invalid.
  • ## Input
  • - A string containing only Roman numerals (`I`, `V`, `X`, `L`, `C`, `D`, `M`).
  • - The string will have length at least 1.
  • - The string will have length at most 15 (this is the length of the longest valid Roman numerals string).
  • ## Output
  • - One of 2 distinct values indicating whether the input is one of the 3999 valid Roman numeral strings.
  • - This may be calculated however you choose - you do not need to use either of the methods mentioned above.
  • - The results must be consistent with both methods mentioned above.
  • ## Examples
  • ### A valid Roman number
  • The input `MCMXCVI` is the unique correct representation of 1996. It contains none of the 50 substrings.
  • ### A string that is not a valid Roman number
  • Although the input `MMXDIII` might be suspected of representing 2493, it is not the unique correct representation of this number (which is `MMCDXCIII`). Note that it contains the substring `XD`, identifying it as invalid.
  • ## Test cases
  • Test cases are in the format `INPUT : output`.
  • ```text
  • I : valid
  • V : valid
  • X : valid
  • L : valid
  • C : valid
  • D : valid
  • M : valid
  • II : valid
  • VV : invalid
  • XX : valid
  • LL : invalid
  • CC : valid
  • DD : invalid
  • MM : valid
  • III : valid
  • VII : valid
  • IVI : invalid
  • IIV : invalid
  • CCI : valid
  • CCV : valid
  • CCX : valid
  • CCL : valid
  • CCC : valid
  • CCD : invalid
  • CCM : invalid
  • IIII : invalid
  • MLDI : invalid
  • MXXC : invalid
  • DCIIX : invalid
  • MCXXXX : invalid
  • MCCCCXVI : invalid
  • MMLXCVII : invalid
  • MMMCMXCIX : valid
  • MMMDCCCLXXXVIII : valid
  • ```
  • ## Scoring
  • This is a [code golf challenge]. Your score is the number of bytes in your code. Lowest score for each language wins.
  • I'm interested to see which approach gives the best score in each language. 50 substrings are a lot to check for, but are the repetitive patterns in them sufficient to make the approach competitive?
  • > Explanations are optional, but I'm more likely to upvote answers that have one.
  • [code golf challenge]: https://codegolf.codidact.com/categories/49/tags/4274 "The code-golf tag"
#8: Post edited by user avatar trichoplax‭ · 2024-03-06T01:18:48Z (9 months ago)
Make the 2 methods into bullet points
  • Given a string of Roman numerals, decide whether it forms a valid Roman number.
  • ## Methods
  • You could do this by constructing all of the valid Roman numbers, and seeing if the input string of Roman numerals is one of them.
  • Alternatively, you could use the following trick that does not involve any addition or subtraction, only checking for substrings:
  • **A string of Roman numerals is a valid Roman number if and only if it contains none of the following 50 strings as a substring:**
  • ```text
  • CCCC
  • CCD
  • CCM
  • CDC
  • CMC
  • CMD
  • CMM
  • DCD
  • DCM
  • DD
  • DM
  • IC
  • ID
  • IIII
  • IIV
  • IIX
  • IL
  • IM
  • IVI
  • IXC
  • IXI
  • IXL
  • IXV
  • IXX
  • LC
  • LD
  • LL
  • LM
  • LXC
  • LXL
  • MMMM
  • VC
  • VD
  • VIV
  • VIX
  • VL
  • VM
  • VV
  • VX
  • XCC
  • XCD
  • XCL
  • XCM
  • XCX
  • XD
  • XLX
  • XM
  • XXC
  • XXL
  • XXXX
  • ```
  • This works for any length string, if a valid Roman number is defined as follows:
  • ## Valid Roman numbers
  • - No numeral appears more than 3 times consecutively.
  • - A Roman number is constructed by concatenating the strings representing its ***thousands***, ***hundreds***, ***tens***, and ***units*** components:
  • - ***Units***
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 1 | "I"
  • 2 | "II"
  • 3 | "III"
  • 4 | "IV"
  • 5 | "V"
  • 6 | "VI"
  • 7 | "VII"
  • 8 | "VIII"
  • 9 | "IX"
  • - ***Tens***
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 10 | "X"
  • 20 | "XX"
  • 30 | "XXX"
  • 40 | "XL"
  • 50 | "L"
  • 60 | "LX"
  • 70 | "LXX"
  • 80 | "LXXX"
  • 90 | "XC"
  • - ***Hundreds***
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 100 | "C"
  • 200 | "CC"
  • 300 | "CCC"
  • 400 | "CD"
  • 500 | "D"
  • 600 | "DC"
  • 700 | "DCC"
  • 800 | "DCCC"
  • 900 | "CM"
  • - ***Thousands***
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 1000 | "M"
  • 2000 | "MM"
  • 3000 | "MMM"
  • So for example, 2345 would be represented as the concatenation of `MM` (for 2000), `CCC` (for 300), `XL` (for 40), and `V` (for 5), or `MMCCCXLV`.
  • This defines a unique correct representation for each number from 1 to 3999 (4000 and above are not representable without breaking the rule that a numeral cannot appear more than 3 times consecutively). The 50 substrings method described above will identify each of these 3999 strings as valid, and all other strings of Roman numerals as invalid.
  • ## Input
  • - A string containing only Roman numerals (`I`, `V`, `X`, `L`, `C`, `D`, `M`).
  • - The string will have length at least 1.
  • - The string will have length at most 15 (this is the length of the longest valid Roman numerals string).
  • ## Output
  • - One of 2 distinct values indicating whether the input is one of the 3999 valid Roman numeral strings.
  • - This may be calculated however you choose - you do not need to use either of the methods mentioned above.
  • - The results must be consistent with both methods mentioned above.
  • ## Examples
  • ### A valid Roman number
  • The input `MCMXCVI` is the unique correct representation of 1996. It contains none of the 50 substrings.
  • ### A string that is not a valid Roman number
  • Although the input `MMXDIII` might be suspected of representing 2493, it is not the unique correct representation of this number (which is `MMCDXCIII`). Note that it contains the substring `XD`, identifying it as invalid.
  • ## Test cases
  • Test cases are in the format `INPUT : output`.
  • ```text
  • I : valid
  • V : valid
  • X : valid
  • L : valid
  • C : valid
  • D : valid
  • M : valid
  • II : valid
  • VV : invalid
  • XX : valid
  • LL : invalid
  • CC : valid
  • DD : invalid
  • MM : valid
  • III : valid
  • VII : valid
  • IVI : invalid
  • IIV : invalid
  • CCI : valid
  • CCV : valid
  • CCX : valid
  • CCL : valid
  • CCC : valid
  • CCD : invalid
  • CCM : invalid
  • IIII : invalid
  • MLDI : invalid
  • MXXC : invalid
  • DCIIX : invalid
  • MCXXXX : invalid
  • MCCCCXVI : invalid
  • MMLXCVII : invalid
  • MMMCMXCIX : valid
  • MMMDCCCLXXXVIII : valid
  • ```
  • ## Scoring
  • This is a [code golf challenge]. Your score is the number of bytes in your code. Lowest score for each language wins.
  • I'm interested to see which approach gives the best score in each language. 50 substrings are a lot to check for, but are the repetitive patterns in them sufficient to make the approach competitive?
  • > Explanations are optional, but I'm more likely to upvote answers that have one.
  • [code golf challenge]: https://codegolf.codidact.com/categories/49/tags/4274 "The code-golf tag"
  • Given a string of Roman numerals, decide whether it forms a valid Roman number.
  • ## Methods
  • - You could do this by constructing all of the valid Roman numbers, and seeing if the input string of Roman numerals is one of them.
  • - Alternatively, you could use the following trick that does not involve any addition or subtraction, only checking for substrings:
  • **A string of Roman numerals is a valid Roman number if and only if it contains none of the following 50 strings as a substring:**
  • ```text
  • CCCC
  • CCD
  • CCM
  • CDC
  • CMC
  • CMD
  • CMM
  • DCD
  • DCM
  • DD
  • DM
  • IC
  • ID
  • IIII
  • IIV
  • IIX
  • IL
  • IM
  • IVI
  • IXC
  • IXI
  • IXL
  • IXV
  • IXX
  • LC
  • LD
  • LL
  • LM
  • LXC
  • LXL
  • MMMM
  • VC
  • VD
  • VIV
  • VIX
  • VL
  • VM
  • VV
  • VX
  • XCC
  • XCD
  • XCL
  • XCM
  • XCX
  • XD
  • XLX
  • XM
  • XXC
  • XXL
  • XXXX
  • ```
  • This works for any length string, if a valid Roman number is defined as follows:
  • ## Valid Roman numbers
  • - No numeral appears more than 3 times consecutively.
  • - A Roman number is constructed by concatenating the strings representing its ***thousands***, ***hundreds***, ***tens***, and ***units*** components:
  • - ***Units***
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 1 | "I"
  • 2 | "II"
  • 3 | "III"
  • 4 | "IV"
  • 5 | "V"
  • 6 | "VI"
  • 7 | "VII"
  • 8 | "VIII"
  • 9 | "IX"
  • - ***Tens***
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 10 | "X"
  • 20 | "XX"
  • 30 | "XXX"
  • 40 | "XL"
  • 50 | "L"
  • 60 | "LX"
  • 70 | "LXX"
  • 80 | "LXXX"
  • 90 | "XC"
  • - ***Hundreds***
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 100 | "C"
  • 200 | "CC"
  • 300 | "CCC"
  • 400 | "CD"
  • 500 | "D"
  • 600 | "DC"
  • 700 | "DCC"
  • 800 | "DCCC"
  • 900 | "CM"
  • - ***Thousands***
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 1000 | "M"
  • 2000 | "MM"
  • 3000 | "MMM"
  • So for example, 2345 would be represented as the concatenation of `MM` (for 2000), `CCC` (for 300), `XL` (for 40), and `V` (for 5), or `MMCCCXLV`.
  • This defines a unique correct representation for each number from 1 to 3999 (4000 and above are not representable without breaking the rule that a numeral cannot appear more than 3 times consecutively). The 50 substrings method described above will identify each of these 3999 strings as valid, and all other strings of Roman numerals as invalid.
  • ## Input
  • - A string containing only Roman numerals (`I`, `V`, `X`, `L`, `C`, `D`, `M`).
  • - The string will have length at least 1.
  • - The string will have length at most 15 (this is the length of the longest valid Roman numerals string).
  • ## Output
  • - One of 2 distinct values indicating whether the input is one of the 3999 valid Roman numeral strings.
  • - This may be calculated however you choose - you do not need to use either of the methods mentioned above.
  • - The results must be consistent with both methods mentioned above.
  • ## Examples
  • ### A valid Roman number
  • The input `MCMXCVI` is the unique correct representation of 1996. It contains none of the 50 substrings.
  • ### A string that is not a valid Roman number
  • Although the input `MMXDIII` might be suspected of representing 2493, it is not the unique correct representation of this number (which is `MMCDXCIII`). Note that it contains the substring `XD`, identifying it as invalid.
  • ## Test cases
  • Test cases are in the format `INPUT : output`.
  • ```text
  • I : valid
  • V : valid
  • X : valid
  • L : valid
  • C : valid
  • D : valid
  • M : valid
  • II : valid
  • VV : invalid
  • XX : valid
  • LL : invalid
  • CC : valid
  • DD : invalid
  • MM : valid
  • III : valid
  • VII : valid
  • IVI : invalid
  • IIV : invalid
  • CCI : valid
  • CCV : valid
  • CCX : valid
  • CCL : valid
  • CCC : valid
  • CCD : invalid
  • CCM : invalid
  • IIII : invalid
  • MLDI : invalid
  • MXXC : invalid
  • DCIIX : invalid
  • MCXXXX : invalid
  • MCCCCXVI : invalid
  • MMLXCVII : invalid
  • MMMCMXCIX : valid
  • MMMDCCCLXXXVIII : valid
  • ```
  • ## Scoring
  • This is a [code golf challenge]. Your score is the number of bytes in your code. Lowest score for each language wins.
  • I'm interested to see which approach gives the best score in each language. 50 substrings are a lot to check for, but are the repetitive patterns in them sufficient to make the approach competitive?
  • > Explanations are optional, but I'm more likely to upvote answers that have one.
  • [code golf challenge]: https://codegolf.codidact.com/categories/49/tags/4274 "The code-golf tag"
#7: Post edited by user avatar trichoplax‭ · 2024-03-06T01:15:26Z (9 months ago)
Punctuate long sentence
  • Given a string of Roman numerals, decide whether it forms a valid Roman number.
  • ## Methods
  • You could do this by constructing all of the valid Roman numbers, and seeing if the input string of Roman numerals is one of them.
  • Alternatively, you could use the following trick that does not involve any addition or subtraction, only checking for substrings:
  • **A string of Roman numerals is a valid Roman number if and only if it contains none of the following 50 strings as a substring:**
  • ```text
  • CCCC
  • CCD
  • CCM
  • CDC
  • CMC
  • CMD
  • CMM
  • DCD
  • DCM
  • DD
  • DM
  • IC
  • ID
  • IIII
  • IIV
  • IIX
  • IL
  • IM
  • IVI
  • IXC
  • IXI
  • IXL
  • IXV
  • IXX
  • LC
  • LD
  • LL
  • LM
  • LXC
  • LXL
  • MMMM
  • VC
  • VD
  • VIV
  • VIX
  • VL
  • VM
  • VV
  • VX
  • XCC
  • XCD
  • XCL
  • XCM
  • XCX
  • XD
  • XLX
  • XM
  • XXC
  • XXL
  • XXXX
  • ```
  • This works for any length string, if a valid Roman number is defined as follows:
  • ## Valid Roman numbers
  • - No numeral appears more than 3 times consecutively.
  • - A Roman number is constructed by concatenating the strings representing its ***thousands***, ***hundreds***, ***tens***, and ***units*** components:
  • - ***Units***
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 1 | "I"
  • 2 | "II"
  • 3 | "III"
  • 4 | "IV"
  • 5 | "V"
  • 6 | "VI"
  • 7 | "VII"
  • 8 | "VIII"
  • 9 | "IX"
  • - ***Tens***
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 10 | "X"
  • 20 | "XX"
  • 30 | "XXX"
  • 40 | "XL"
  • 50 | "L"
  • 60 | "LX"
  • 70 | "LXX"
  • 80 | "LXXX"
  • 90 | "XC"
  • - ***Hundreds***
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 100 | "C"
  • 200 | "CC"
  • 300 | "CCC"
  • 400 | "CD"
  • 500 | "D"
  • 600 | "DC"
  • 700 | "DCC"
  • 800 | "DCCC"
  • 900 | "CM"
  • - ***Thousands***
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 1000 | "M"
  • 2000 | "MM"
  • 3000 | "MMM"
  • So for example, 2345 would be represented as the concatenation of `MM` (for 2000), `CCC` (for 300), `XL` (for 40), and `V` (for 5), or `MMCCCXLV`.
  • This defines a unique correct representation for each number from 1 to 3999 (4000 and above are not representable without breaking the rule that a numeral cannot appear more than 3 times consecutively). The 50 substrings method described above will identify each of these 3999 strings as valid, and all other strings of Roman numerals as invalid.
  • ## Input
  • - A string containing only Roman numerals (`I`, `V`, `X`, `L`, `C`, `D`, `M`).
  • - The string will have length at least 1.
  • - The string will have length at most 15 (this is the length of the longest valid Roman numerals string).
  • ## Output
  • - One of 2 distinct values indicating whether the input is one of the 3999 valid Roman numeral strings.
  • - This may be calculated however you choose - you do not need to use either of the methods mentioned above.
  • - The results must be consistent with both methods mentioned above.
  • ## Examples
  • ### A valid Roman number
  • The input `MCMXCVI` is the unique correct representation of 1996. It contains none of the 50 substrings.
  • ### A string that is not a valid Roman number
  • Although the input `MMXDIII` might be suspected of representing 2493, it is not the unique correct representation of this number (which is `MMCDXCIII`). Note that it contains the substring `XD`, identifying it as invalid.
  • ## Test cases
  • Test cases are in the format `INPUT : output`.
  • ```text
  • I : valid
  • V : valid
  • X : valid
  • L : valid
  • C : valid
  • D : valid
  • M : valid
  • II : valid
  • VV : invalid
  • XX : valid
  • LL : invalid
  • CC : valid
  • DD : invalid
  • MM : valid
  • III : valid
  • VII : valid
  • IVI : invalid
  • IIV : invalid
  • CCI : valid
  • CCV : valid
  • CCX : valid
  • CCL : valid
  • CCC : valid
  • CCD : invalid
  • CCM : invalid
  • IIII : invalid
  • MLDI : invalid
  • MXXC : invalid
  • DCIIX : invalid
  • MCXXXX : invalid
  • MCCCCXVI : invalid
  • MMLXCVII : invalid
  • MMMCMXCIX : valid
  • MMMDCCCLXXXVIII : valid
  • ```
  • ## Scoring
  • This is a [code golf challenge]. Your score is the number of bytes in your code. Lowest score for each language wins.
  • I'm interested to see which approach gives the best score in each language. 50 substrings is a lot to check for but are the repetitive patterns in them sufficient to make the substrings approach competitive?
  • > Explanations are optional, but I'm more likely to upvote answers that have one.
  • [code golf challenge]: https://codegolf.codidact.com/categories/49/tags/4274 "The code-golf tag"
  • Given a string of Roman numerals, decide whether it forms a valid Roman number.
  • ## Methods
  • You could do this by constructing all of the valid Roman numbers, and seeing if the input string of Roman numerals is one of them.
  • Alternatively, you could use the following trick that does not involve any addition or subtraction, only checking for substrings:
  • **A string of Roman numerals is a valid Roman number if and only if it contains none of the following 50 strings as a substring:**
  • ```text
  • CCCC
  • CCD
  • CCM
  • CDC
  • CMC
  • CMD
  • CMM
  • DCD
  • DCM
  • DD
  • DM
  • IC
  • ID
  • IIII
  • IIV
  • IIX
  • IL
  • IM
  • IVI
  • IXC
  • IXI
  • IXL
  • IXV
  • IXX
  • LC
  • LD
  • LL
  • LM
  • LXC
  • LXL
  • MMMM
  • VC
  • VD
  • VIV
  • VIX
  • VL
  • VM
  • VV
  • VX
  • XCC
  • XCD
  • XCL
  • XCM
  • XCX
  • XD
  • XLX
  • XM
  • XXC
  • XXL
  • XXXX
  • ```
  • This works for any length string, if a valid Roman number is defined as follows:
  • ## Valid Roman numbers
  • - No numeral appears more than 3 times consecutively.
  • - A Roman number is constructed by concatenating the strings representing its ***thousands***, ***hundreds***, ***tens***, and ***units*** components:
  • - ***Units***
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 1 | "I"
  • 2 | "II"
  • 3 | "III"
  • 4 | "IV"
  • 5 | "V"
  • 6 | "VI"
  • 7 | "VII"
  • 8 | "VIII"
  • 9 | "IX"
  • - ***Tens***
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 10 | "X"
  • 20 | "XX"
  • 30 | "XXX"
  • 40 | "XL"
  • 50 | "L"
  • 60 | "LX"
  • 70 | "LXX"
  • 80 | "LXXX"
  • 90 | "XC"
  • - ***Hundreds***
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 100 | "C"
  • 200 | "CC"
  • 300 | "CCC"
  • 400 | "CD"
  • 500 | "D"
  • 600 | "DC"
  • 700 | "DCC"
  • 800 | "DCCC"
  • 900 | "CM"
  • - ***Thousands***
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 1000 | "M"
  • 2000 | "MM"
  • 3000 | "MMM"
  • So for example, 2345 would be represented as the concatenation of `MM` (for 2000), `CCC` (for 300), `XL` (for 40), and `V` (for 5), or `MMCCCXLV`.
  • This defines a unique correct representation for each number from 1 to 3999 (4000 and above are not representable without breaking the rule that a numeral cannot appear more than 3 times consecutively). The 50 substrings method described above will identify each of these 3999 strings as valid, and all other strings of Roman numerals as invalid.
  • ## Input
  • - A string containing only Roman numerals (`I`, `V`, `X`, `L`, `C`, `D`, `M`).
  • - The string will have length at least 1.
  • - The string will have length at most 15 (this is the length of the longest valid Roman numerals string).
  • ## Output
  • - One of 2 distinct values indicating whether the input is one of the 3999 valid Roman numeral strings.
  • - This may be calculated however you choose - you do not need to use either of the methods mentioned above.
  • - The results must be consistent with both methods mentioned above.
  • ## Examples
  • ### A valid Roman number
  • The input `MCMXCVI` is the unique correct representation of 1996. It contains none of the 50 substrings.
  • ### A string that is not a valid Roman number
  • Although the input `MMXDIII` might be suspected of representing 2493, it is not the unique correct representation of this number (which is `MMCDXCIII`). Note that it contains the substring `XD`, identifying it as invalid.
  • ## Test cases
  • Test cases are in the format `INPUT : output`.
  • ```text
  • I : valid
  • V : valid
  • X : valid
  • L : valid
  • C : valid
  • D : valid
  • M : valid
  • II : valid
  • VV : invalid
  • XX : valid
  • LL : invalid
  • CC : valid
  • DD : invalid
  • MM : valid
  • III : valid
  • VII : valid
  • IVI : invalid
  • IIV : invalid
  • CCI : valid
  • CCV : valid
  • CCX : valid
  • CCL : valid
  • CCC : valid
  • CCD : invalid
  • CCM : invalid
  • IIII : invalid
  • MLDI : invalid
  • MXXC : invalid
  • DCIIX : invalid
  • MCXXXX : invalid
  • MCCCCXVI : invalid
  • MMLXCVII : invalid
  • MMMCMXCIX : valid
  • MMMDCCCLXXXVIII : valid
  • ```
  • ## Scoring
  • This is a [code golf challenge]. Your score is the number of bytes in your code. Lowest score for each language wins.
  • I'm interested to see which approach gives the best score in each language. 50 substrings are a lot to check for, but are the repetitive patterns in them sufficient to make the approach competitive?
  • > Explanations are optional, but I'm more likely to upvote answers that have one.
  • [code golf challenge]: https://codegolf.codidact.com/categories/49/tags/4274 "The code-golf tag"
#6: Post edited by user avatar trichoplax‭ · 2024-03-06T01:08:12Z (9 months ago)
Add clarification of construction example and maximum value
  • Given a string of Roman numerals, decide whether it forms a valid Roman number.
  • ## Methods
  • You could do this by constructing all of the valid Roman numbers, and seeing if the input string of Roman numerals is one of them.
  • Alternatively, you could use the following trick that does not involve any addition or subtraction, only checking for substrings:
  • **A string of Roman numerals is a valid Roman number if and only if it contains none of the following 50 strings as a substring:**
  • ```text
  • CCCC
  • CCD
  • CCM
  • CDC
  • CMC
  • CMD
  • CMM
  • DCD
  • DCM
  • DD
  • DM
  • IC
  • ID
  • IIII
  • IIV
  • IIX
  • IL
  • IM
  • IVI
  • IXC
  • IXI
  • IXL
  • IXV
  • IXX
  • LC
  • LD
  • LL
  • LM
  • LXC
  • LXL
  • MMMM
  • VC
  • VD
  • VIV
  • VIX
  • VL
  • VM
  • VV
  • VX
  • XCC
  • XCD
  • XCL
  • XCM
  • XCX
  • XD
  • XLX
  • XM
  • XXC
  • XXL
  • XXXX
  • ```
  • This works for any length string, if a valid Roman number is defined as follows:
  • ## Valid Roman numbers
  • - No numeral appears more than 3 times consecutively.
  • - A Roman number is constructed by concatenating the strings representing its ***thousands***, ***hundreds***, ***tens***, and ***units*** components:
  • - ***Units***
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 1 | "I"
  • 2 | "II"
  • 3 | "III"
  • 4 | "IV"
  • 5 | "V"
  • 6 | "VI"
  • 7 | "VII"
  • 8 | "VIII"
  • 9 | "IX"
  • - ***Tens***
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 10 | "X"
  • 20 | "XX"
  • 30 | "XXX"
  • 40 | "XL"
  • 50 | "L"
  • 60 | "LX"
  • 70 | "LXX"
  • 80 | "LXXX"
  • 90 | "XC"
  • - ***Hundreds***
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 100 | "C"
  • 200 | "CC"
  • 300 | "CCC"
  • 400 | "CD"
  • 500 | "D"
  • 600 | "DC"
  • 700 | "DCC"
  • 800 | "DCCC"
  • 900 | "CM"
  • - ***Thousands***
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 1000 | "M"
  • 2000 | "MM"
  • 3000 | "MMM"
  • So for example, 2345 would be represented as the concatenation of `MM`, `CCC`, `XL`, and `V`, or `MMCCCXLV`.
  • This defines a unique correct representation for each number from 1 to 3999. The 50 substrings method described above will identify each of these 3999 strings as valid, and all other strings of Roman numerals as invalid.
  • ## Input
  • - A string containing only Roman numerals (`I`, `V`, `X`, `L`, `C`, `D`, `M`).
  • - The string will have length at least 1.
  • - The string will have length at most 15 (this is the length of the longest valid Roman numerals string).
  • ## Output
  • - One of 2 distinct values indicating whether the input is one of the 3999 valid Roman numeral strings.
  • - This may be calculated however you choose - you do not need to use either of the methods mentioned above.
  • - The results must be consistent with both methods mentioned above.
  • ## Examples
  • ### A valid Roman number
  • The input `MCMXCVI` is the unique correct representation of 1996. It contains none of the 50 substrings.
  • ### A string that is not a valid Roman number
  • Although the input `MMXDIII` might be suspected of representing 2493, it is not the unique correct representation of this number (which is `MMCDXCIII`). Note that it contains the substring `XD`, identifying it as invalid.
  • ## Test cases
  • Test cases are in the format `INPUT : output`.
  • ```text
  • I : valid
  • V : valid
  • X : valid
  • L : valid
  • C : valid
  • D : valid
  • M : valid
  • II : valid
  • VV : invalid
  • XX : valid
  • LL : invalid
  • CC : valid
  • DD : invalid
  • MM : valid
  • III : valid
  • VII : valid
  • IVI : invalid
  • IIV : invalid
  • CCI : valid
  • CCV : valid
  • CCX : valid
  • CCL : valid
  • CCC : valid
  • CCD : invalid
  • CCM : invalid
  • IIII : invalid
  • MLDI : invalid
  • MXXC : invalid
  • DCIIX : invalid
  • MCXXXX : invalid
  • MCCCCXVI : invalid
  • MMLXCVII : invalid
  • MMMCMXCIX : valid
  • MMMDCCCLXXXVIII : valid
  • ```
  • ## Scoring
  • This is a [code golf challenge]. Your score is the number of bytes in your code. Lowest score for each language wins.
  • I'm interested to see which approach gives the best score in each language. 50 substrings is a lot to check for but are the repetitive patterns in them sufficient to make the substrings approach competitive?
  • > Explanations are optional, but I'm more likely to upvote answers that have one.
  • [code golf challenge]: https://codegolf.codidact.com/categories/49/tags/4274 "The code-golf tag"
  • Given a string of Roman numerals, decide whether it forms a valid Roman number.
  • ## Methods
  • You could do this by constructing all of the valid Roman numbers, and seeing if the input string of Roman numerals is one of them.
  • Alternatively, you could use the following trick that does not involve any addition or subtraction, only checking for substrings:
  • **A string of Roman numerals is a valid Roman number if and only if it contains none of the following 50 strings as a substring:**
  • ```text
  • CCCC
  • CCD
  • CCM
  • CDC
  • CMC
  • CMD
  • CMM
  • DCD
  • DCM
  • DD
  • DM
  • IC
  • ID
  • IIII
  • IIV
  • IIX
  • IL
  • IM
  • IVI
  • IXC
  • IXI
  • IXL
  • IXV
  • IXX
  • LC
  • LD
  • LL
  • LM
  • LXC
  • LXL
  • MMMM
  • VC
  • VD
  • VIV
  • VIX
  • VL
  • VM
  • VV
  • VX
  • XCC
  • XCD
  • XCL
  • XCM
  • XCX
  • XD
  • XLX
  • XM
  • XXC
  • XXL
  • XXXX
  • ```
  • This works for any length string, if a valid Roman number is defined as follows:
  • ## Valid Roman numbers
  • - No numeral appears more than 3 times consecutively.
  • - A Roman number is constructed by concatenating the strings representing its ***thousands***, ***hundreds***, ***tens***, and ***units*** components:
  • - ***Units***
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 1 | "I"
  • 2 | "II"
  • 3 | "III"
  • 4 | "IV"
  • 5 | "V"
  • 6 | "VI"
  • 7 | "VII"
  • 8 | "VIII"
  • 9 | "IX"
  • - ***Tens***
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 10 | "X"
  • 20 | "XX"
  • 30 | "XXX"
  • 40 | "XL"
  • 50 | "L"
  • 60 | "LX"
  • 70 | "LXX"
  • 80 | "LXXX"
  • 90 | "XC"
  • - ***Hundreds***
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 100 | "C"
  • 200 | "CC"
  • 300 | "CCC"
  • 400 | "CD"
  • 500 | "D"
  • 600 | "DC"
  • 700 | "DCC"
  • 800 | "DCCC"
  • 900 | "CM"
  • - ***Thousands***
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 1000 | "M"
  • 2000 | "MM"
  • 3000 | "MMM"
  • So for example, 2345 would be represented as the concatenation of `MM` (for 2000), `CCC` (for 300), `XL` (for 40), and `V` (for 5), or `MMCCCXLV`.
  • This defines a unique correct representation for each number from 1 to 3999 (4000 and above are not representable without breaking the rule that a numeral cannot appear more than 3 times consecutively). The 50 substrings method described above will identify each of these 3999 strings as valid, and all other strings of Roman numerals as invalid.
  • ## Input
  • - A string containing only Roman numerals (`I`, `V`, `X`, `L`, `C`, `D`, `M`).
  • - The string will have length at least 1.
  • - The string will have length at most 15 (this is the length of the longest valid Roman numerals string).
  • ## Output
  • - One of 2 distinct values indicating whether the input is one of the 3999 valid Roman numeral strings.
  • - This may be calculated however you choose - you do not need to use either of the methods mentioned above.
  • - The results must be consistent with both methods mentioned above.
  • ## Examples
  • ### A valid Roman number
  • The input `MCMXCVI` is the unique correct representation of 1996. It contains none of the 50 substrings.
  • ### A string that is not a valid Roman number
  • Although the input `MMXDIII` might be suspected of representing 2493, it is not the unique correct representation of this number (which is `MMCDXCIII`). Note that it contains the substring `XD`, identifying it as invalid.
  • ## Test cases
  • Test cases are in the format `INPUT : output`.
  • ```text
  • I : valid
  • V : valid
  • X : valid
  • L : valid
  • C : valid
  • D : valid
  • M : valid
  • II : valid
  • VV : invalid
  • XX : valid
  • LL : invalid
  • CC : valid
  • DD : invalid
  • MM : valid
  • III : valid
  • VII : valid
  • IVI : invalid
  • IIV : invalid
  • CCI : valid
  • CCV : valid
  • CCX : valid
  • CCL : valid
  • CCC : valid
  • CCD : invalid
  • CCM : invalid
  • IIII : invalid
  • MLDI : invalid
  • MXXC : invalid
  • DCIIX : invalid
  • MCXXXX : invalid
  • MCCCCXVI : invalid
  • MMLXCVII : invalid
  • MMMCMXCIX : valid
  • MMMDCCCLXXXVIII : valid
  • ```
  • ## Scoring
  • This is a [code golf challenge]. Your score is the number of bytes in your code. Lowest score for each language wins.
  • I'm interested to see which approach gives the best score in each language. 50 substrings is a lot to check for but are the repetitive patterns in them sufficient to make the substrings approach competitive?
  • > Explanations are optional, but I'm more likely to upvote answers that have one.
  • [code golf challenge]: https://codegolf.codidact.com/categories/49/tags/4274 "The code-golf tag"
#5: Post edited by user avatar trichoplax‭ · 2024-03-06T01:02:01Z (9 months ago)
Make title more accurate
  • The 50 substrings that invalidate any string of Roman numerals
  • The 50 substrings that validate any string of Roman numerals
#4: Post edited by user avatar trichoplax‭ · 2024-03-06T01:01:03Z (9 months ago)
Express interest in which approach is competitive in each language
  • Given a string of Roman numerals, decide whether it forms a valid Roman number.
  • ## Methods
  • You could do this by constructing all of the valid Roman numbers, and seeing if the input string of Roman numerals is one of them.
  • Alternatively, you could use the following trick that does not involve any addition or subtraction, only checking for substrings:
  • **A string of Roman numerals is a valid Roman number if and only if it contains none of the following 50 strings as a substring:**
  • ```text
  • CCCC
  • CCD
  • CCM
  • CDC
  • CMC
  • CMD
  • CMM
  • DCD
  • DCM
  • DD
  • DM
  • IC
  • ID
  • IIII
  • IIV
  • IIX
  • IL
  • IM
  • IVI
  • IXC
  • IXI
  • IXL
  • IXV
  • IXX
  • LC
  • LD
  • LL
  • LM
  • LXC
  • LXL
  • MMMM
  • VC
  • VD
  • VIV
  • VIX
  • VL
  • VM
  • VV
  • VX
  • XCC
  • XCD
  • XCL
  • XCM
  • XCX
  • XD
  • XLX
  • XM
  • XXC
  • XXL
  • XXXX
  • ```
  • This works for any length string, if a valid Roman number is defined as follows:
  • ## Valid Roman numbers
  • - No numeral appears more than 3 times consecutively.
  • - A Roman number is constructed by concatenating the strings representing its ***thousands***, ***hundreds***, ***tens***, and ***units*** components:
  • - ***Units***
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 1 | "I"
  • 2 | "II"
  • 3 | "III"
  • 4 | "IV"
  • 5 | "V"
  • 6 | "VI"
  • 7 | "VII"
  • 8 | "VIII"
  • 9 | "IX"
  • - ***Tens***
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 10 | "X"
  • 20 | "XX"
  • 30 | "XXX"
  • 40 | "XL"
  • 50 | "L"
  • 60 | "LX"
  • 70 | "LXX"
  • 80 | "LXXX"
  • 90 | "XC"
  • - ***Hundreds***
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 100 | "C"
  • 200 | "CC"
  • 300 | "CCC"
  • 400 | "CD"
  • 500 | "D"
  • 600 | "DC"
  • 700 | "DCC"
  • 800 | "DCCC"
  • 900 | "CM"
  • - ***Thousands***
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 1000 | "M"
  • 2000 | "MM"
  • 3000 | "MMM"
  • So for example, 2345 would be represented as the concatenation of `MM`, `CCC`, `XL`, and `V`, or `MMCCCXLV`.
  • This defines a unique correct representation for each number from 1 to 3999. The 50 substrings method described above will identify each of these 3999 strings as valid, and all other strings of Roman numerals as invalid.
  • ## Input
  • - A string containing only Roman numerals (`I`, `V`, `X`, `L`, `C`, `D`, `M`).
  • - The string will have length at least 1.
  • - The string will have length at most 15 (this is the length of the longest valid Roman numerals string).
  • ## Output
  • - One of 2 distinct values indicating whether the input is one of the 3999 valid Roman numeral strings.
  • - This may be calculated however you choose - you do not need to use either of the methods mentioned above.
  • - The results must be consistent with both methods mentioned above.
  • ## Examples
  • ### A valid Roman number
  • The input `MCMXCVI` is the unique correct representation of 1996. It contains none of the 50 substrings.
  • ### A string that is not a valid Roman number
  • Although the input `MMXDIII` might be suspected of representing 2493, it is not the unique correct representation of this number (which is `MMCDXCIII`). Note that it contains the substring `XD`, identifying it as invalid.
  • ## Test cases
  • Test cases are in the format `INPUT : output`.
  • ```text
  • I : valid
  • V : valid
  • X : valid
  • L : valid
  • C : valid
  • D : valid
  • M : valid
  • II : valid
  • VV : invalid
  • XX : valid
  • LL : invalid
  • CC : valid
  • DD : invalid
  • MM : valid
  • III : valid
  • VII : valid
  • IVI : invalid
  • IIV : invalid
  • CCI : valid
  • CCV : valid
  • CCX : valid
  • CCL : valid
  • CCC : valid
  • CCD : invalid
  • CCM : invalid
  • IIII : invalid
  • MLDI : invalid
  • MXXC : invalid
  • DCIIX : invalid
  • MCXXXX : invalid
  • MCCCCXVI : invalid
  • MMLXCVII : invalid
  • MMMCMXCIX : valid
  • MMMDCCCLXXXVIII : valid
  • ```
  • ## Scoring
  • This is a [code golf challenge]. Your score is the number of bytes in your code. Lowest score for each language wins.
  • > Explanations are optional, but I'm more likely to upvote answers that have one.
  • [code golf challenge]: https://codegolf.codidact.com/categories/49/tags/4274 "The code-golf tag"
  • Given a string of Roman numerals, decide whether it forms a valid Roman number.
  • ## Methods
  • You could do this by constructing all of the valid Roman numbers, and seeing if the input string of Roman numerals is one of them.
  • Alternatively, you could use the following trick that does not involve any addition or subtraction, only checking for substrings:
  • **A string of Roman numerals is a valid Roman number if and only if it contains none of the following 50 strings as a substring:**
  • ```text
  • CCCC
  • CCD
  • CCM
  • CDC
  • CMC
  • CMD
  • CMM
  • DCD
  • DCM
  • DD
  • DM
  • IC
  • ID
  • IIII
  • IIV
  • IIX
  • IL
  • IM
  • IVI
  • IXC
  • IXI
  • IXL
  • IXV
  • IXX
  • LC
  • LD
  • LL
  • LM
  • LXC
  • LXL
  • MMMM
  • VC
  • VD
  • VIV
  • VIX
  • VL
  • VM
  • VV
  • VX
  • XCC
  • XCD
  • XCL
  • XCM
  • XCX
  • XD
  • XLX
  • XM
  • XXC
  • XXL
  • XXXX
  • ```
  • This works for any length string, if a valid Roman number is defined as follows:
  • ## Valid Roman numbers
  • - No numeral appears more than 3 times consecutively.
  • - A Roman number is constructed by concatenating the strings representing its ***thousands***, ***hundreds***, ***tens***, and ***units*** components:
  • - ***Units***
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 1 | "I"
  • 2 | "II"
  • 3 | "III"
  • 4 | "IV"
  • 5 | "V"
  • 6 | "VI"
  • 7 | "VII"
  • 8 | "VIII"
  • 9 | "IX"
  • - ***Tens***
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 10 | "X"
  • 20 | "XX"
  • 30 | "XXX"
  • 40 | "XL"
  • 50 | "L"
  • 60 | "LX"
  • 70 | "LXX"
  • 80 | "LXXX"
  • 90 | "XC"
  • - ***Hundreds***
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 100 | "C"
  • 200 | "CC"
  • 300 | "CCC"
  • 400 | "CD"
  • 500 | "D"
  • 600 | "DC"
  • 700 | "DCC"
  • 800 | "DCCC"
  • 900 | "CM"
  • - ***Thousands***
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 1000 | "M"
  • 2000 | "MM"
  • 3000 | "MMM"
  • So for example, 2345 would be represented as the concatenation of `MM`, `CCC`, `XL`, and `V`, or `MMCCCXLV`.
  • This defines a unique correct representation for each number from 1 to 3999. The 50 substrings method described above will identify each of these 3999 strings as valid, and all other strings of Roman numerals as invalid.
  • ## Input
  • - A string containing only Roman numerals (`I`, `V`, `X`, `L`, `C`, `D`, `M`).
  • - The string will have length at least 1.
  • - The string will have length at most 15 (this is the length of the longest valid Roman numerals string).
  • ## Output
  • - One of 2 distinct values indicating whether the input is one of the 3999 valid Roman numeral strings.
  • - This may be calculated however you choose - you do not need to use either of the methods mentioned above.
  • - The results must be consistent with both methods mentioned above.
  • ## Examples
  • ### A valid Roman number
  • The input `MCMXCVI` is the unique correct representation of 1996. It contains none of the 50 substrings.
  • ### A string that is not a valid Roman number
  • Although the input `MMXDIII` might be suspected of representing 2493, it is not the unique correct representation of this number (which is `MMCDXCIII`). Note that it contains the substring `XD`, identifying it as invalid.
  • ## Test cases
  • Test cases are in the format `INPUT : output`.
  • ```text
  • I : valid
  • V : valid
  • X : valid
  • L : valid
  • C : valid
  • D : valid
  • M : valid
  • II : valid
  • VV : invalid
  • XX : valid
  • LL : invalid
  • CC : valid
  • DD : invalid
  • MM : valid
  • III : valid
  • VII : valid
  • IVI : invalid
  • IIV : invalid
  • CCI : valid
  • CCV : valid
  • CCX : valid
  • CCL : valid
  • CCC : valid
  • CCD : invalid
  • CCM : invalid
  • IIII : invalid
  • MLDI : invalid
  • MXXC : invalid
  • DCIIX : invalid
  • MCXXXX : invalid
  • MCCCCXVI : invalid
  • MMLXCVII : invalid
  • MMMCMXCIX : valid
  • MMMDCCCLXXXVIII : valid
  • ```
  • ## Scoring
  • This is a [code golf challenge]. Your score is the number of bytes in your code. Lowest score for each language wins.
  • I'm interested to see which approach gives the best score in each language. 50 substrings is a lot to check for but are the repetitive patterns in them sufficient to make the substrings approach competitive?
  • > Explanations are optional, but I'm more likely to upvote answers that have one.
  • [code golf challenge]: https://codegolf.codidact.com/categories/49/tags/4274 "The code-golf tag"
#3: Post edited by user avatar trichoplax‭ · 2024-03-06T00:22:22Z (9 months ago)
Improve wording of output section
  • Given a string of Roman numerals, decide whether it forms a valid Roman number.
  • ## Methods
  • You could do this by constructing all of the valid Roman numbers, and seeing if the input string of Roman numerals is one of them.
  • Alternatively, you could use the following trick that does not involve any addition or subtraction, only checking for substrings:
  • **A string of Roman numerals is a valid Roman number if and only if it contains none of the following 50 strings as a substring:**
  • ```text
  • CCCC
  • CCD
  • CCM
  • CDC
  • CMC
  • CMD
  • CMM
  • DCD
  • DCM
  • DD
  • DM
  • IC
  • ID
  • IIII
  • IIV
  • IIX
  • IL
  • IM
  • IVI
  • IXC
  • IXI
  • IXL
  • IXV
  • IXX
  • LC
  • LD
  • LL
  • LM
  • LXC
  • LXL
  • MMMM
  • VC
  • VD
  • VIV
  • VIX
  • VL
  • VM
  • VV
  • VX
  • XCC
  • XCD
  • XCL
  • XCM
  • XCX
  • XD
  • XLX
  • XM
  • XXC
  • XXL
  • XXXX
  • ```
  • This works for any length string, if a valid Roman number is defined as follows:
  • ## Valid Roman numbers
  • - No numeral appears more than 3 times consecutively.
  • - A Roman number is constructed by concatenating the strings representing its ***thousands***, ***hundreds***, ***tens***, and ***units*** components:
  • - ***Units***
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 1 | "I"
  • 2 | "II"
  • 3 | "III"
  • 4 | "IV"
  • 5 | "V"
  • 6 | "VI"
  • 7 | "VII"
  • 8 | "VIII"
  • 9 | "IX"
  • - ***Tens***
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 10 | "X"
  • 20 | "XX"
  • 30 | "XXX"
  • 40 | "XL"
  • 50 | "L"
  • 60 | "LX"
  • 70 | "LXX"
  • 80 | "LXXX"
  • 90 | "XC"
  • - ***Hundreds***
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 100 | "C"
  • 200 | "CC"
  • 300 | "CCC"
  • 400 | "CD"
  • 500 | "D"
  • 600 | "DC"
  • 700 | "DCC"
  • 800 | "DCCC"
  • 900 | "CM"
  • - ***Thousands***
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 1000 | "M"
  • 2000 | "MM"
  • 3000 | "MMM"
  • So for example, 2345 would be represented as the concatenation of `MM`, `CCC`, `XL`, and `V`, or `MMCCCXLV`.
  • This defines a unique correct representation for each number from 1 to 3999. The 50 substrings method described above will identify each of these 3999 strings as valid, and all other strings of Roman numerals as invalid.
  • ## Input
  • - A string containing only Roman numerals (`I`, `V`, `X`, `L`, `C`, `D`, `M`).
  • - The string will have length at least 1.
  • - The string will have length at most 15 (this is the length of the longest valid Roman numerals string).
  • ## Output
  • - One of 2 distinct values indicating whether the input is one of the 3999 valid Roman numbers.
  • - This may be calculated however you choose - you do not need to use either of the methods mentioned above.
  • - The results must be consistent with both methods mentioned above.
  • ## Examples
  • ### A valid Roman number
  • The input `MCMXCVI` is the unique correct representation of 1996. It contains none of the 50 substrings.
  • ### A string that is not a valid Roman number
  • Although the input `MMXDIII` might be suspected of representing 2493, it is not the unique correct representation of this number (which is `MMCDXCIII`). Note that it contains the substring `XD`, identifying it as invalid.
  • ## Test cases
  • Test cases are in the format `INPUT : output`.
  • ```text
  • I : valid
  • V : valid
  • X : valid
  • L : valid
  • C : valid
  • D : valid
  • M : valid
  • II : valid
  • VV : invalid
  • XX : valid
  • LL : invalid
  • CC : valid
  • DD : invalid
  • MM : valid
  • III : valid
  • VII : valid
  • IVI : invalid
  • IIV : invalid
  • CCI : valid
  • CCV : valid
  • CCX : valid
  • CCL : valid
  • CCC : valid
  • CCD : invalid
  • CCM : invalid
  • IIII : invalid
  • MLDI : invalid
  • MXXC : invalid
  • DCIIX : invalid
  • MCXXXX : invalid
  • MCCCCXVI : invalid
  • MMLXCVII : invalid
  • MMMCMXCIX : valid
  • MMMDCCCLXXXVIII : valid
  • ```
  • ## Scoring
  • This is a [code golf challenge]. Your score is the number of bytes in your code. Lowest score for each language wins.
  • > Explanations are optional, but I'm more likely to upvote answers that have one.
  • [code golf challenge]: https://codegolf.codidact.com/categories/49/tags/4274 "The code-golf tag"
  • Given a string of Roman numerals, decide whether it forms a valid Roman number.
  • ## Methods
  • You could do this by constructing all of the valid Roman numbers, and seeing if the input string of Roman numerals is one of them.
  • Alternatively, you could use the following trick that does not involve any addition or subtraction, only checking for substrings:
  • **A string of Roman numerals is a valid Roman number if and only if it contains none of the following 50 strings as a substring:**
  • ```text
  • CCCC
  • CCD
  • CCM
  • CDC
  • CMC
  • CMD
  • CMM
  • DCD
  • DCM
  • DD
  • DM
  • IC
  • ID
  • IIII
  • IIV
  • IIX
  • IL
  • IM
  • IVI
  • IXC
  • IXI
  • IXL
  • IXV
  • IXX
  • LC
  • LD
  • LL
  • LM
  • LXC
  • LXL
  • MMMM
  • VC
  • VD
  • VIV
  • VIX
  • VL
  • VM
  • VV
  • VX
  • XCC
  • XCD
  • XCL
  • XCM
  • XCX
  • XD
  • XLX
  • XM
  • XXC
  • XXL
  • XXXX
  • ```
  • This works for any length string, if a valid Roman number is defined as follows:
  • ## Valid Roman numbers
  • - No numeral appears more than 3 times consecutively.
  • - A Roman number is constructed by concatenating the strings representing its ***thousands***, ***hundreds***, ***tens***, and ***units*** components:
  • - ***Units***
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 1 | "I"
  • 2 | "II"
  • 3 | "III"
  • 4 | "IV"
  • 5 | "V"
  • 6 | "VI"
  • 7 | "VII"
  • 8 | "VIII"
  • 9 | "IX"
  • - ***Tens***
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 10 | "X"
  • 20 | "XX"
  • 30 | "XXX"
  • 40 | "XL"
  • 50 | "L"
  • 60 | "LX"
  • 70 | "LXX"
  • 80 | "LXXX"
  • 90 | "XC"
  • - ***Hundreds***
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 100 | "C"
  • 200 | "CC"
  • 300 | "CCC"
  • 400 | "CD"
  • 500 | "D"
  • 600 | "DC"
  • 700 | "DCC"
  • 800 | "DCCC"
  • 900 | "CM"
  • - ***Thousands***
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 1000 | "M"
  • 2000 | "MM"
  • 3000 | "MMM"
  • So for example, 2345 would be represented as the concatenation of `MM`, `CCC`, `XL`, and `V`, or `MMCCCXLV`.
  • This defines a unique correct representation for each number from 1 to 3999. The 50 substrings method described above will identify each of these 3999 strings as valid, and all other strings of Roman numerals as invalid.
  • ## Input
  • - A string containing only Roman numerals (`I`, `V`, `X`, `L`, `C`, `D`, `M`).
  • - The string will have length at least 1.
  • - The string will have length at most 15 (this is the length of the longest valid Roman numerals string).
  • ## Output
  • - One of 2 distinct values indicating whether the input is one of the 3999 valid Roman numeral strings.
  • - This may be calculated however you choose - you do not need to use either of the methods mentioned above.
  • - The results must be consistent with both methods mentioned above.
  • ## Examples
  • ### A valid Roman number
  • The input `MCMXCVI` is the unique correct representation of 1996. It contains none of the 50 substrings.
  • ### A string that is not a valid Roman number
  • Although the input `MMXDIII` might be suspected of representing 2493, it is not the unique correct representation of this number (which is `MMCDXCIII`). Note that it contains the substring `XD`, identifying it as invalid.
  • ## Test cases
  • Test cases are in the format `INPUT : output`.
  • ```text
  • I : valid
  • V : valid
  • X : valid
  • L : valid
  • C : valid
  • D : valid
  • M : valid
  • II : valid
  • VV : invalid
  • XX : valid
  • LL : invalid
  • CC : valid
  • DD : invalid
  • MM : valid
  • III : valid
  • VII : valid
  • IVI : invalid
  • IIV : invalid
  • CCI : valid
  • CCV : valid
  • CCX : valid
  • CCL : valid
  • CCC : valid
  • CCD : invalid
  • CCM : invalid
  • IIII : invalid
  • MLDI : invalid
  • MXXC : invalid
  • DCIIX : invalid
  • MCXXXX : invalid
  • MCCCCXVI : invalid
  • MMLXCVII : invalid
  • MMMCMXCIX : valid
  • MMMDCCCLXXXVIII : valid
  • ```
  • ## Scoring
  • This is a [code golf challenge]. Your score is the number of bytes in your code. Lowest score for each language wins.
  • > Explanations are optional, but I'm more likely to upvote answers that have one.
  • [code golf challenge]: https://codegolf.codidact.com/categories/49/tags/4274 "The code-golf tag"
#2: Post edited by user avatar trichoplax‭ · 2024-03-06T00:17:28Z (9 months ago)
Mention reason for upper limit on input length
  • Given a string of Roman numerals, decide whether it forms a valid Roman number.
  • ## Methods
  • You could do this by constructing all of the valid Roman numbers, and seeing if the input string of Roman numerals is one of them.
  • Alternatively, you could use the following trick that does not involve any addition or subtraction, only checking for substrings:
  • **A string of Roman numerals is a valid Roman number if and only if it contains none of the following 50 strings as a substring:**
  • ```text
  • CCCC
  • CCD
  • CCM
  • CDC
  • CMC
  • CMD
  • CMM
  • DCD
  • DCM
  • DD
  • DM
  • IC
  • ID
  • IIII
  • IIV
  • IIX
  • IL
  • IM
  • IVI
  • IXC
  • IXI
  • IXL
  • IXV
  • IXX
  • LC
  • LD
  • LL
  • LM
  • LXC
  • LXL
  • MMMM
  • VC
  • VD
  • VIV
  • VIX
  • VL
  • VM
  • VV
  • VX
  • XCC
  • XCD
  • XCL
  • XCM
  • XCX
  • XD
  • XLX
  • XM
  • XXC
  • XXL
  • XXXX
  • ```
  • This works for any length string, if a valid Roman number is defined as follows:
  • ## Valid Roman numbers
  • - No numeral appears more than 3 times consecutively.
  • - A Roman number is constructed by concatenating the strings representing its ***thousands***, ***hundreds***, ***tens***, and ***units*** components:
  • - ***Units***
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 1 | "I"
  • 2 | "II"
  • 3 | "III"
  • 4 | "IV"
  • 5 | "V"
  • 6 | "VI"
  • 7 | "VII"
  • 8 | "VIII"
  • 9 | "IX"
  • - ***Tens***
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 10 | "X"
  • 20 | "XX"
  • 30 | "XXX"
  • 40 | "XL"
  • 50 | "L"
  • 60 | "LX"
  • 70 | "LXX"
  • 80 | "LXXX"
  • 90 | "XC"
  • - ***Hundreds***
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 100 | "C"
  • 200 | "CC"
  • 300 | "CCC"
  • 400 | "CD"
  • 500 | "D"
  • 600 | "DC"
  • 700 | "DCC"
  • 800 | "DCCC"
  • 900 | "CM"
  • - ***Thousands***
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 1000 | "M"
  • 2000 | "MM"
  • 3000 | "MMM"
  • So for example, 2345 would be represented as the concatenation of `MM`, `CCC`, `XL`, and `V`, or `MMCCCXLV`.
  • This defines a unique correct representation for each number from 1 to 3999. The 50 substrings method described above will identify each of these 3999 strings as valid, and all other strings of Roman numerals as invalid.
  • ## Input
  • - A string containing only Roman numerals (`I`, `V`, `X`, `L`, `C`, `D`, `M`).
  • - The string will have length at least 1.
  • - The string will have length at most 15.
  • ## Output
  • - One of 2 distinct values indicating whether the input is one of the 3999 valid Roman numbers.
  • - This may be calculated however you choose - you do not need to use either of the methods mentioned above.
  • - The results must be consistent with both methods mentioned above.
  • ## Examples
  • ### A valid Roman number
  • The input `MCMXCVI` is the unique correct representation of 1996. It contains none of the 50 substrings.
  • ### A string that is not a valid Roman number
  • Although the input `MMXDIII` might be suspected of representing 2493, it is not the unique correct representation of this number (which is `MMCDXCIII`). Note that it contains the substring `XD`, identifying it as invalid.
  • ## Test cases
  • Test cases are in the format `INPUT : output`.
  • ```text
  • I : valid
  • V : valid
  • X : valid
  • L : valid
  • C : valid
  • D : valid
  • M : valid
  • II : valid
  • VV : invalid
  • XX : valid
  • LL : invalid
  • CC : valid
  • DD : invalid
  • MM : valid
  • III : valid
  • VII : valid
  • IVI : invalid
  • IIV : invalid
  • CCI : valid
  • CCV : valid
  • CCX : valid
  • CCL : valid
  • CCC : valid
  • CCD : invalid
  • CCM : invalid
  • IIII : invalid
  • MLDI : invalid
  • MXXC : invalid
  • DCIIX : invalid
  • MCXXXX : invalid
  • MCCCCXVI : invalid
  • MMLXCVII : invalid
  • MMMCMXCIX : valid
  • MMMDCCCLXXXVIII : valid
  • ```
  • ## Scoring
  • This is a [code golf challenge]. Your score is the number of bytes in your code. Lowest score for each language wins.
  • > Explanations are optional, but I'm more likely to upvote answers that have one.
  • [code golf challenge]: https://codegolf.codidact.com/categories/49/tags/4274 "The code-golf tag"
  • Given a string of Roman numerals, decide whether it forms a valid Roman number.
  • ## Methods
  • You could do this by constructing all of the valid Roman numbers, and seeing if the input string of Roman numerals is one of them.
  • Alternatively, you could use the following trick that does not involve any addition or subtraction, only checking for substrings:
  • **A string of Roman numerals is a valid Roman number if and only if it contains none of the following 50 strings as a substring:**
  • ```text
  • CCCC
  • CCD
  • CCM
  • CDC
  • CMC
  • CMD
  • CMM
  • DCD
  • DCM
  • DD
  • DM
  • IC
  • ID
  • IIII
  • IIV
  • IIX
  • IL
  • IM
  • IVI
  • IXC
  • IXI
  • IXL
  • IXV
  • IXX
  • LC
  • LD
  • LL
  • LM
  • LXC
  • LXL
  • MMMM
  • VC
  • VD
  • VIV
  • VIX
  • VL
  • VM
  • VV
  • VX
  • XCC
  • XCD
  • XCL
  • XCM
  • XCX
  • XD
  • XLX
  • XM
  • XXC
  • XXL
  • XXXX
  • ```
  • This works for any length string, if a valid Roman number is defined as follows:
  • ## Valid Roman numbers
  • - No numeral appears more than 3 times consecutively.
  • - A Roman number is constructed by concatenating the strings representing its ***thousands***, ***hundreds***, ***tens***, and ***units*** components:
  • - ***Units***
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 1 | "I"
  • 2 | "II"
  • 3 | "III"
  • 4 | "IV"
  • 5 | "V"
  • 6 | "VI"
  • 7 | "VII"
  • 8 | "VIII"
  • 9 | "IX"
  • - ***Tens***
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 10 | "X"
  • 20 | "XX"
  • 30 | "XXX"
  • 40 | "XL"
  • 50 | "L"
  • 60 | "LX"
  • 70 | "LXX"
  • 80 | "LXXX"
  • 90 | "XC"
  • - ***Hundreds***
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 100 | "C"
  • 200 | "CC"
  • 300 | "CCC"
  • 400 | "CD"
  • 500 | "D"
  • 600 | "DC"
  • 700 | "DCC"
  • 800 | "DCCC"
  • 900 | "CM"
  • - ***Thousands***
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 1000 | "M"
  • 2000 | "MM"
  • 3000 | "MMM"
  • So for example, 2345 would be represented as the concatenation of `MM`, `CCC`, `XL`, and `V`, or `MMCCCXLV`.
  • This defines a unique correct representation for each number from 1 to 3999. The 50 substrings method described above will identify each of these 3999 strings as valid, and all other strings of Roman numerals as invalid.
  • ## Input
  • - A string containing only Roman numerals (`I`, `V`, `X`, `L`, `C`, `D`, `M`).
  • - The string will have length at least 1.
  • - The string will have length at most 15 (this is the length of the longest valid Roman numerals string).
  • ## Output
  • - One of 2 distinct values indicating whether the input is one of the 3999 valid Roman numbers.
  • - This may be calculated however you choose - you do not need to use either of the methods mentioned above.
  • - The results must be consistent with both methods mentioned above.
  • ## Examples
  • ### A valid Roman number
  • The input `MCMXCVI` is the unique correct representation of 1996. It contains none of the 50 substrings.
  • ### A string that is not a valid Roman number
  • Although the input `MMXDIII` might be suspected of representing 2493, it is not the unique correct representation of this number (which is `MMCDXCIII`). Note that it contains the substring `XD`, identifying it as invalid.
  • ## Test cases
  • Test cases are in the format `INPUT : output`.
  • ```text
  • I : valid
  • V : valid
  • X : valid
  • L : valid
  • C : valid
  • D : valid
  • M : valid
  • II : valid
  • VV : invalid
  • XX : valid
  • LL : invalid
  • CC : valid
  • DD : invalid
  • MM : valid
  • III : valid
  • VII : valid
  • IVI : invalid
  • IIV : invalid
  • CCI : valid
  • CCV : valid
  • CCX : valid
  • CCL : valid
  • CCC : valid
  • CCD : invalid
  • CCM : invalid
  • IIII : invalid
  • MLDI : invalid
  • MXXC : invalid
  • DCIIX : invalid
  • MCXXXX : invalid
  • MCCCCXVI : invalid
  • MMLXCVII : invalid
  • MMMCMXCIX : valid
  • MMMDCCCLXXXVIII : valid
  • ```
  • ## Scoring
  • This is a [code golf challenge]. Your score is the number of bytes in your code. Lowest score for each language wins.
  • > Explanations are optional, but I'm more likely to upvote answers that have one.
  • [code golf challenge]: https://codegolf.codidact.com/categories/49/tags/4274 "The code-golf tag"
#1: Post edited by user avatar trichoplax‭ · 2024-03-06T00:13:57Z (9 months ago)
Be more specific about substrings method
  • Given a string of Roman numerals, decide whether it forms a valid Roman number.
  • ## Methods
  • You could do this by constructing all of the valid Roman numbers, and seeing if the input string of Roman numerals is one of them.
  • Alternatively, you could use the following trick that does not involve any addition or subtraction, only checking for substrings:
  • **A string of Roman numerals is a valid Roman number if and only if it contains none of the following 50 strings as a substring:**
  • ```text
  • CCCC
  • CCD
  • CCM
  • CDC
  • CMC
  • CMD
  • CMM
  • DCD
  • DCM
  • DD
  • DM
  • IC
  • ID
  • IIII
  • IIV
  • IIX
  • IL
  • IM
  • IVI
  • IXC
  • IXI
  • IXL
  • IXV
  • IXX
  • LC
  • LD
  • LL
  • LM
  • LXC
  • LXL
  • MMMM
  • VC
  • VD
  • VIV
  • VIX
  • VL
  • VM
  • VV
  • VX
  • XCC
  • XCD
  • XCL
  • XCM
  • XCX
  • XD
  • XLX
  • XM
  • XXC
  • XXL
  • XXXX
  • ```
  • This works for any length string, if a valid Roman number is defined as follows:
  • ## Valid Roman numbers
  • - No numeral appears more than 3 times consecutively.
  • - A Roman number is constructed by concatenating the strings representing its ***thousands***, ***hundreds***, ***tens***, and ***units*** components:
  • - ***Units***
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 1 | "I"
  • 2 | "II"
  • 3 | "III"
  • 4 | "IV"
  • 5 | "V"
  • 6 | "VI"
  • 7 | "VII"
  • 8 | "VIII"
  • 9 | "IX"
  • - ***Tens***
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 10 | "X"
  • 20 | "XX"
  • 30 | "XXX"
  • 40 | "XL"
  • 50 | "L"
  • 60 | "LX"
  • 70 | "LXX"
  • 80 | "LXXX"
  • 90 | "XC"
  • - ***Hundreds***
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 100 | "C"
  • 200 | "CC"
  • 300 | "CCC"
  • 400 | "CD"
  • 500 | "D"
  • 600 | "DC"
  • 700 | "DCC"
  • 800 | "DCCC"
  • 900 | "CM"
  • - ***Thousands***
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 1000 | "M"
  • 2000 | "MM"
  • 3000 | "MMM"
  • So for example, 2345 would be represented as the concatenation of `MM`, `CCC`, `XL`, and `V`, or `MMCCCXLV`.
  • This defines a unique correct representation for each number from 1 to 3999. The substring method described above will identify each of these 3999 strings as valid, and all other strings of Roman numerals as invalid.
  • ## Input
  • - A string containing only Roman numerals (`I`, `V`, `X`, `L`, `C`, `D`, `M`).
  • - The string will have length at least 1.
  • - The string will have length at most 15.
  • ## Output
  • - One of 2 distinct values indicating whether the input is one of the 3999 valid Roman numbers.
  • - This may be calculated however you choose - you do not need to use either of the methods mentioned above.
  • - The results must be consistent with both methods mentioned above.
  • ## Examples
  • ### A valid Roman number
  • The input `MCMXCVI` is the unique correct representation of 1996. It contains none of the 50 substrings.
  • ### A string that is not a valid Roman number
  • Although the input `MMXDIII` might be suspected of representing 2493, it is not the unique correct representation of this number (which is `MMCDXCIII`). Note that it contains the substring `XD`, identifying it as invalid.
  • ## Test cases
  • Test cases are in the format `INPUT : output`.
  • ```text
  • I : valid
  • V : valid
  • X : valid
  • L : valid
  • C : valid
  • D : valid
  • M : valid
  • II : valid
  • VV : invalid
  • XX : valid
  • LL : invalid
  • CC : valid
  • DD : invalid
  • MM : valid
  • III : valid
  • VII : valid
  • IVI : invalid
  • IIV : invalid
  • CCI : valid
  • CCV : valid
  • CCX : valid
  • CCL : valid
  • CCC : valid
  • CCD : invalid
  • CCM : invalid
  • IIII : invalid
  • MLDI : invalid
  • MXXC : invalid
  • DCIIX : invalid
  • MCXXXX : invalid
  • MCCCCXVI : invalid
  • MMLXCVII : invalid
  • MMMCMXCIX : valid
  • MMMDCCCLXXXVIII : valid
  • ```
  • ## Scoring
  • This is a [code golf challenge]. Your score is the number of bytes in your code. Lowest score for each language wins.
  • > Explanations are optional, but I'm more likely to upvote answers that have one.
  • [code golf challenge]: https://codegolf.codidact.com/categories/49/tags/4274 "The code-golf tag"
  • Given a string of Roman numerals, decide whether it forms a valid Roman number.
  • ## Methods
  • You could do this by constructing all of the valid Roman numbers, and seeing if the input string of Roman numerals is one of them.
  • Alternatively, you could use the following trick that does not involve any addition or subtraction, only checking for substrings:
  • **A string of Roman numerals is a valid Roman number if and only if it contains none of the following 50 strings as a substring:**
  • ```text
  • CCCC
  • CCD
  • CCM
  • CDC
  • CMC
  • CMD
  • CMM
  • DCD
  • DCM
  • DD
  • DM
  • IC
  • ID
  • IIII
  • IIV
  • IIX
  • IL
  • IM
  • IVI
  • IXC
  • IXI
  • IXL
  • IXV
  • IXX
  • LC
  • LD
  • LL
  • LM
  • LXC
  • LXL
  • MMMM
  • VC
  • VD
  • VIV
  • VIX
  • VL
  • VM
  • VV
  • VX
  • XCC
  • XCD
  • XCL
  • XCM
  • XCX
  • XD
  • XLX
  • XM
  • XXC
  • XXL
  • XXXX
  • ```
  • This works for any length string, if a valid Roman number is defined as follows:
  • ## Valid Roman numbers
  • - No numeral appears more than 3 times consecutively.
  • - A Roman number is constructed by concatenating the strings representing its ***thousands***, ***hundreds***, ***tens***, and ***units*** components:
  • - ***Units***
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 1 | "I"
  • 2 | "II"
  • 3 | "III"
  • 4 | "IV"
  • 5 | "V"
  • 6 | "VI"
  • 7 | "VII"
  • 8 | "VIII"
  • 9 | "IX"
  • - ***Tens***
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 10 | "X"
  • 20 | "XX"
  • 30 | "XXX"
  • 40 | "XL"
  • 50 | "L"
  • 60 | "LX"
  • 70 | "LXX"
  • 80 | "LXXX"
  • 90 | "XC"
  • - ***Hundreds***
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 100 | "C"
  • 200 | "CC"
  • 300 | "CCC"
  • 400 | "CD"
  • 500 | "D"
  • 600 | "DC"
  • 700 | "DCC"
  • 800 | "DCCC"
  • 900 | "CM"
  • - ***Thousands***
  • Decimal | Roman numerals
  • - | -
  • 0 | ""
  • 1000 | "M"
  • 2000 | "MM"
  • 3000 | "MMM"
  • So for example, 2345 would be represented as the concatenation of `MM`, `CCC`, `XL`, and `V`, or `MMCCCXLV`.
  • This defines a unique correct representation for each number from 1 to 3999. The 50 substrings method described above will identify each of these 3999 strings as valid, and all other strings of Roman numerals as invalid.
  • ## Input
  • - A string containing only Roman numerals (`I`, `V`, `X`, `L`, `C`, `D`, `M`).
  • - The string will have length at least 1.
  • - The string will have length at most 15.
  • ## Output
  • - One of 2 distinct values indicating whether the input is one of the 3999 valid Roman numbers.
  • - This may be calculated however you choose - you do not need to use either of the methods mentioned above.
  • - The results must be consistent with both methods mentioned above.
  • ## Examples
  • ### A valid Roman number
  • The input `MCMXCVI` is the unique correct representation of 1996. It contains none of the 50 substrings.
  • ### A string that is not a valid Roman number
  • Although the input `MMXDIII` might be suspected of representing 2493, it is not the unique correct representation of this number (which is `MMCDXCIII`). Note that it contains the substring `XD`, identifying it as invalid.
  • ## Test cases
  • Test cases are in the format `INPUT : output`.
  • ```text
  • I : valid
  • V : valid
  • X : valid
  • L : valid
  • C : valid
  • D : valid
  • M : valid
  • II : valid
  • VV : invalid
  • XX : valid
  • LL : invalid
  • CC : valid
  • DD : invalid
  • MM : valid
  • III : valid
  • VII : valid
  • IVI : invalid
  • IIV : invalid
  • CCI : valid
  • CCV : valid
  • CCX : valid
  • CCL : valid
  • CCC : valid
  • CCD : invalid
  • CCM : invalid
  • IIII : invalid
  • MLDI : invalid
  • MXXC : invalid
  • DCIIX : invalid
  • MCXXXX : invalid
  • MCCCCXVI : invalid
  • MMLXCVII : invalid
  • MMMCMXCIX : valid
  • MMMDCCCLXXXVIII : valid
  • ```
  • ## Scoring
  • This is a [code golf challenge]. Your score is the number of bytes in your code. Lowest score for each language wins.
  • > Explanations are optional, but I'm more likely to upvote answers that have one.
  • [code golf challenge]: https://codegolf.codidact.com/categories/49/tags/4274 "The code-golf tag"