直覺上我覺得我有一個可以用遞回解決的問題,但是,在盯著這個看了幾天之后,我找不到第一個執行緒來拉動并開始解決它的程序。
我希望你能提供任何幫助來解決這個問題,并分解它來解決它和解決它自己。我目前正在嘗試用 javascript 解決這個問題。
給定一個像這樣的物件串列:
[
{position: 1, jump: 1, letter: "b"},
{position: 1, jump: 2, letter: "b"},
{position: 1, jump: 1, letter: "c"},
{position: 2, jump: 2, letter: "e"},
{position: 2, jump: 1, letter: "e"},
{position: 3, jump: 1, letter: "a"},
{position: 4, jump: 1, letter: "t"},
{position: 4, jump: 1, letter: "d"},
]
我想生成一個可能的字串串列,如下所示:
bet
bed
beat
bead
bat
bad
cet
ced
ceat
cead
我要遞回?遍歷串列并在每個物件處獲取字母,然后從該位置跳轉由跳轉值指示的空格數,獲取該字母并將其添加到字串中,以便通過物件串列的每個可能路徑。
因此,對于第一個字串,我們將從第一個物件 {position: 1, jump: 1, letter "b"} 開始,我會抓住字母 "b" 并從位置 1 跳到位置 2,因為指示的跳轉只是1. 然后我會抓住在那個新位置指示的第一個物件的字母并跳躍直到我完成串列。但是在我完成第一個單詞后,我會回傳未完成的位置,因此在這種情況下,在我成功創建“下注”后,我將回傳位置 4 以抓住另一種可能性,然后我將回傳位置 2 并開始作業關于下一個物件允許的其他可能性。最終,我會創建顯示的串列。
我可以完成串列并毫無問題地找到單詞,但我似乎無法找到一種方法來管理在任何一個位置查看多個選項的必要性。那么對于前兩個字串“bet”和“bed”的情況,當我到達串列的末尾時,捕獲一個位置上所有物件的串列是否有用?也許最好通過將每個物件根據它們的位置分成串列來開始整個程序??,然后遍歷每個串列并根據正確跳轉的標準對其進行評估?
任何幫助,將不勝感激。
編輯:
我不知道如何進一步簡化這一點,但也許不同的第二種解釋會有所幫助?
I have a list of objects with three properties. The first property describes a position numerically (position), the second property describes a step to take to the next position numerically (jump) and the third property is a single character string (letter). I want to run through this list as many times as it takes to generate all possible multi-character strings based on their position and jumps.
So to generate the first string I would iterate through the list hit the first object {position: 1, jump: 1, letter: "b"} and pull out the letter "b". It lets me know I am at position 1 and that the next step is one jump away so position 1 plus jump 1 equals position 2. I go to the first object described as position 2 in this case it is the fourth object {position: 2, jump: 2, letter: "e"} I add the "e" to the "b" making "be" and note that the next jump is two, so currently at position 2 I jump 2 to position 4. At the first object described as position 4 the 7th object listed in my example {position: 4, jump: 1, letter: "t"} I grab the "t" and add it to "be" to get "bet" and then the next jump goes off the end of the list so this entire iteration is complete.
I then want to repeat this process for every described combination in the list of objects.
uj5u.com熱心網友回復:
一個簡單的遞回版本可能如下所示:
const words = (input, position = 1, matches = input .filter (x => x .position == position)) =>
matches .length
? matches .flatMap (
({jump, letter}) => words (input, position jump) .map (p => letter p)
)
: ['']
const data = [{position: 1, jump: 1, letter: "b"}, {position: 1, jump: 2, letter: "b"}, {position: 1, jump: 1, letter: "c"}, {position: 2, jump: 2, letter: "e"}, {position: 2, jump: 1, letter: "e"}, {position: 3, jump: 1, letter: "a"}, {position: 4, jump: 1, letter: "t"}, {position: 4, jump: 1, letter: "d"}]
console .log (words (data))
.as-console-wrapper {max-height: 100% !important; top: 0}
這是通過首先找到與我們的位置匹配的所有值來作業的。如果沒有,我們回傳一個只包含空字串的陣列。如果不是,那么對于每一個,我們添加position和jump并重復,將其結果與當前字母組合。
如果由于必須搜索匹配而導致效率低下,那么我們可以將輸入資料重構為如下所示:
{
1: [{jump: 1, letter: "b"}, {jump: 2, letter: "b"}, {jump: 1, letter: "c"}],
2: [{jump: 2, letter: "e"}, {jump: 1, letter: "e"}],
3: [{jump: 1, letter: "a"}],
4: [{jump: 1, letter: "t"}, {jump: 1, letter: "d"}]
}
此時找到下一個位置只是一個查找。這個變體會做到這一點:
const restructure = (data) => data .reduce (
(a, {position, jump, letter}) =>
((a [position] = a [position] || []), (a [position] .push({jump, letter})), a),
{}
)
const words = (input, position = 1) =>
position in input
? input [position] .flatMap (
({jump, letter}) => words (input, position jump) .map (p => letter p)
)
: ['']
const data = [{position: 1, jump: 1, letter: "b"}, {position: 1, jump: 2, letter: "b"}, {position: 1, jump: 1, letter: "c"}, {position: 2, jump: 2, letter: "e"}, {position: 2, jump: 1, letter: "e"}, {position: 3, jump: 1, letter: "a"}, {position: 4, jump: 1, letter: "t"}, {position: 4, jump: 1, letter: "d"}]
console .log (words (restructure (data)))
.as-console-wrapper {max-height: 100% !important; top: 0}
uj5u.com熱心網友回復:
您可以使用遞回生成器函式:
var positions = [
{position: 1, jump: 1, letter: "b"},
{position: 1, jump: 2, letter: "b"},
{position: 1, jump: 1, letter: "c"},
{position: 2, jump: 2, letter: "e"},
{position: 2, jump: 1, letter: "e"},
{position: 3, jump: 1, letter: "a"},
{position: 4, jump: 1, letter: "t"},
{position: 4, jump: 1, letter: "d"},
]
function* build(p, s){
var c = positions.filter(x => x.position === p);
if (c.length === 0){
yield s
}
else{
for (var i of c){
yield* build(i.position i.jump, s i.letter)
}
}
}
console.log(Array.from(build(1, '')))
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/359895.html
上一篇:如何遞回訪問物件值?
