回答編集履歴
4
コードの修正
test
CHANGED
@@ -2,9 +2,17 @@
|
|
2
2
|
|
3
3
|
1つの場合でも ( ) をつけて tuple にしないといけません。
|
4
4
|
|
5
|
+
というのが最初のコードでしたが、その問題を修正しました。
|
6
|
+
|
5
7
|
```Python
|
6
8
|
|
7
9
|
def range_sub(a, b):
|
10
|
+
|
11
|
+
if len(a) == 0 or len(b) == 0: return []
|
12
|
+
|
13
|
+
if type(a[0]) != tuple: a = [tuple(a)]
|
14
|
+
|
15
|
+
if type(b[0]) != tuple: b = [tuple(b)]
|
8
16
|
|
9
17
|
c = []
|
10
18
|
|
@@ -58,15 +66,17 @@
|
|
58
66
|
|
59
67
|
c.append((d[i], d[i+1]))
|
60
68
|
|
69
|
+
if len(c) == 1: c = [c[0][0], c[0][1]]
|
70
|
+
|
61
71
|
return c
|
62
72
|
|
63
73
|
|
64
74
|
|
65
75
|
|
66
76
|
|
67
|
-
a = [
|
77
|
+
a = ["2021/08/23 12:00:00", "2021/08/23 18:00:00"]
|
68
78
|
|
69
|
-
b = [
|
79
|
+
b = ["2021/08/23 13:00:00", "2021/08/23 14:00:00"]
|
70
80
|
|
71
81
|
c = range_sub(a, b)
|
72
82
|
|
@@ -80,11 +90,11 @@
|
|
80
90
|
|
81
91
|
|
82
92
|
|
83
|
-
a = [
|
93
|
+
a = ["2021/08/23 12:00:00", "2021/08/23 18:00:00"]
|
84
94
|
|
85
95
|
b = [("2021/08/23 13:00:00", "2021/08/23 14:00:00"), ("2021/08/23 17:00:00", "2021/08/23 21:00:00")]
|
86
96
|
|
87
|
-
c
|
97
|
+
c = range_sub(a, b)
|
88
98
|
|
89
99
|
print(a)
|
90
100
|
|
@@ -92,8 +102,24 @@
|
|
92
102
|
|
93
103
|
print(c)
|
94
104
|
|
105
|
+
print()
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
a = ["2021/08/23 12:00:00", "2021/08/23 18:00:00"]
|
110
|
+
|
111
|
+
b = ["2021/08/23 19:00:00", "2021/08/23 20:00:00"]
|
112
|
+
|
113
|
+
c = range_sub(a, b)
|
114
|
+
|
115
|
+
print(a)
|
116
|
+
|
117
|
+
print(b)
|
118
|
+
|
119
|
+
print(c)
|
120
|
+
|
121
|
+
|
122
|
+
|
95
123
|
```
|
96
124
|
|
97
|
-
同じ時刻があった場合どうなるか、まだ検証していません。
|
98
|
-
|
99
|
-
|
125
|
+
わからないことがあれば、コメントをください。
|
3
コードの改善
test
CHANGED
@@ -30,25 +30,25 @@
|
|
30
30
|
|
31
31
|
if t[1] == 2:
|
32
32
|
|
33
|
-
if not aa and not bb: d.append(
|
33
|
+
if not aa and not bb: d.append(t[0])
|
34
34
|
|
35
35
|
aa = True
|
36
36
|
|
37
37
|
elif t[1] == 3:
|
38
38
|
|
39
|
-
if aa and not bb: d.append(
|
39
|
+
if aa and not bb: d.append(t[0])
|
40
40
|
|
41
41
|
aa = False
|
42
42
|
|
43
43
|
elif t[1] == 1:
|
44
44
|
|
45
|
-
if not bb and aa: d.append(
|
45
|
+
if not bb and aa: d.append(t[0])
|
46
46
|
|
47
47
|
bb = True
|
48
48
|
|
49
49
|
else:
|
50
50
|
|
51
|
-
if bb and aa: d.append(
|
51
|
+
if bb and aa: d.append(t[0])
|
52
52
|
|
53
53
|
bb = False
|
54
54
|
|
@@ -56,7 +56,7 @@
|
|
56
56
|
|
57
57
|
for i in range(0, len(d), 2):
|
58
58
|
|
59
|
-
c.append((d[i]
|
59
|
+
c.append((d[i], d[i+1]))
|
60
60
|
|
61
61
|
return c
|
62
62
|
|
2
コードの修正
test
CHANGED
@@ -10,13 +10,13 @@
|
|
10
10
|
|
11
11
|
for t in a:
|
12
12
|
|
13
|
-
c.append([t[0],
|
13
|
+
c.append([t[0], 2])
|
14
14
|
|
15
|
-
c.append([t[1],
|
15
|
+
c.append([t[1], 3])
|
16
16
|
|
17
17
|
for t in b:
|
18
18
|
|
19
|
-
c.append([t[0],
|
19
|
+
c.append([t[0], 1])
|
20
20
|
|
21
21
|
c.append([t[1], 4])
|
22
22
|
|
@@ -28,19 +28,19 @@
|
|
28
28
|
|
29
29
|
for t in c:
|
30
30
|
|
31
|
-
if t[1] ==
|
31
|
+
if t[1] == 2:
|
32
32
|
|
33
33
|
if not aa and not bb: d.append([t[0], 1])
|
34
34
|
|
35
35
|
aa = True
|
36
36
|
|
37
|
-
elif t[1] ==
|
37
|
+
elif t[1] == 3:
|
38
38
|
|
39
39
|
if aa and not bb: d.append([t[0], 2])
|
40
40
|
|
41
41
|
aa = False
|
42
42
|
|
43
|
-
elif t[1] ==
|
43
|
+
elif t[1] == 1:
|
44
44
|
|
45
45
|
if not bb and aa: d.append([t[0], 2])
|
46
46
|
|
1
コードの改善
test
CHANGED
@@ -13,8 +13,6 @@
|
|
13
13
|
c.append([t[0], 1])
|
14
14
|
|
15
15
|
c.append([t[1], 2])
|
16
|
-
|
17
|
-
|
18
16
|
|
19
17
|
for t in b:
|
20
18
|
|
@@ -36,19 +34,19 @@
|
|
36
34
|
|
37
35
|
aa = True
|
38
36
|
|
39
|
-
if t[1] == 2:
|
37
|
+
elif t[1] == 2:
|
40
38
|
|
41
39
|
if aa and not bb: d.append([t[0], 2])
|
42
40
|
|
43
41
|
aa = False
|
44
42
|
|
45
|
-
if t[1] == 3:
|
43
|
+
elif t[1] == 3:
|
46
44
|
|
47
45
|
if not bb and aa: d.append([t[0], 2])
|
48
46
|
|
49
47
|
bb = True
|
50
48
|
|
51
|
-
|
49
|
+
else:
|
52
50
|
|
53
51
|
if bb and aa: d.append([t[0], 1])
|
54
52
|
|