回答編集履歴

1

コード修正

2022/01/08 01:10

投稿

8524ba23
8524ba23

スコア38341

test CHANGED
@@ -28,33 +28,27 @@
28
28
 
29
29
 
30
30
 
31
- def conv_ym(s):
31
+ def conv(s):
32
32
 
33
- m = re.search(r'(\d+)-(\d+)', s)
33
+ # 年月を抽出
34
34
 
35
- if m:
35
+ rules = [(r'(\d+)-(\d+)', 0), (r'平成(\d+)年(\d+)月', 1988), (r'昭和(\d+)年(\d+)月', 1925)]
36
36
 
37
- return int(m.group(1)), int(m.group(2))
37
+ y, m = None, None
38
38
 
39
- m = re.search(r'平成(\d+)年(\d+)月', s)
39
+ for exp, offset in rules:
40
40
 
41
- if m:
41
+ ret = re.search(exp, s)
42
42
 
43
- return int(m.group(1))+1988, int(m.group(2))
43
+ if ret:
44
44
 
45
- m = re.search(r'昭和(\d+)年(\d+)月', s)
45
+ y = int(ret.group(1)) + offset
46
46
 
47
- if m:
47
+ m = int(ret.group(2))
48
48
 
49
- return int(m.group(1))+1925, int(m.group(2))
50
-
51
- return None,None
49
+ break
52
50
 
53
51
 
54
-
55
- def conv(s):
56
-
57
- y,m = conv_ym(s)
58
52
 
59
53
  s = ''
60
54
 
@@ -63,8 +57,6 @@
63
57
  s = f'{y}年{m}月'
64
58
 
65
59
  return s
66
-
67
-
68
60
 
69
61
 
70
62
 
@@ -82,4 +74,8 @@
82
74
 
83
75
  #3 昭和47年12月 1972年12月
84
76
 
77
+
78
+
79
+
80
+
85
81
  ```