我需要一些幫助,也許是一些關于如何以遞回方式思考以下解決方案的指標。
該程式應該采用一個句子并列印我們可以強調該句子中單詞的所有不同方式。
像這樣的東西:
Input:
what are you doing
Output:
what are you doing?
what are you DOING?
what are YOU doing?
what are YOU DOING?
what ARE you doing?
what ARE you DOING?
what ARE YOU doing?
what ARE YOU DOING?
WHAT are you doing?
WHAT are you DOING?
WHAT are YOU doing?
WHAT are YOU DOING?
WHAT ARE you doing?
WHAT ARE you DOING?
WHAT ARE YOU doing?
WHAT ARE YOU DOING?
我的迭代實作:
from itertools import chain, combinations
inputStr = "what are you doing"
wordList = inputStr.split()
# getting all possible subsets which are all possible word combinations from inputStr
def powerset(iterable):
"powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)"
a = chain.from_iterable(combinations(iterable, r) for r in range(len(iterable) 1))
return a
# list of tuples of subsets
temp = list(powerset(wordList))
# function that converts tuple to string
def join_tuple_string(strings_tuple) -> str:
return ' '.join(strings_tuple)
# joining all the tuples into strings
result = map(join_tuple_string, temp)
listOfListsOfSubsets = list(result)
# getting list of lists of words that form all possible subsets of inputStr and converting them to uppercase
for i in range(0,len(listOfListsOfSubsets)):
listOfListsOfSubsets[i] = listOfListsOfSubsets[i].split()
# print(listOfListsOfSubsets)
# converting every word in in every possible subset of words from inputStr into upper-case
for i in range(0, len(listOfListsOfSubsets)):
listOfListsOfSubsets[i] = [x.upper() for x in listOfListsOfSubsets[i]]
# reconstructing each subset of words to the full sentence (matching inputStr) while being case sensitive so that what was converted
# previously to uppper case, stays upper-case
for li in listOfListsOfSubsets:
for word in wordList:
if word.upper() not in li:
i = wordList.index(word)
li.insert(i, word)
# printing all possible allEmphasesOf inputStr
print(" ".join(li))
uj5u.com熱心網友回復:
每當我需要遞回解決問題時,我都會想到兩個主要問題:
- 給定輸入,我如何通過找到相同問題的更簡單版本的解決方案來解決問題?
- 這個問題最簡單的版本是什么?
特別是針對您的問題,我將首先查看可能的輸出范圍。請注意,輸入的前半部分和后半部分幾乎相同,除了第一個單詞:
[what/WHAT] are you doing?
[what/WHAT] are you DOING?
[what/WHAT] are YOU doing?
[what/WHAT] are YOU DOING?
[what/WHAT] ARE you doing?
[what/WHAT] ARE you DOING?
[what/WHAT] ARE YOU doing?
[what/WHAT] ARE YOU DOING?
因此,如果我們能找到一種方法來生成最后 3 個單詞的所有可能變體的串列,那么我們可以簡單地制作該串列的兩個副本,并what添加到第一個串列WHAT的每個元素和第二個串列的每個元素.
那么我們如何解決這個子問題呢?請注意,輸出再次重復:
[are/ARE] you doing?
[are/ARE] you DOING?
[are/ARE] YOU doing?
[are/ARE] YOU DOING?
...等等。
至于最簡單的程式版本,那就是只有零個或一個單詞的時候。那里的輸出很明顯。
把這一切放在一起:
- 我們需要撰寫一個函式來生成所有可能的大小寫變化的串列
- 在簡單的情況下,有零個或一個單詞,您可以對解決方案進行硬編碼
- 在所有其他情況下,我們在除第一個單詞之外的所有單詞上呼叫我們的函式,然后制作兩個副本,并將兩個不同的大寫字母添加到每個單詞上。
這對你思考問題有幫助嗎?
uj5u.com熱心網友回復:
基本情況是,如果只有一個單詞,則按原樣回傳,并添加一個大寫版本。
否則,為剩余的單詞生成所有選項。對于每個選項,在開頭添加單詞或大寫單詞。
def dfs(words):
if not words:
return []
if len(words)==1:
return [[words[0]], [words[0].capitalize()]]
else:
ans = []
options = dfs(words[1:])
for option in options:
ans.append([words[0]] option)
ans.append([words[0].capitalize()] option)
return ans
s = "what are you doing"
print(dfs(s.split()))
uj5u.com熱心網友回復:
所以,表達這個問題的另一種方式是:找到給定集合的冪集。
我看到你已經為此撰寫了一個函式(我從你的問題中復制了)。我猜你是從itertools食譜頁面得到這個代碼的。
# getting all possible subsets which are all possible word combinations from inputStr
def powerset(iterable):
"powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)"
a = chain.from_iterable(combinations(iterable, r) for r in range(len(iterable) 1))
return a
我們現在需要做的是使用遞回重新撰寫這個函式。為此,這是我的方法:
def get_powerset(lst):
if not lst:
return [[]]
with_first = [[lst[0]] rest for rest in get_powerset(lst[1:])]
without_first = get_powerset(lst[1:])
return with_first without_first
input_string = 'what are you doing'
input_length = len(input_string.split(' '))
powerset = get_powerset(range(input_length))
print(powerset)
運行該函式后,您將獲得所有子集的串列。只需遍歷它們并將相應索引處的單詞大寫即可。
uj5u.com熱心網友回復:
我的觀察是,如果您計算組合的數量并將每個組合表示為二進制數,那么表示為字串的二進制數可用于指示哪些單詞應該是大寫單詞:
def to_upper(sentence, i = 0):
upper = f"{i:0{len(sentence)}b}"
to_print = []
for s in range(len(sentence)):
if upper[s] == '1': to_print.append(sentence[s].upper())
else: to_print.append(sentence[s])
print(' '.join(to_print))
if i < 2**len(sentence)-1:
to_upper(sentence, i = i 1)
to_upper('what are you doing'.split(''))
uj5u.com熱心網友回復:
我看到你找到了 module itertools。它提供了許多有用的功能來組合可能性。
在這里,每個單詞都有兩種可能性:大寫版本和非大寫版本。您可以將所有可能性與itertools.product.
from itertools import product
def all_emphases(s):
for c in product(*((w, w.upper()) for w in s.split())):
yield ' '.join(c)
for c in all_emphases('what are you doing?'):
print(c)
輸出:
what are you doing?
what are you DOING?
what are YOU doing?
what are YOU DOING?
what ARE you doing?
what ARE you DOING?
what ARE YOU doing?
what ARE YOU DOING?
WHAT are you doing?
WHAT are you DOING?
WHAT are YOU doing?
WHAT are YOU DOING?
WHAT ARE you doing?
WHAT ARE you DOING?
WHAT ARE YOU doing?
WHAT ARE YOU DOING?
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/442116.html
上一篇:遞回列印倒三角形
