Files
VV-ISTIC-TP4/exercises/roman-numerals.md
2025-12-19 14:51:40 +01:00

62 lines
2.8 KiB
Markdown

# Roman numerals
Any natural number between 0 and 3999 can be represented in *roman numerals* using the following rules:
1. Only symbols *M*, *D*, *C*, *X*, *V*, *I* can be used. Their values are shown below:
| M | D | C | L | X | V | I |
|------|-----|-----|----|----|---|---|
| 1000 | 500 | 100 | 50 | 10 | 5 | 1 |
2. Symbols M, C, X, I can be repeated consecutively up to three times.
3. Symbols D, L and V can not be repeated.
4. When a symbol of lower value of appears to the right of a symbol of equal or higher value, all symbol values are added.
~~5. When a symbols of lower value appears to the left of a symbols of higher value, the lower value is subtracted from the higher value. Only symbols C, X, V can be subtracted. Each symbol can be subtracted only once. The subtracted symbol must be one fifth or one tenth of the larger.~~
/!\ Ennoncé erroné /!\ :
5. Quand un symbole plus petit est à gauche du symbole courant, alors la valeur du symbole est soustraite par le symbole de gauche. Tous les symboles peuvent se faire soustraire (pas uniquement C X V) par un autre symbole appartenant à [I,X,C] et devant être la division par 5 ou 10 du symbole courant. Le symbole de gauche ne peut pas être répété.
Donc seul : IV, IX, XL, XC, CD, CM sont autorisés.
Exemple : IIX -> pas ok -> symbole de gauche répété
6. Après une soustraction du cas .5, on ne peut plus avoir de symbole avec une valeur >= au symbole de gauche (le plus petit des deux) dans la suite du chiffre romain. Mais si une valeur == à la valeur max autorisée est soustraite, alors elle est autorisée (car avec la soustraction elle est donc inférieure à la valeur autorisée XCIX -> valeurs >= 10 interdites, IX = 9 -> 10>9 -> ok)
Exemple :
IXX -> pas ok : valeurs > I interdite, le 2nd X>I
XLIX -> ok : valeurs > X interdite, le 2nd X est soustrait (donc fait 9) il est donc autorisé
XLX -> pas ok : valeurs > X interdite, le 2nd X n'est pas soustrait, il est donc non autorisé
*Examples:*
- 1 = I
- 4 = IV
- 8 = VIII
- 9 = IX
- 14 = XIV
- 16 = XVI
- 19 = XIX
- 99 = XCIX
- 105 = CV
- 1001 = MI
- 2289 = MMCCLXXXIX
Implement the following methods in the `RomanNumeralUtils` class:
```java
class RomanNumeralUtils {
public static boolean isValidRomanNumeral(String value) { ... }
public static int parseRomanNumeral(String numeral) { ... }
public static String toRomanNumeral(int number) { ... }
}
```
Use [jqwik](https://jqwik.net/) to create property based tests verifying these three methods. Create the tests before implementing the methods. Document any bugs you found with the help of these tests during the process.
**NOTE:**
- Do not use any existing implementation, write your own code.
- Use the provided project template as a starting point.
- In the project you can launch the tests with `mvn test`.