for i in range(1,len(line)+1):
raise ValueError("The input must a one-line notation of a permutation on [{}]. The number {} does not appear in {}".format(len(line),i,line))
return 'My Easy Permutation with one-line notation {}'.format(self._line)
def compose(self,infront):
infront is applied, then self
for pos in range(0, self._n):
new[pos]=self._line[infront._line[pos]-1]
Return a list with descents positions (where possible positions
are 1,2, ..., n-1). A position k = 1,2, ..., n-1 is called
a descent of a permutation p if p(k+1)<p(k).
for pos in range(0,self._n-1):
if self._line[pos]>self._line[pos+1]:
def number_of_descents(self):
return the number of descents of ``self``.
sage: EasyPerm([2,1,4,3]).number_of_descents()
return len(self.descents())
def to_cycles(self, singletons=True):
Return the permutation ``self`` as a list of disjoint cycles.
The cycles are returned in the order of increasing smallest
elements, and each cycle is returned as a tuple which starts
with its smallest element.
If ``singletons=False`` is given, the list does not contain the
sage: EasyPerm([2,1,3,4]).to_cycles()
sage: EasyPerm([2,1,3,4]).to_cycles(singletons=False)
sage: EasyPerm([4,1,5,2,6,3]).to_cycles()
The algorithm is of complexity `O(n)` where `n` is the size of the
while next != cycleFirst:
l[next - 1], next = False, l[next - 1]
if singletons or len(cycle) > 1:
cycles.append(tuple(cycle))
def is_derangement(self):
A fixed point of a permutation p is an element i such that p(i)=i.
A derangement is a permutation that has no fixed points.
Define a function called is_derangement that
returns True if p is a derangement and returns False otherwise.
sage: EasyPerm([2,1,4,3]).is_derangement()
sage: EasyPerm([2,1,3,4]).is_derangement()
for pos in range(0,self._n):
if self._line[pos]==pos+1:
if not EasyPerm(m).to_cycles(singletons=True) == m.to_cycles():
if not EasyPerm(m).to_cycles(singletons=False) == m.to_cycles(singletons=False):
if not len(EasyPerm(m).descents()) == len(m.descents()):
if not EasyPerm(m).number_of_descents() == m.number_of_descents():
if EasyPerm(m).is_derangement() and len(m.fixed_points())>0:
if (not EasyPerm(m).is_derangement()) and len(m.fixed_points())==0:
p = EasyPerm([2,1,4,3,5])
print 'The cycle notation of {} is'.format(p), EasyPerm([2,1,4,3]).to_cycles()
print 'The descent positions of {} are'. format(p), EasyPerm([2,1,4,3]).descents()
print 'Is {} a derangement? '.format(p), p.is_derangement()