add roman_numerals exercise
This commit is contained in:
parent
1146ef44e1
commit
26667a2559
@ -1,11 +1,11 @@
|
||||
# Fifth
|
||||
# Fifth Stack
|
||||
|
||||
A simple stack-based language called Fifth
|
||||
|
||||
## Usage
|
||||
|
||||
```shell
|
||||
python fifth.py
|
||||
python fifth_stack.py
|
||||
```
|
||||
|
||||
### Commands
|
||||
@ -48,7 +48,7 @@ EXIT
|
||||
|
||||
```bash
|
||||
pip install pytest
|
||||
pytest -v test_fifth.py
|
||||
pytest -v test_fifth_stack.py
|
||||
```
|
||||
|
||||
## Requirements
|
||||
@ -1,5 +1,5 @@
|
||||
import pytest
|
||||
from fifth import FifthStack
|
||||
from fifth_stack import FifthStack
|
||||
|
||||
|
||||
# Test the functions
|
||||
36
roman_numerals/roman_numerals.py
Normal file
36
roman_numerals/roman_numerals.py
Normal file
@ -0,0 +1,36 @@
|
||||
import readline
|
||||
|
||||
def convert_to_number(numerals: str) -> str:
|
||||
filtered = list(filter(lambda x: x in "IVXCM", list(numerals.upper())))
|
||||
if len(filtered) == 0:
|
||||
return "Invalid input"
|
||||
|
||||
values: list[int] = []
|
||||
for i in range(len(filtered)):
|
||||
match filtered[i]:
|
||||
case "I":
|
||||
values.append(1)
|
||||
case "V":
|
||||
values.append(5)
|
||||
case "X":
|
||||
values.append(10)
|
||||
case "C":
|
||||
values.append(100)
|
||||
case "M":
|
||||
values.append(1000)
|
||||
|
||||
if len(values) > 1:
|
||||
return f"{''.join(filtered)} = {' + '.join(str(v) for v in values)} = {sum(values)}"
|
||||
else:
|
||||
return f"{''.join(filtered)} = {values[0]}"
|
||||
|
||||
def main():
|
||||
while True:
|
||||
try:
|
||||
numerals = input("Please enter some Roman numerals: ")
|
||||
print(convert_to_number(numerals))
|
||||
except (EOFError, KeyboardInterrupt):
|
||||
break
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
38
roman_numerals/test_roman_numerals.py
Normal file
38
roman_numerals/test_roman_numerals.py
Normal file
@ -0,0 +1,38 @@
|
||||
import pytest
|
||||
from roman_numerals import convert_to_number
|
||||
|
||||
def test_valid_single_numerals():
|
||||
assert convert_to_number("I") == "I = 1"
|
||||
assert convert_to_number("V") == "V = 5"
|
||||
assert convert_to_number("X") == "X = 10"
|
||||
assert convert_to_number("C") == "C = 100"
|
||||
assert convert_to_number("M") == "M = 1000"
|
||||
|
||||
def test_valid_multiple_numerals():
|
||||
assert convert_to_number("IV") == "IV = 1 + 5 = 6"
|
||||
assert convert_to_number("XCM") == "XCM = 10 + 100 + 1000 = 1110"
|
||||
assert convert_to_number("MCMXCIV") == "MCMXCIV = 1000 + 100 + 1000 + 10 + 100 + 1 + 5 = 2216"
|
||||
|
||||
def test_lowercase_input():
|
||||
assert convert_to_number("ivxcm") == "IVXCM = 1 + 5 + 10 + 100 + 1000 = 1116"
|
||||
|
||||
def test_mixed_case_input():
|
||||
assert convert_to_number("iVxCm") == "IVXCM = 1 + 5 + 10 + 100 + 1000 = 1116"
|
||||
|
||||
def test_invalid_characters():
|
||||
assert convert_to_number("EFGH") == "Invalid input"
|
||||
assert convert_to_number("123") == "Invalid input"
|
||||
assert convert_to_number("!@#") == "Invalid input"
|
||||
|
||||
def test_mixed_valid_and_invalid_characters():
|
||||
assert convert_to_number("AIXB") == "IX = 1 + 10 = 11"
|
||||
assert convert_to_number("M1C2") == "MC = 1000 + 100 = 1100"
|
||||
|
||||
def test_empty_string():
|
||||
assert convert_to_number("") == "Invalid input"
|
||||
|
||||
def test_only_spaces():
|
||||
assert convert_to_number(" ") == "Invalid input"
|
||||
|
||||
def test_spaces_and_valid_numerals():
|
||||
assert convert_to_number(" I V ") == "IV = 1 + 5 = 6"
|
||||
Loading…
Reference in New Issue
Block a user