2016-09-23 60 views
0

该函数做我想做的事情,但是当它完成时,它只是坐在那里,而不是从我称之为的地方继续,我无法弄清楚为什么。该代码是:Python函数卡住了(没有错误),但我不明白为什么

x = 9 
y = 9 
n = 10 
ty = 1 
tx = 1 

while ty <= y: 
    while tx <= x: 
     vars()["p" + str(ty) + str(tx)] = 0 
     tx += 1 
    ty += 1 
    tx = 1 

tn = 1 
while tn <= n: 
    vars()["m" + str(tn)] = tn 
    tn += 1 

t = x * y 
tm = n 
def recursion(z): 
    global tm 
    if z < n: 
     for x in range(n): 
      recursion(z + 1) 
    else: 
     if tm > 0: 
      tv = "m" + str(tm) 
      otm = eval(tv) 
      while eval(tv) < t - n + tm: 
       vars()[tv] = eval(tv) + 1 
       print(tv + " = " + str(eval(tv))) 
      vars()[tv] = otm + 1 
      print(tv + " = " + str(eval(tv))) 
      if tm > 1: 
       vars()["m" + str(tm - 1)] = eval("m" + str(tm - 1)) + 1 
       print(str("m" + str(tm - 1) + " = " + str(eval("m" + str(tm -1))))) 
      tm -= 1 

recursion(1) 

print("done") 

我已经把在我希望它结束​​,但据我所知它不应该真正需要它的回报。

任何人都可以看到我做了什么导致它卡住?

感谢

+1

如果它卡住了,它不会做你想要的。每次递归都需要一个基本情况,它返回一个基本值,以及一个递归部分,其中实际函数调用是在return语句中进行的。你的功能不这样做。 (return recursion(z + 1)) –

+2

定义了哪个'n'? – dg99

+0

@ dg99:和't',和'm1'和'm2' ...这段代码很疯狂!它看起来像是一个POCC(Python混淆代码竞赛)的入口。 – rodrigo

回答

0

对于没有经历过变更历史记录的人们,请注意:这是基于对其他答案的评论。更新:更好的版本。

import itertools 

def permutations(on, total): 
    all_indices = range(total) 
    for indices in itertools.combinations(all_indices, on): 
     board = ['0'] * total 
     for index in indices: 
      board[index] = '1' 
     yield ''.join(board) 
+0

这比我想出的代码快得多,而且更简单易读。谢谢! –

0

我不能上班发生了什么事(原来如果我离开它了几分钟它实际上虽然完成),相反,我意识到,我并不需要使用递归来实现我想要的(并且我也意识到这个函数实际上并没有做我想做的事)。

任何有兴趣,我简化并重写了它是一个几while循环,而不是:

x = 9 
y = 9 
t = x * y 
n = 10 

tt = 1 
while tt <= t: 
     vars()["p" + str(tt)] = 0 
     tt += 1 

tn = 1 
while tn <= n: 
    vars()["m" + str(tn)] = tn 
    vars()["p" + str(tn)] = 1 
    tn += 1 

def cl(): 
    w = "" 
    tt = 1 
    while tt <= t: 
     w = w + str(eval("p" + str(tt))) 
     tt += 1 
    p.append(w) 

tm = n 
tv = "m" + str(tm) 
p = [] 
while m1 < t - n + tm - 1:  
    cl() 
    while tm == n and eval(tv) < t - n + tm: 
     vars()["p" + str(eval(tv))] = 0 
     vars()[tv] = eval(tv) + 1 
     vars()["p" + str(eval(tv))] = 1 
     cl()   
    tm -= 1 
    tv = "m" + str(tm) 
    while tm < n and tm > 0: 
     if eval(tv) < t - n + tm: 
      vars()["p" + str(eval(tv))] = 0 
      vars()[tv] = eval(tv) + 1 
      vars()["p" + str(eval(tv))] = 1 
      while tm < n: 
       tm += 1 
       ptv = tv 
       tv = "m" + str(tm) 
       vars()["p" + str(eval(tv))] = 0 
       vars()[tv] = eval(ptv) + 1 
       vars()["p" + str(eval(tv))] = 1 
     else: 
      tm -= 1 
      tv = "m" + str(tm) 
+0

出于好奇,这段代码应该做什么?当我尝试运行它时,它看起来像是一个无限循环(一旦我解决了名称错误)。 – mwchase

+0

我认为我发布的版本可能是老实说,我一直在为它奋斗了很多年。它应该会产生每一个可能的状态,一个x的网格可能会以n的数量为1,其余的为0.我后来意识到我只是将它输出为一行,因此大大简化了它,我会用它更新后现在你感兴趣了。 –

0

如果有人有兴趣的原代码做了什么,我重新安排了条件语句来修剪的函数调用树:

x = 9 
y = 9 
n = 10 
ty = 1 
tx = 1 

while ty <= y: 
    while tx <= x: 
     vars()["p" + str(ty) + str(tx)] = 0 
     tx += 1 
    ty += 1 
    tx = 1 

tn = 1 
while tn <= n: 
    vars()["m" + str(tn)] = tn 
    tn += 1 

t = x * y 
tm = n 
def recursion(z): 
    global tm 
    if tm > 0: 
     if z < n: 
      for x in range(n): 
       recursion(z + 1) 
     else: 
      tv = "m" + str(tm) 
      otm = eval(tv) 
      while eval(tv) < t - n + tm: 
       vars()[tv] = eval(tv) + 1 
       print(tv + " = " + str(eval(tv))) 
      vars()[tv] = otm + 1 
      print(tv + " = " + str(eval(tv))) 
      if tm > 1: 
       vars()["m" + str(tm - 1)] = eval("m" + str(tm - 1)) + 1 
       print(str("m" + str(tm - 1) + " = " + str(eval("m" + str(tm -1))))) 
      tm -= 1 

recursion(1) 

print("done") 

通过使用列表和范围对象可以使这更清晰,但这需要付出努力。

相关问题