-
-
Notifications
You must be signed in to change notification settings - Fork 660
add method is_simple to permutations #35446
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd like to see a little more in the description/examples so that a user can more readily verify that they understand the definition correctly without having to go to the OEIS link.
Description could mention that [6,1,3,5,2,4] is not simple because it maps the interval [3,4,5,6] to [2,3,4,5], whereas [2,6,3,5,1,4] is simple. And then have those both be tests/examples (along with the cardinality check).
2b26981
to
7fa158d
Compare
thanks for the review. I have added your suggestions. |
Isn't there a linear algorithm? At least the following is sub-quadratic def is_simple(self):
r"""
EXAMPLES::
sage: [sum(is_simple(pi) for pi in Permutations(n)) for n in range(8)]
[1, 1, 2, 0, 2, 6, 46, 338]
"""
n = len(self)
if n <= 2:
return True
# testing intervals starting at position 0
start = 0
left = right = self._list[0]
end = 1
while end < n - 1:
elt = self._list[end]
if elt < left:
left = elt
elif elt > right:
right = elt
if right - left == end - start:
return False
elif right - left == n - 1:
break
end += 1
# testing intervals starting at later positions
for start in range(1, n - 1):
left = right = self._list[start]
end = start + 1
while end < n:
elt = self._list[end]
if elt < left:
left = elt
elif elt > right:
right = elt
if right - left == end - start:
return False
elif right - left > n - 1 - start:
break
end += 1
return True |
oui, c'est déjà mieux. Il faudrait chercher dans la littérature si quelqu'un a un meilleur algo. On peut aussi mettre le code en cython. |
|
||
TESTS:: | ||
|
||
sage: [len([pi for pi in Permutations(n) if pi.is_simple()]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this only a TEST? It is relevant for the user (not the most efficient way to get the list of simple permutations though).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Comme tu veux. Mais je ne vois pas ce que ca apporte. On a le lien OEIS.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Précisément, c'est un lien, il faut cliquer dessus pour avoir l'info.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Voilà. On cythonise ou pas ? J'ai la flemme, et d'autres occupations plus pressantes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Je vois pas trop l'intérêt à ce stade.
Documentation preview for this PR is ready! 🎉 |
merci Vincent. |
📚 Description
Add one method to permutations, checking if it is simple (not sending any proper sub-interval to a sub-interval)
Also fixing a few pep8 warnings in the modified file on the way.
📝 Checklist
⌛ Dependencies