Re: D'Oliveiro: I saw your recursion/permutation post on comp.lang.c

Liste des GroupesRevenir à ol advocacy 
Sujet : Re: D'Oliveiro: I saw your recursion/permutation post on comp.lang.c
De : nospam (at) *nospam* dfs.com (DFS)
Groupes : comp.os.linux.advocacy
Date : 25. Apr 2024, 01:58:29
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <v0c6b4$2jp69$1@dont-email.me>
References : 1
User-Agent : Betterbird (Windows)
On 4/24/2024 10:04 AM, DFS wrote:

Heap's algorithm
Improvements: lexico sort and enumerate the output, remove list punctuation, line break on each change of the first element
----------------------------------------------------------------------------
import sys
def permute(k, a):
global b
if k == 1:
b.append(list(a))
else:
permute(k-1, a)
for i in range(k-1):
if k % 2 == 0:
a[i], a[k-1] = a[k-1], a[i]
else:
a[0], a[k-1] = a[k-1], a[0]
permute(k-1, a)
a,b = [],[]
for i in range(1,len(sys.argv)):
a.append(sys.argv[i])
permute(len(a),a)
e = sorted(b)[0][0]
for i, itm in enumerate(sorted(b)):
if len(b) > 2:
if itm[0] != e:
e = itm[0]
print()
print("%2d." % (i+1),end = ' ')
print(*itm)
----------------------------------------------------------------------------

$ python3 permute_Heap.py dance the night away
  1. away dance night the
  2. away dance the night
  3. away night dance the
  4. away night the dance
  5. away the dance night
  6. away the night dance
  7. dance away night the
  8. dance away the night
  9. dance night away the
10. dance night the away
11. dance the away night
12. dance the night away
13. night away dance the
14. night away the dance
15. night dance away the
16. night dance the away
17. night the away dance
18. night the dance away
19. the away dance night
20. the away night dance
21. the dance away night
22. the dance night away
23. the night away dance
24. the night dance away
previously
['dance', 'the', 'night', 'away']
['the', 'dance', 'night', 'away']
['night', 'dance', 'the', 'away']
['dance', 'night', 'the', 'away']
['the', 'night', 'dance', 'away']
['night', 'the', 'dance', 'away']
['away', 'the', 'dance', 'night']
['the', 'away', 'dance', 'night']
['dance', 'away', 'the', 'night']
['away', 'dance', 'the', 'night']
['the', 'dance', 'away', 'night']
['dance', 'the', 'away', 'night']
['dance', 'night', 'away', 'the']
['night', 'dance', 'away', 'the']
['away', 'dance', 'night', 'the']
['dance', 'away', 'night', 'the']
['night', 'away', 'dance', 'the']
['away', 'night', 'dance', 'the']
['away', 'night', 'the', 'dance']
['night', 'away', 'the', 'dance']
['the', 'away', 'night', 'dance']
['away', 'the', 'night', 'dance']
['night', 'the', 'away', 'dance']
['the', 'night', 'away', 'dance']

Date Sujet#  Auteur
24 Apr 24 * D'Oliveiro: I saw your recursion/permutation post on comp.lang.c2DFS
25 Apr 24 `- Re: D'Oliveiro: I saw your recursion/permutation post on comp.lang.c1DFS

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal