Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
Perm((1, 3, 4, 2, 0))
"""
times = times % 4
if times == 0:
return self
if times == 2:
return self.reverse_complement()
n = len(self)
result = [0] * n
if times == 1:
for idx, val in enumerate(self):
result[val] = n - idx - 1
else:
for idx, val in enumerate(self):
result[n - val - 1] = idx
return Perm(result)
def remove_element(self, selected: Optional[int] = None) -> "Perm":
"""Return the perm acquired by removing a specific element from self. It
defaults to the largest element.
Examples:
>>> Perm((3, 0, 1, 2)).remove_element()
Perm((0, 1, 2))
>>> Perm((3, 0, 2, 1)).remove_element(0)
Perm((2, 1, 0))
"""
if selected is None:
if len(self) == 0:
return self
selected = len(self) - 1
assert 0 <= selected < len(self)
return Perm(
val if val < selected else val - 1 for val in self if val != selected
)
stackL = [] # the left stack read from top to bottom
out = []
while len(out) < len(perm):
if inp and Perm.to_standard(stackR + [inp[0]]).reverse().avoids(*B):
stackR.append(inp.pop(0))
elif stackR and (
Perm.to_standard(stackL + [stackR[-1]]).reverse().avoids(Perm((1, 0)))
):
stackL.append(stackR.pop())
elif stackL:
out.append(stackL.pop())
else:
print("wtf")
print(out, stackL, stackR, inp)
assert False
return Perm(out)
def _add_point_new_perm(self, x: int, y: int) -> Perm:
iterator = iter(self.pattern)
return Perm(
chain(
(val if val < y else (val + 1) for val in islice(iterator, x)),
(y,),
(val if val < y else (val + 1) for val in iterator),
)
Mesh pattern:
Occurrences of instances's perm in the pattern's perm is found, and if
the sub mesh pattern formed by the occurrence indices is a superset of
the instance shading, they are included.
Example:
>>> mp = MeshPatt(Perm((1, 0, 2)), [(1, 2), (2, 2), (2, 3)])
>>> p = Perm((3, 1, 0, 2, 4))
>>> sorted(mp.occurrences_in(p))
[(0, 1, 4), (0, 2, 4), (0, 3, 4), (1, 2, 3)]
>>> mp2 = MeshPatt(p, [(0,0), (0,1), (0,2), (1,4), (2,4), (3,3), (3,4),
... (3,5), (4,0), (4,3), (4,4), (4,5), (5,0)])
>>> sorted(mp.occurrences_in(mp2))
[(0, 2, 4), (0, 3, 4)]
"""
assert isinstance(patt, (Perm, MeshPatt))
if isinstance(patt, Perm):
yield from self._occurrences_in_perm(patt)
else:
yield from self._occurrences_in_mesh(patt)
def _quick_sort(perm_slice: List[int]) -> List[int]:
assert not perm_slice or set(perm_slice) == set(
range(min(perm_slice), max(perm_slice) + 1)
)
n = len(perm_slice)
if n == 0:
return perm_slice
maxind = -1
# Note that perm does not need standardizing as sfp uses left to right maxima.
for maxind in Perm(perm_slice).strong_fixed_points():
pass
if maxind != -1:
lis: List[int] = (
_quick_sort(perm_slice[:maxind])
+ [perm_slice[maxind]]
+ _quick_sort(perm_slice[maxind + 1 :])
)
else:
firstval = perm_slice[0]
lis = (
list(filter(lambda x: x < firstval, perm_slice))
+ [perm_slice[0]]
+ list(filter(lambda x: x > firstval, perm_slice))
)
return lis
def complement(self) -> "Perm":
"""Return the complement of the perm self.
Examples:
>>> Perm((1, 2, 3, 0, 4)).complement()
Perm((3, 2, 1, 4, 0))
>>> Perm((2, 0, 1)).complement()
Perm((0, 2, 1))
"""
base = len(self) - 1
return Perm(base - element for element in self)
def compose(self, *others: "Perm") -> "Perm":
"""Return the composition of two or more perms.
Examples:
>>> Perm((0, 3, 1, 2)).compose(Perm((2, 1, 0, 3)))
Perm((1, 3, 0, 2))
>>> Perm((1, 0, 2)).compose(Perm((0, 1, 2)), Perm((2, 1, 0)))
Perm((2, 0, 1))
"""
assert all(
isinstance(other, Perm) and len(other) == len(self) for other in others
)
return Perm(self._composed_value(idx, *others) for idx in range(len(self)))