Skip to content

Instantly share code, notes, and snippets.

@s-hiiragi
Created February 16, 2025 04:51
Show Gist options
  • Save s-hiiragi/57681a77b90bec0220164f4205408f56 to your computer and use it in GitHub Desktop.
Save s-hiiragi/57681a77b90bec0220164f4205408f56 to your computer and use it in GitHub Desktop.
nCkの組合せのリストを生成する
# https://x.com/ellnore_pad_267/status/1889922279750160791/photo/1
N = 5
k = 3
t = [None] * k
r = []
print(f'{N=}, {r=}, {t=}')
def combination(x, i, prefix, indent):
sp = '^ ' * indent
print(f'{prefix}{sp}({x}, {i})')
if i == len(t):
print(f'{prefix}{sp}append {t}')
r.append(t[:])
return
if x == N:
return
combination(x+1, i, 'L', indent+1)
t[i] = x
combination(x+1, i+1, 'R', indent+1)
combination(x=0, i=0, prefix=' ', indent=0)
print(f'{len(r)=}')
for t in r:
print(t)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment