| 857 | 857 | #g) (Permutation) = adjust size or return copy |
| 858 | 858 | ok = True |
| 859 | 859 | if not args: # a |
| 860 | return _af_new(list(range(size or 0))) | |
| 860 | return cls._af_new(list(range(size or 0))) | |
| 861 | 861 | elif len(args) > 1: # c |
| 862 | return _af_new(Cycle(*args).list(size)) | |
| 862 | return cls._af_new(Cycle(*args).list(size)) | |
| 863 | 863 | if len(args) == 1: |
| 864 | 864 | a = args[0] |
| 865 | 865 | if isinstance(a, Perm): # g |
| 866 | 866 | if size is None or size == a.size: |
| 867 | return a | |
| 868 | return Perm(a.array_form, size=size) | |
| 867 | # Create a new instance of the subclass with the same data | |
| 868 | return cls._af_new(a.array_form) | |
| 869 | return cls(a.array_form, size=size) | |
| 869 | 870 | if isinstance(a, Cycle): # f |
| 870 | return _af_new(a.list(size)) | |
| 871 | return cls._af_new(a.list(size)) | |
| 871 | 872 | if not is_sequence(a): # b |
| 872 | return _af_new(list(range(a + 1))) | |
| 873 | return cls._af_new(list(range(a + 1))) | |
| 873 | 874 | if has_variety(is_sequence(ai) for ai in a): |
| 874 | 875 | ok = False |
| 875 | 876 | else: |
| 924 | 925 | obj._size = size |
| 925 | 926 | return obj |
| 926 | 927 | |
| 927 | @staticmethod | |
| 928 | def _af_new(perm): | |
| 928 | @classmethod | |
| 929 | def _af_new(cls, perm): | |
| 929 | 930 | """A method to produce a Permutation object from a list; |
| 930 | 931 | the list is bound to the _array_form attribute, so it must |
| 931 | 932 | not be modified; this method is meant for internal use only; |
| 944 | 945 | Permutation([2, 1, 3, 0]) |
| 945 | 946 | |
| 946 | 947 | """ |
| 947 | p = Basic.__new__(Perm, perm) | |
| 948 | p = Basic.__new__(cls, perm) | |
| 948 | 949 | p._array_form = perm |
| 949 | 950 | p._size = len(perm) |
| 950 | 951 | return p |
| Test Name | Status |
|---|---|
test_Permutation_subclassing | Fail |
test_Permutation | Pass |
test_josephus | Pass |
test_ranking | Pass |
test_mul | Pass |
test_args | Pass |
test_Cycle | Pass |
test_from_sequence | Pass |
test_printing_cyclic | Pass |
Loading...
Ridges.AI© 2025 Ridges AI. Building the future of decentralized AI development.
