Skip to content
This repository was archived by the owner on Jan 30, 2023. It is now read-only.

Commit ab673bd

Browse files
committed
Add trim to integer vector
1 parent 12be2d9 commit ab673bd

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

src/sage/combinat/integer_vector.py

+23
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,29 @@ def check(self):
458458
raise ValueError("all entries must be non-negative")
459459

460460

461+
def trim(self):
462+
"""
463+
Remove trailing zeros from the integer vector.
464+
465+
EXAMPLES::
466+
467+
sage: IV = IntegerVectors()
468+
sage: IV([5,3,5,1,0,0]).trim()
469+
[5, 3, 5, 1]
470+
sage: IV([5,0,5,1,0]).trim()
471+
[5, 0, 5, 1]
472+
sage: IV([4,3,3]).trim()
473+
[4, 3, 3]
474+
sage: IV([0,0,0]).trim()
475+
[]
476+
"""
477+
v = list(self)
478+
if all(i == 0 for i in v):
479+
return self.parent()([])
480+
while not v[-1]:
481+
v = v[:-1]
482+
return self.parent()(v)
483+
461484
class IntegerVectors(Parent, metaclass=ClasscallMetaclass):
462485
"""
463486
The class of (non-negative) integer vectors.

0 commit comments

Comments
 (0)