MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / generate

Function generate

divide_and_conquer/heaps_algorithm.py:35–47  ·  view source on GitHub ↗
(k: int, arr: list)

Source from the content-addressed store, hash-verified

33 res = []
34
35 def generate(k: int, arr: list):
36 if k == 1:
37 res.append(tuple(arr[:]))
38 return
39
40 generate(k - 1, arr)
41
42 for i in range(k - 1):
43 if k % 2 == 0: # k is even
44 arr[i], arr[k - 1] = arr[k - 1], arr[i]
45 else: # k is odd
46 arr[0], arr[k - 1] = arr[k - 1], arr[0]
47 generate(k - 1, arr)
48
49 generate(len(arr), arr)
50 return res

Callers 1

heapsFunction · 0.70

Calls 1

appendMethod · 0.45

Tested by

no test coverage detected