>>> s = 'abc'
>>> results = sorted([''.join(c) for l in range(len(s)) for c in permutations(s, l+1)])
['a', 'b', 'c', 'ab', 'ac', 'ba', 'bc', 'ca', 'cb', 'abc', 'acb', 'bac', 'bca', 'cab', 'cba']
這個寫法沒什麼特別的,不過是使用兩個 for 的 list comprehension 罷了。 同時,它回傳的是一個 string 的 list,與原題目比較接近。
def get_combinations(string):
combs = []
for i in range(1, 2**len(string)):
pat = "{0:b}".format(i).zfill(len(string))
combs.append(''.join(c for c, b in zip(string, pat) if int(b)))
return combs
def get_permutations(clst):
if len(clst)==1:
return [clst[0]]
results = []
for idx, c in enumerate(clst):
results += [c+substr for substr in get_permutations(clst[:idx] + clst[idx+1:])]
return results
# encoding:utf-8
def addChar(strs, chars):
result = []
for str in strs:
for char in chars:
result.append(str + char)
return result
chars = ['a', 'b', 'c']
group = {}
count = len(chars)
for i in xrange(1, count + 1):
if i == 1:
group[i] = addChar([''], chars)
else:
group[i] = addChar(group[i - 1], chars)
# 合并数组
result = []
for i in group:
result += group[i]
print result
result = []
def function(arg, string):
global result
if len(arg) >= len(string):
return None
for alphabet in string:
if alphabet in arg:
continue
function(arg+alphabet, string)
result.append(arg+alphabet)
string = 'abc'
for alphabet in string:
result.append(alphabet)
function(alphabet, string)
print list(set(result))
# coding: utf-8
import itertools as t
li = ['a', 'b', 'c']
tmp = []
for n in range(1, len(li) + 1):
x = t.permutations(li, n)
for i in x:
tmp.append(''.join(i))
print tmp
我覺得這個問題相當有趣,做為一個 Python 狂熱者,我不能同意 @garry_qian 的答案更多了,既然 Python 都提供了那麼好用的標準庫,不使用一下實在是太可惜了,在此立場下,一個
簡潔,簡短(好啦並沒有...),但邏輯上基本相同的做法如下:這個寫法沒什麼特別的,不過是使用兩個
for的 list comprehension 罷了。同時,它回傳的是一個 string 的 list,與原題目比較接近。
但是這激發了我的一些好奇心,想自己來寫寫看,同時以沒有這些排列組合工具的他種語言來說,也許比較容易利用相同的邏輯來完成。
首先我完成的是關於組合的 function,他代入一個字串並且回傳所有長度的所有字元組合,但不排列:
測試:
一如預期,我們拿到:
長度為 1 的
'c', 'b', 'a'長度為 2 的
'bc', 'ac', 'ab'長度為 3 的
'abc'果然是各種長度下的所有組合都到手了。
這個寫法肯定不是最好的,但我覺得想法滿有趣的。想法就是,要考慮
'abc'的所有組合,那不就是分別考慮a要不要取,b要不要取 和c要不要取,於是總共2*2*2 = 8(2**len(string)) 種組合,那不就正好對應到:所以在
get_combinations中,用了一點技巧去生出從 1 到 7 的二進位碼,再根據 0 與 1 決定每一種組合該取用那些字元。這還沒完成任務,我們距離標準答案,還必須取得:
這產生了
get_permutations這個 function:測試:
邏輯很簡單,用 recursive 的方式去找出
固定長度字元組合所有的排列。有了以上兩種 function,我們就可以求出答案囉:
結論:
別重複發明輪胎,這不但累死你,還很顯笨
人生苦短,我用 Python
既然同时打了
php和python标签,那就用两种方式都写下吧,逻辑用的一样的。php代码python代码python2.7,和@garry_qian 相同,写完才发现有了,其他楼的python方案我都懒得看了
P(2,3)
P(3,3)
12种可能性
假设字符串的长度为2, 那所有的组合就是: 2! + 2! / 1! = 4
假设字符串的长度为3, 那所有的组合就是: 3! + 3! / 1! + 3! / 2! = 15
假设字符串的长度为4, 那所有的组合就是: 4! + 4! / 1! + 4! / 2! + 4! / 3! = 64
这个公式可以进行推广
n! + n! / 1! + n! / 2! + ... + n! / (n-1)!
代码就不贴了