permutationsメソッドを実装し実行したがうまく出力されないです。
a = [2,5,7,3,6] def permutations(iterable, r=None): # permutations('ABCD', 2) --> AB AC AD BA BC BD CA CB CD DA DB DC # permutations(range(3)) --> 012 021 102 120 201 210 pool = tuple(iterable) n = len(pool) r = n if r is None else r if r > n: return indices = list(range(n)) cycles = list(range(n, n-r, -1)) yield tuple(pool[i] for i in indices[:r]) while n: for i in reversed(range(r)): cycles[i] -= 1 if cycles[i] == 0: indices[i:] = indices[i+1:] + indices[i:i+1] cycles[i] = n - i else: j = cycles[i] indices[i], indices[-j] = indices[-j], indices[i] yield tuple(pool[i] for i in indices[:r]) break else: return print(permutations(a, r=None))
とコードを書いて実行すると
<generator object permutations at 0x102840f50>
と出力されました。
printでは
2,5,7,3,6
や
2,5,3,7,6
や
5,2,3,7,6
などの配列の要素を順列で並べ替えたものを全部出力させたいです。
なのに今はObjectが出力されてしまいます。
どう直せばよいのでしょうか?
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2017/06/30 04:15
2017/06/30 04:21