简体   繁体   English

Python,递归:给出满足布尔表达式的所有可能的元组组合

[英]Python, recursion : give all possible tuple combinations who meet a boolean expression

So, for school, I got an exercise on recursion, which goes as follows: I'm given a string and a random int value 'N'.所以,在学校,我做了一个关于递归的练习,如下所示:我得到了一个字符串和一个随机的 int 值“N”。 The string is a boolean expression, for example '3*x - 2* y <0' .该字符串是一个布尔表达式,例如 '3*x - 2* y <0' 。 The result has to be a list of tuples(x, y), (-N < x < N and -N < y < N), from all the possible tuple combinations who meet the expression.结果必须是满足表达式的所有可能的元组组合中的元组(x, y) (-N < x < N and -N < y < N) 的列表。 I did this exercise first with for loops etc and that was not so difficult, but then I had to do it recursively, and here's where I got stuck: As you can see, I just add up x and y by '1' at the end of the code, which will give me all tuple combinations where X and Y are the same.我首先用 for 循环等做了这个练习,这并不难,但后来我不得不递归地做,这就是我卡住的地方:如你所见,我只是在 x 和 y 上加了“1”代码的结尾,它将为我提供 X 和 Y 相同的所有元组组合。 For example, if N = 5, my code only evaluates the combinations (-4,-4), (-3,-3) ... (4,4) but not (-2,1) or (1,3) for example.例如,如果 N = 5,我的代码只计算组合 (-4,-4)、(-3,-3) ... (4,4) 而不是 (-2,1) 或 (1,3 ) 例如。 So my question is: can anyone help me writing a recursive code which evaluates the boolean expression in all the possible tuple combinations?所以我的问题是:谁能帮我写一个递归代码来评估所有可能的元组组合中的布尔表达式? My code has to be written recursively and I can't use functions as 'itertools' etc, it's not allowed in our school.我的代码必须递归编写,我不能将函数用作“itertools”等,我们学校不允许这样做。

**MY CODE:**
def solution(expression, N,x=None,y=None):
  if x is None: x = -N + 1
  if y is None: y = -N + 1
  res = []
  if x >= N and y >= N:
      return []

  if eval(expression) == True:
      res.append((x, y))

  return res + solution(expression, N, x+1, y+1)






I have modified your code and I think it works now:我已经修改了你的代码,我认为它现在可以工作了:

UPDATE: I have corrected this expression if x < N - 1 : to that one if x < N - 1 or y < N - 1:更新: if x < N - 1 :我已经更正了这个表达式if x < N - 1 : if x < N - 1 or y < N - 1:到那个表达式if x < N - 1 or y < N - 1:

def solution(expression, N, x=None, y=None):
  if x is None: x = -N + 1
  if y is None: y = -N + 1
  res = []

  if eval(expression) == True:
    res.append((x, y))

  # if x < N - 1 :
  if x < N - 1 or y < N - 1:
    if y < N - 1:
      y += 1
    else:
      x += 1
      y = - N + 1
    return res + solution(expression, N, x, y)
  else:
    return res

print(solution('3*x - 2* y <0', 4))

Slightly different approach building on getting permutations for a list and in the end checking the expression.以获取列表的排列并最终检查表达式为基础的略有不同的方法。 Not the prettiest code but does the job.不是最漂亮的代码,但可以完成工作。

results = []


def check(expression, items):
    x = y = None

    if len(items) == 1:
        x = y = items[0]
        if eval(expression) and (x, y) not in results:
            results.append((x, y))

    if len(items) == 2:
        x = items[0]
        y = items[1]
        if eval(expression) and (x, y) not in results:
            results.append((x, y))
        x = items[1]
        y = items[0]
        if eval(expression) and (x, y) not in results:
            results.append((x, y))

    if len(items) > 2:
        for i in items:
            remaining_elements = [x for x in items if x != i]
            check(expression, remaining_elements)


expression = "3*x - 2*y < 0"
N = 4
items = range(-N + 1, N)
check(expression, items)
print(results)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM