teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

各メンバ比較例を追加

2017/08/16 05:44

投稿

8524ba23
8524ba23

スコア38352

answer CHANGED
@@ -15,13 +15,53 @@
15
15
  self.length = length
16
16
  def __lt__(self,other):
17
17
  return (self.start,self.end,self.length) < (other.start,other.end,other.length)
18
+
19
+ # 以下:各メンバ毎に比較する例
20
+ """
21
+ ret = (self.start < other.start)
22
+ if( ret != True): return ret
23
+
24
+ ret = (self.end < other.end)
25
+ if( ret != True): return ret
18
26
 
27
+ ret = (self.length < other.length)
28
+ return ret
29
+ """
30
+
19
31
  H = []
20
32
  heapq.heapify(H)
33
+ A = []
34
+ for s in reversed(range(2)):
35
+ for e in reversed(range(2)):
36
+ for l in reversed(range(2)):
37
+ b = Branch(s,e,l)
38
+ heapq.heappush(H, b)
39
+ A.append(b)
21
40
 
41
+ print('A')
42
+ A.sort()
22
- heapq.heappush(H, Branch(4,5,6))
43
+ for a in A: print(a.start,a.end,a.length)
23
- heapq.heappush(H, Branch(1,2,3))
24
44
 
25
- for h in H:
45
+ print('H')
26
- print(h.start,h.end,h.length)
46
+ for h in H: print(h.start,h.end,h.length)
47
+ ```
48
+ ```
49
+ A
50
+ 0 0 0
51
+ 0 0 1
52
+ 0 1 0
53
+ 0 1 1
54
+ 1 0 0
55
+ 1 0 1
56
+ 1 1 0
57
+ 1 1 1
58
+ H
59
+ 0 0 0
60
+ 0 0 1
61
+ 0 1 0
62
+ 1 0 0
63
+ 1 0 1
64
+ 1 1 0
65
+ 0 1 1
66
+ 1 1 1
27
67
  ```