Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # kirk_lawrence_algorithm.py ??? not sure if i invented this
- def str2ints(n):
- return [int(digit) for digit in n]
- def ints2str(n):
- return [str(digit) for digit in n]
- def next_permutation(n):
- if '9'*len(n) == n:
- return None
- n = str2ints(n)
- if len(set(n)) == 1:
- max_digit = n[0] + 1
- return '0' * (len(n) - 1) + str(max_digit)
- i = len(n) - 2
- while i >= 0 and n[i] >= n[i + 1]:
- i -= 1
- if i < 0:
- n[-1] += 1
- return ''.join(map(str, n[::-1]))
- j = len(n) - 1
- while n[j] <= n[i]:
- j -= 1
- n[i], n[j] = n[j], n[i]
- n[i + 1:] = n[i + 1:][::-1]
- return ''.join(map(str, n))
- # Test the function
- perm = '000'
- while perm is not None:
- print(perm, end=' ')
- perm = next_permutation(perm)
- '''
- 000 001 010 100 101 110 111 002 020 200 102 120 201 210 112 121 211 212 221 222 003 030 300 103 130 301 310 113 131 311 213 231 312 321 223 232 322 323 332 333 004 040 400 104 140 401 410 114 141 411 214 241 412 421 224 242 422 324 342 423 432 334 343 433 434 443 444 005 050 500 105 150 501 510 115 151 511 215 251 512 521 225 252 522 325 352 523 532 335 353 533 435 453 534 543 445 454 544 545 554 555 006 060 600 106 160 601 610 116 161 611 216 261 612 621 226 262 622 326 362 623 632 336 363 633 436 463 634 643 446 464 644 546 564 645 654 556 565 655 656 665 666 007 070 700 107 170 701 710 117 171 711 217 271 712 721 227 272 722 327 372 723 732 337 373 733 437 473 734 743 447 474 744 547 574 745 754 557 575 755 657 675 756 765 667 676 766 767 776 777 008 080 800 108 180 801 810 118 181 811 218 281 812 821 228 282 822 328 382 823 832 338 383 833 438 483 834 843 448 484 844 548 584 845 854 558 585 855 658 685 856 865 668 686 866 768 786 867 876 778 787 877 878 887 888 009 090 900 109 190 901 910 119 191 911 219 291 912 921 229 292 922 329 392 923 932 339 393 933 439 493 934 943 449 494 944 549 594 945 954 559 595 955 659 695 956 965 669 696 966 769 796 967 976 779 797 977 879 897 978 987 889 898 988 989 998 999
- '''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement