回答編集履歴
2
コード追記
test
CHANGED
@@ -82,7 +82,11 @@
|
|
82
82
|
|
83
83
|
def num_direction(num: int, direction: str) -> Optional[int]:
|
84
84
|
|
85
|
-
if num < 0 or num >= grid_size ** 2: # 不正な
|
85
|
+
if num < 0 or num >= grid_size ** 2: # numが不正な値のとき
|
86
|
+
|
87
|
+
return None
|
88
|
+
|
89
|
+
elif direction not in direction_converter: # directionが不正な値のとき
|
86
90
|
|
87
91
|
return None
|
88
92
|
|
@@ -112,6 +116,8 @@
|
|
112
116
|
|
113
117
|
print(num_direction(16, "下")) # None
|
114
118
|
|
119
|
+
print(num_direction(16, "斜め")) # None
|
120
|
+
|
115
121
|
|
116
122
|
|
117
123
|
|
1
コード追記
test
CHANGED
@@ -5,6 +5,10 @@
|
|
5
5
|
```Python
|
6
6
|
|
7
7
|
"""
|
8
|
+
|
9
|
+
https://teratail.com/questions/247619
|
10
|
+
|
11
|
+
|
8
12
|
|
9
13
|
周囲が-1で囲まれた番兵付きの拡大版gridを用意する。
|
10
14
|
|
@@ -78,6 +82,10 @@
|
|
78
82
|
|
79
83
|
def num_direction(num: int, direction: str) -> Optional[int]:
|
80
84
|
|
85
|
+
if num < 0 or num >= grid_size ** 2: # 不正な引数
|
86
|
+
|
87
|
+
return None
|
88
|
+
|
81
89
|
next_index = grid.index(num) + direction_converter[direction]
|
82
90
|
|
83
91
|
if (next_num := grid[next_index]) != -1:
|
@@ -92,11 +100,17 @@
|
|
92
100
|
|
93
101
|
def main():
|
94
102
|
|
103
|
+
# 入力例
|
104
|
+
|
95
105
|
print(num_direction(0, "右")) # 1
|
96
106
|
|
97
107
|
print(num_direction(2, "右上")) # None
|
98
108
|
|
99
109
|
print(num_direction(7, "上")) # 3
|
110
|
+
|
111
|
+
print(num_direction(-1, "下")) # None
|
112
|
+
|
113
|
+
print(num_direction(16, "下")) # None
|
100
114
|
|
101
115
|
|
102
116
|
|