以下是我努力的求職面試問題。 (不必在列表和集合之間切換,測試它並意識到它缺少預期輸出,步驟太多)。 如果可能的話,尋找正確的答案,或者是我應該如何解決問題的指南。謝謝。字符串的組合
問題:給一個字符串,從它找到所有可能的組合(正面和反面)。 打印組合的所有組合和總數。順序無關緊要。
實施例S = '馬球'
前回答= 'P', 'PO', 'POL', '馬球', '醇', ''010', 'LO',「O ','l'。
反向回答:'o','ol','olo','olop', 'lop','op','p','l'。
我的回答:
count = 0
count2 = -1
length = len(s)
my_list = []
for i in s:
temp = s[count:]
temp2 = s[:count2]
my_list.append(i)
my_list.append(temp)
my_list.append(temp2)
count += 1
count2 -= 1
my_set = set(my_list)
for f in my_set:
print(f)
print(len(my_set)) # Answer for front
new_list = []
for f in my_set:
new_list.append(f[::-1])
print('Reverse Result:')
for f in new_list:
print(f)
print(len(new_list)) # Answer for reverse
[尋找在python給定的字符串的所有可能的排列]的可能的複製(https://stackoverflow.com/questions/8306654/finding-all-possible-permutations-of -a-given-string-in-python) –