' Compute permutations. ' def permutenext ( values ): ' Alter the list of values in-place to produce to the next permutation in lexicographical order. 'values' must support slicing and::reverse. ' last = len ( values ) - 1 a = last while a 0: b = a a -= 1 if values a = values c : # = allows duplicates c -= 1 values a , values c = values c , values a # Swap. Values b: = values : b - 1: - 1 # Reverse.
![]()
Return values. Reverse def permuteprev ( values ): ' Alter the list of values in-place to produce to the previous permutation in lexicographical order. 'values' must support slicing and::reverse. ' last = len ( values ) - 1 a = last while a 0: b = a a -= 1 if values a values b : c = last while values a values or current seen start ) self. AssertEqual ( expect, len ( seen )) def test0 ( self ): self.
Name: Fission Version: 2.3.0 Mac Platform: Intel Includes: KG OS version: 10.9 or later 64-bit Link for more information:.
Count ( , 1 ) def test1 ( self ): self. Count ( 42 , 1 ) def test2 ( self ): self. Count ( 1, 1 , 1 ) self. Count ( 1, 2 , 2 ) self. Count ( 2, 1 , 2 ) def test3 ( self ): self. Count ( range ( 3 ), 6 ) self.
Count ( range ( 3, 0, - 1 ), 6 ) self. Count ( 1, 1, 1 , 1 ) self.
Count ( 1, 1, 2 , 3 ) self. Count ( 1, 2, 1 , 3 ) self. Count ( 2, 1, 1 , 3 ) self. Count ( 1, 2, 2 , 3 ) self. Count ( 2, 1, 2 , 3 ) self.
Count ( 2, 2, 1 , 3 ) def test4 ( self ): self. Count ( range ( 4 ), 24 ) self. Count ( range ( 4, 0, - 1 ), 24 ) self. Count ( 1, 1, 1, 1 , 1 ) self. Count ( 1, 1, 1, 2 , 4 ) self. Count ( 1, 1, 2, 1 , 4 ) self.
Count ( 1, 2, 1, 1 , 4 ) self. Count ( 2, 1, 1, 1 , 4 ) self.
![]()
Count ( 1, 2, 2, 2 , 4 ) self. Count ( 2, 1, 2, 2 , 4 ) self. Count ( 2, 2, 1, 2 , 4 ) self. Count ( 2, 2, 2, 1 , 4 ) self. Count ( 1, 1, 2, 2 , 6 ) self. Count ( 1, 2, 2, 1 , 6 ) self. Count ( 2, 2, 1, 1 , 6 ) self.
Count ( 2, 1, 1, 2 , 6 ) self. Count ( 1, 2, 1, 2 , 6 ) self. Count ( 2, 1, 2, 1 , 6 ) def test5 ( self ): self. Count ( 'c', 'a', 'a', 'a', 'c', 'd', 'd' , 210 ) def test : unittest. Main def run ( values ): seen = while values not in seen: print values seen. Append ( values :) permuteprev ( values ) if name 'main': from optparse import OptionParser parser = OptionParser ( usage = '%prog options x y z.'
, description = 'Show all permutations.' Addoption ( '-t', '-test', action = 'storetrue', help = 'Run tests.' ) opts, extras = parser. Parseargs if opts.
Test: import sys sys. Argv 1: = extras test try: run ( map ( int, extras )) except ValueError: run ( extras ).
![]() Comments are closed.
|
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
February 2023
Categories |