>>> expand_tuple((2, 4), 2) (1, 1, 2, 2) >>> expand_tuple((2, 4), 3) (1, 1, 1, 1, 2) >>> expand_tuple((3, 4), 2) (1, 2, 2, 2) >>> expand_tuple((7, 4), 3) (2, 2, 3, 1, 1, 2)
(chunks, factor)
| 227 | |
| 228 | |
| 229 | def expand_tuple(chunks, factor): |
| 230 | """ |
| 231 | |
| 232 | >>> expand_tuple((2, 4), 2) |
| 233 | (1, 1, 2, 2) |
| 234 | |
| 235 | >>> expand_tuple((2, 4), 3) |
| 236 | (1, 1, 1, 1, 2) |
| 237 | |
| 238 | >>> expand_tuple((3, 4), 2) |
| 239 | (1, 2, 2, 2) |
| 240 | |
| 241 | >>> expand_tuple((7, 4), 3) |
| 242 | (2, 2, 3, 1, 1, 2) |
| 243 | """ |
| 244 | if factor == 1: |
| 245 | return chunks |
| 246 | |
| 247 | out = [] |
| 248 | for c in chunks: |
| 249 | x = c |
| 250 | part = max(x / factor, 1) |
| 251 | while x >= 2 * part: |
| 252 | out.append(int(part)) |
| 253 | x -= int(part) |
| 254 | if x: |
| 255 | out.append(x) |
| 256 | assert sum(chunks) == sum(out) |
| 257 | return tuple(out) |
| 258 | |
| 259 | |
| 260 | def contract_tuple(chunks, factor): |
searching dependent graphs…