RingSeq
ring_seq.ring_seq.RingSeq
Bases: Generic[T], Sequence[T]
A sequence considered circular, with ring-specific operations.
The ring is stored internally as a tuple. Use to_list(), to_tuple(),
or to_str() to unwrap at the boundary. All transformations return a new
RingSeq; RingSeq instances are immutable and hashable.
Source code in src/ring_seq/ring_seq.py
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 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 | |
__getitem__(i)
__getitem__(i: int) -> T
__getitem__(i: slice) -> RingSeq[T]
Circular indexing and slicing.
Any integer index wraps around the ring. A slice returns a RingSeq that
may contain more elements than the original — cycling through the ring as
needed. Negative step or reversed bounds produce an empty RingSeq.
Examples:
>>> RingSeq("ABC")[-1]
'C'
>>> RingSeq("ABC")[30001]
'B'
>>> RingSeq("ABC")[-1:5].to_str()
'CABCAB'
>>> RingSeq("ABC")[1:3].to_str()
'BC'
>>> RingSeq("ABCDE")[0:5:2].to_str()
'ACE'
Source code in src/ring_seq/ring_seq.py
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 | |
__init__(seq=())
Builds a RingSeq from any iterable (empty by default).
Source code in src/ring_seq/ring_seq.py
124 125 126 | |
align_to(that)
Rotation offset k such that self.start_at(k) == RingSeq(that), or None.
Examples:
>>> RingSeq((0, 1, 2)).align_to((2, 0, 1))
2
>>> RingSeq((0, 1, 2)).align_to((0, 1, 2))
0
>>> RingSeq((0, 1, 2)).align_to((1, 0, 2)) is None
True
Source code in src/ring_seq/ring_seq.py
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 | |
bracelet()
Lexicographically smallest representative under both rotation and reflection.
Examples:
>>> RingSeq((2, 0, 1)).bracelet().to_tuple()
(0, 1, 2)
>>> RingSeq("CBA").bracelet().to_str()
'ABC'
Source code in src/ring_seq/ring_seq.py
675 676 677 678 679 680 681 682 683 684 685 686 687 688 | |
canonical()
Lexicographically smallest rotation (necklace canonical form).
Examples:
>>> RingSeq((2, 0, 1)).canonical().to_tuple()
(0, 1, 2)
>>> RingSeq("CAB").canonical().to_str()
'ABC'
Source code in src/ring_seq/ring_seq.py
662 663 664 665 666 667 668 669 670 671 672 673 | |
canonical_index()
Starting index of the lex-smallest rotation (Booth's algorithm, O(n)).
Examples:
>>> RingSeq((2, 0, 1)).canonical_index()
1
>>> RingSeq(()).canonical_index()
0
Source code in src/ring_seq/ring_seq.py
649 650 651 652 653 654 655 656 657 658 659 660 | |
drop_while(p, from_=0)
Suffix after dropping the longest prefix from from_ whose elements satisfy p.
Examples:
>>> RingSeq((0, 1, 2, 3, 4)).drop_while(lambda x: x < 3, 1).to_tuple()
(3, 4, 0)
Source code in src/ring_seq/ring_seq.py
350 351 352 353 354 355 356 357 358 359 | |
grouped(size)
Groups the ring in fixed-size blocks, wrapping the last block across the seam.
Examples:
>>> [g.to_str() for g in RingSeq("ABCDE").grouped(2)]
['AB', 'CD', 'EA']
Source code in src/ring_seq/ring_seq.py
422 423 424 425 426 427 428 429 430 431 432 433 | |
hamming_distance(that)
Number of positional mismatches between this ring and that.
Examples:
>>> RingSeq((1, 0, 1, 1)).hamming_distance((1, 1, 0, 1))
2
>>> RingSeq((1, 2, 3)).hamming_distance((1, 2, 3))
0
Raises:
| Type | Description |
|---|---|
ValueError
|
if the sizes differ. |
Source code in src/ring_seq/ring_seq.py
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 | |
index(value, start=0, stop=None)
Circular index of the first occurrence of value.
Searches one full revolution by default. Searching past the end wraps around.
Examples:
>>> RingSeq("ABCA").index("A")
0
>>> RingSeq("ABCA").index("A", 1)
3
>>> RingSeq("ABC").index("A", 5)
0
Raises:
| Type | Description |
|---|---|
ValueError
|
if |
Source code in src/ring_seq/ring_seq.py
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 | |
index_from(i)
Normalizes a circular index to [0, len(self)).
Examples:
>>> RingSeq("ABC").index_from(-1)
2
>>> RingSeq("ABC").index_from(3)
0
Raises:
| Type | Description |
|---|---|
ArithmeticError
|
if the ring is empty. |
Source code in src/ring_seq/ring_seq.py
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 | |
is_reflection_of(that)
Whether this ring is a reflection of that.
Examples:
>>> RingSeq("ABC").is_reflection_of("ACB")
True
Source code in src/ring_seq/ring_seq.py
473 474 475 476 477 478 479 480 | |
is_reversion_of(that)
Whether this ring is a reversion of that.
Examples:
>>> RingSeq("ABC").is_reversion_of("CBA")
True
Source code in src/ring_seq/ring_seq.py
482 483 484 485 486 487 488 489 | |
is_rotation_of(that)
Whether this ring is a rotation of that.
Examples:
>>> RingSeq("ABC").is_rotation_of("BCA")
True
>>> RingSeq("ABC").is_rotation_of("ABC")
True
Source code in src/ring_seq/ring_seq.py
462 463 464 465 466 467 468 469 470 471 | |
is_rotation_or_reflection_of(that)
Whether this ring is a rotation and/or reflection of that.
Examples:
>>> RingSeq("ABC").is_rotation_or_reflection_of("BAC")
True
Source code in src/ring_seq/ring_seq.py
491 492 493 494 495 496 497 498 | |
min_rotational_hamming_distance(that)
Minimum Hamming distance over all rotations of this ring.
Examples:
>>> RingSeq((1, 2, 3, 4)).min_rotational_hamming_distance((3, 4, 1, 2))
0
>>> RingSeq((0, 0, 1, 1, 0)).min_rotational_hamming_distance((1, 1, 0, 0, 1))
1
Raises:
| Type | Description |
|---|---|
ValueError
|
if the sizes differ. |
Source code in src/ring_seq/ring_seq.py
540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 | |
reflect_at(i=0)
Reflects the sequence with element at circular index i as the axis head.
Examples:
>>> RingSeq("ABC").reflect_at().to_str()
'ACB'
>>> RingSeq("ABC").reflect_at(1).to_str()
'BAC'
Source code in src/ring_seq/ring_seq.py
278 279 280 281 282 283 284 285 286 287 288 | |
reflectional_symmetry_axes()
Axes of reflectional symmetry as pairs of AxisLocation values.
Examples:
>>> RingSeq((1, 1, 2, 3, 2)).reflectional_symmetry_axes()
[(Vertex(i=3), Edge(i=0, j=1))]
>>> RingSeq("ABC").reflectional_symmetry_axes()
[]
Source code in src/ring_seq/ring_seq.py
608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 | |
reflections()
The sequence and its reflection.
Examples:
>>> [r.to_str() for r in RingSeq("ABC").reflections()]
['ABC', 'ACB']
Source code in src/ring_seq/ring_seq.py
384 385 386 387 388 389 390 391 392 393 | |
reversions()
The sequence and its reversion.
Examples:
>>> [r.to_str() for r in RingSeq("ABC").reversions()]
['ABC', 'CBA']
Source code in src/ring_seq/ring_seq.py
395 396 397 398 399 400 401 402 403 404 | |
rotate_left(step)
Rotates the sequence left by step positions.
Examples:
>>> RingSeq("ABC").rotate_left(1).to_str()
'BCA'
Source code in src/ring_seq/ring_seq.py
260 261 262 263 264 265 266 267 | |
rotate_right(step)
Rotates the sequence right by step positions.
Examples:
>>> RingSeq("ABC").rotate_right(1).to_str()
'CAB'
Source code in src/ring_seq/ring_seq.py
247 248 249 250 251 252 253 254 255 256 257 258 | |
rotational_symmetry()
Order of rotational symmetry: number of rotations in which the ring looks the same.
Examples:
>>> RingSeq("-|--|--|--|-").rotational_symmetry()
4
>>> RingSeq("-|+-|+-|+-|+").rotational_symmetry()
4
>>> RingSeq([0, 1, 0, 1]).rotational_symmetry()
2
Source code in src/ring_seq/ring_seq.py
572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 | |
rotations()
All rotations of this ring, one step at a time to the left.
Examples:
>>> [r.to_str() for r in RingSeq("ABC").rotations()]
['ABC', 'BCA', 'CAB']
Source code in src/ring_seq/ring_seq.py
373 374 375 376 377 378 379 380 381 382 | |
rotations_and_reflections()
All rotations of the sequence and of its reflection.
Examples:
>>> [r.to_str() for r in RingSeq("ABC").rotations_and_reflections()]
['ABC', 'BCA', 'CAB', 'ACB', 'CBA', 'BAC']
Source code in src/ring_seq/ring_seq.py
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 | |
span(p, from_=0)
Splits at the first element, starting at from_, that does not satisfy p.
Examples:
>>> take, drop = RingSeq((0, 1, 2, 3, 4)).span(lambda x: x < 3, 1)
>>> take.to_tuple(), drop.to_tuple()
((1, 2), (3, 4, 0))
Source code in src/ring_seq/ring_seq.py
361 362 363 364 365 366 367 368 369 | |
start_at(i)
Rotates the sequence to start at circular index i (equivalent to rotate_left(i)).
Examples:
>>> RingSeq("ABC").start_at(1).to_str()
'BCA'
Source code in src/ring_seq/ring_seq.py
269 270 271 272 273 274 275 276 | |
symmetry()
Order of reflectional (mirror) symmetry.
Examples:
>>> RingSeq("-|--|--|--|-").symmetry()
4
>>> RingSeq("-|+-|+-|+-|+").symmetry()
0
Source code in src/ring_seq/ring_seq.py
636 637 638 639 640 641 642 643 644 645 | |
symmetry_indices()
Reflection shifts: shift values such that this ring equals its reversal rotated left.
Each shift identifies one axis of reflectional symmetry.
Examples:
>>> RingSeq("-|--|--|--|-").symmetry_indices()
[0, 3, 6, 9]
>>> RingSeq("-|+-|+-|+-|+").symmetry_indices()
[]
Source code in src/ring_seq/ring_seq.py
591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 | |
take_while(p, from_=0)
Longest prefix from circular index from_ whose elements satisfy p.
Examples:
>>> RingSeq((0, 1, 2, 3, 4)).take_while(lambda x: x < 3, 1).to_tuple()
(1, 2)
>>> RingSeq((0, 1, 2, 3, 4)).take_while(lambda x: x != 1, 3).to_tuple()
(3, 4, 0)
Source code in src/ring_seq/ring_seq.py
337 338 339 340 341 342 343 344 345 346 347 348 | |
to_list()
Returns the ring as a new list.
Examples:
>>> RingSeq("ABC").to_list()
['A', 'B', 'C']
Source code in src/ring_seq/ring_seq.py
197 198 199 200 201 202 203 204 | |
to_str(sep='')
Joins the elements into a string using sep.
Examples:
>>> RingSeq("ABC").to_str()
'ABC'
>>> RingSeq([1, 2, 3]).to_str("-")
'1-2-3'
Source code in src/ring_seq/ring_seq.py
215 216 217 218 219 220 221 222 223 224 | |
to_tuple()
Returns the ring as a tuple (the internal storage).
Examples:
>>> RingSeq([1, 2, 3]).to_tuple()
(1, 2, 3)
Source code in src/ring_seq/ring_seq.py
206 207 208 209 210 211 212 213 | |
zip_with_index(from_=0)
Iterates over (element, original-index) pairs, starting at from_.
Examples:
>>> list(RingSeq(("a", "b", "c")).zip_with_index(1))
[('b', 1), ('c', 2), ('a', 0)]
>>> list(RingSeq(("a", "b", "c")).zip_with_index())
[('a', 0), ('b', 1), ('c', 2)]
Source code in src/ring_seq/ring_seq.py
435 436 437 438 439 440 441 442 443 444 445 446 447 448 | |
AxisLocation types
ring_seq.ring_seq.AxisLocation
A location on the circular sequence where a symmetry axis can pass through.
Vertex: the axis passes directly through the element at that index.Edge: the axis passes between the elements at those two indices.
Source code in src/ring_seq/ring_seq.py
41 42 43 44 45 46 | |
ring_seq.ring_seq.Vertex
dataclass
Bases: AxisLocation
A symmetry axis location passing through a single element.
Source code in src/ring_seq/ring_seq.py
49 50 51 52 53 | |
ring_seq.ring_seq.Edge
Bases: AxisLocation
A symmetry axis location passing between two consecutive elements of a circular sequence.
The invariant j == (i + 1) % n is enforced at construction. Pattern matching
with match e: case Edge(i, j): ... still works.
Source code in src/ring_seq/ring_seq.py
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 | |
__init__(i, n)
Builds the edge starting at circular index i in a ring of size n.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
i
|
Index
|
circular index of the first endpoint (any integer, normalized to |
required |
n
|
int
|
the ring size; must be positive |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
if |
Source code in src/ring_seq/ring_seq.py
65 66 67 68 69 70 71 72 73 74 75 76 77 78 | |