yan chuanli 2 years ago
parent
commit
9d97cfee03
2 changed files with 46 additions and 0 deletions
  1. 4 0
      long_common_pre.py
  2. 42 0
      roman_to_int.py

+ 4 - 0
long_common_pre.py

@@ -0,0 +1,4 @@
+from typing import List
+
+def longestCommonPrefix(strs: List[str]):
+    lens = len(strs)

+ 42 - 0
roman_to_int.py

@@ -0,0 +1,42 @@
+import re
+import pytest
+def romanToInt(s:str):
+    special = {'IV': 4,
+                'IX': 9,
+                'XL': 40,
+                'XC': 90,
+                'CD': 400,
+                'CM': 900}
+    single = {'I': 1,
+              'V': 5,
+              'X': 10,
+              'L': 50,
+              'C': 100,
+              'D': 500,
+              'M': 1000}
+
+    r = 0
+    for k, v in special.items():
+        if k in s:
+            r += v
+            s = re.sub(k, '', s)
+            print(s)
+
+    for i in s:
+        r += single[i]
+
+    return r
+
+
+@pytest.mark.parametrize(
+    "s, expect",
+    [
+        ('IV', 4),
+        ('III', 3),
+        ('LVIII', 58),
+        ('MCMXCIV', 1994)
+    ]
+)
+def test_cases(s, expect):
+    assert romanToInt(s) == expect
+