Python转化为JS的问题

Python转化为JS的问题

有这样一段Python代码:

map = {

'V0': ['V1', 'V2'],

'V1': ['V0', 'V3', 'V4'],

'V2': ['V0', 'V5', 'V6'],

'V3': ['V1', 'V7'],

'V4': ['V1', 'V5', 'V7'],

'V5': ['V2', 'V4', 'V6'],

'V6': ['V2', 'V5', 'V8'],

'V7': ['V3', 'V4'],

'V8': ['V6']

}

def traverse(start, end):

main_stack = [start]

side_stack = [map[start]]

while len(main_stack) > 0:

side_top = side_stack.pop()

if len(side_top) > 0:

main_top = side_top[0]

main_stack.append(main_top)

side_stack.append(side_top[1:])

if len(map[main_top]) > 0:

side_top = []

for v in map[main_top]:

if v not in main_stack:

side_top.append(v)

side_stack.append(side_top)

else:

main_stack.pop()

if len(main_stack) > 0 and main_stack[-1] == end:

print("==============FOUND==============")

print(main_stack)

pop_stack(main_stack, side_stack)

def pop_stack(main_stack, side_stack):

main_stack.pop()

if len(side_stack) > 0:

side_stack.pop()

if __name__ == '__main__':

traverse('V3', 'V6')

我尝试转换成javascript:

var map = {

'V0': ['V1', 'V2'],

'V1': ['V0', 'V3', 'V4'],

'V2': ['V0', 'V5', 'V6'],

'V3': ['V1', 'V7'],

'V4': ['V1', 'V5', 'V7'],

'V5': ['V2', 'V4', 'V6'],

'V6': ['V2', 'V5', 'V8'],

'V7': ['V3', 'V4'],

'V8': ['V6']

}

var main_stack

var side_stack

function traverse(start, end) {

main_stack = [start]

side_stack = [map[start]]

while (main_stack.length) {

var side_top = side_stack.pop()

if (side_top.length) {

var main_top = side_top[0]

main_stack.push(main_top)

side_stack.push(side_top.slice(1))

if (map[main_top].length) {

side_top = []

for (let v of map[main_top]) {

if (!map[main_top].includes(v)) {

side_top.push(v)

}

}

side_stack.push(side_top)

}

} else {

main_stack.pop()

}

if (main_stack.length && main_stack[main_stack.length - 1] === end) {

console.log(main_stack)

pop_stack(main_stack, side_stack)

}

}

}

function pop_stack(main_stack, side_stack) {

main_stack.pop()

if (side_stack.length)

side_stack.pop()

}

traverse('V3', 'V6')

可是并没有输出Python代码的运行结果,我对Python语法不是很熟悉,可以请教一下高人是哪里写错了吗?谢谢~


回答:

for (let v of map[main_top]) {

if (!map[main_top].includes(v)) {

side_top.push(v)

}

}

这句写错了吧,自相矛盾的样子

以上是 Python转化为JS的问题 的全部内容, 来源链接: utcz.com/a/159168.html

回到顶部