diff --git a/fifth/README.md b/fifth_stack/README.md similarity index 90% rename from fifth/README.md rename to fifth_stack/README.md index d8a776c..6822118 100644 --- a/fifth/README.md +++ b/fifth_stack/README.md @@ -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 diff --git a/fifth/fifth.py b/fifth_stack/fifth_stack.py similarity index 100% rename from fifth/fifth.py rename to fifth_stack/fifth_stack.py diff --git a/fifth/requirements.txt b/fifth_stack/requirements.txt similarity index 100% rename from fifth/requirements.txt rename to fifth_stack/requirements.txt diff --git a/fifth/test_fifth.py b/fifth_stack/test_fifth_stack.py similarity index 99% rename from fifth/test_fifth.py rename to fifth_stack/test_fifth_stack.py index e2a15aa..5e6aadc 100644 --- a/fifth/test_fifth.py +++ b/fifth_stack/test_fifth_stack.py @@ -1,5 +1,5 @@ import pytest -from fifth import FifthStack +from fifth_stack import FifthStack # Test the functions diff --git a/roman_numerals/roman_numerals.py b/roman_numerals/roman_numerals.py new file mode 100644 index 0000000..21b33cd --- /dev/null +++ b/roman_numerals/roman_numerals.py @@ -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() diff --git a/roman_numerals/test_roman_numerals.py b/roman_numerals/test_roman_numerals.py new file mode 100644 index 0000000..cefc500 --- /dev/null +++ b/roman_numerals/test_roman_numerals.py @@ -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"