-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdiagram.py
187 lines (159 loc) · 5.34 KB
/
diagram.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from sage.rings.integer import Integer
class Diagram():
r"""
A list of cells in the grid $\mathbb{N} \times \mathbb{N}$ :
$[(a_1, b_1), (a_2, b_2), ..., (a_n, b_n)]$.
EXAMPLES ::
sage: d = Diagram([(0,0),(3,0),(6,0)])
sage: d = Diagram([(0,0),(1,0),(2,0),(3,0),(0,1),(2,1)])
"""
def __init__(self, cells):
c = True
for cell in cells:
if len(cell) != 2:
c = False
self._cells = []
if c:
for cell in cells:
self._cells += [(cell[1],cell[0])]
self._cells.sort()
else:
print("Wrong format for class Diagram")
def size(self) :
"""
The size of the diagram, which is the number of cells.
OUTPUT: The number of cells of the diagram.
EXAMPLES:
sage: d = Diagram([(0,0),(3,0),(6,0)])
sage: d.size()
3
sage: d = Diagram([(0,0),(1,0),(2,0),(3,0),(0,1),(2,1)])
sage: d.size()
6
"""
return Integer(len(self._cells))
def cells(self):
"""
Gives the list of the matrix coordinates of the cells of the diagram.
OUTPUT: The list of the diagram's cells.
EXAMPLES ::
sage: d = Diagram([(0,0),(3,0),(6,0)])
sage: d.cells()
[(0, 0), (0, 3), (0, 6)]
sage: d = Diagram([(0,0),(1,0),(2,0),(3,0),(0,1),(2,1)])
sage: d.cells()
[(0, 0), (0, 1), (0, 2), (0, 3), (1, 0), (1, 2)]
"""
return self._cells
def nb_cols(self):
"""
Gives the number of columns of the diagram including the empty ones
that lie between unempty ones.
The number of columns thus corresponds to the greatest $a_i$.
OUTPUT: The number of columns of the diagram.
EXAMPLES ::
sage: d = Diagram([(0,0),(3,0),(6,0)])
sage: d.nb_cols()
6
sage: d = Diagram([(0,0),(1,0),(2,0),(3,0),(0,1),(2,1)])
sage: d.nb_cols()
3
"""
return max([c[1] for c in self.cells()])
def draw(self):
"""
Print the diagram.
EXAMPLES ::
sage: Diagram([(0,0),(1,1),(2,2)]).draw()
_
_|_|
_|_|
|_|
sage: Diagram([(0,0), (1,0), (2,0), (0,1), (2,1)]).draw()
_ _
|_|_|_|
|_|_|_|
sage: Diagram([(0,2),(1,1),(2,0)]).draw()
_
|_|_
|_|_
|_|
sage: Diagram([(0,0),(1,1)]).draw()
_
_|_|
|_|
sage: Diagram([(0,1),(1,0),(1,1)]).draw()
_ _
|_|_|
|_|
sage: Diagram([(0,1),(2,2)]).draw()
_
_ |_|
|_|
sage: Diagram([(0,1),(2,3)]).draw()
_
|_|
_
|_|
sage: Diagram([(0,1),(3,2),(3,3)]).draw()
_
|_|
_ |_|
|_|
sage: Diagram([(0,1),(3,2),(2,3)]).draw()
_
|_|_
_ |_|
|_|
"""
cells = self.cells()
max_a = max(a for a,b in cells)
max_b = max(b for a,b in cells)
diag_string = []
k = 0
for a in range(max_a, -2, -1):
string = ''
for b in range(max_b+1):
if a==-1 and ((0,b) in cells):
if (0,b+1) in cells :
string += '|_'
else:
string += '|_|'
elif a==max_a:
if ((a,b) in cells):
string += ' _'
else:
string += ' '
elif ((a,b) in cells):
if b==0 or (a+1,b-1) not in cells:
if (a+1,b) in cells:
if (a+1, b+1) in cells:
string += '|_'
else:
string += '|_|'
else:
string += ' _'
else:
if (a+1,b) in cells:
string += '|_|'
elif (a+1,b+1) in cells:
string += '_'
else :
string += '_ '
else:
if b==0 or (a+1,b-1) not in cells:
if (a+1,b) in cells:
if (a+1, b+1) not in cells:
string += '|_|'
else:
string += '|_'
else:
string += ' '
else:
if (a+1,b) in cells:
string += '|_|'
else:
string += ' '
print(string)