简体   繁体   English

python类中的最大递归深度超出项目Euler#4

[英]maximum recursion depth exceeded in python class, project Euler #4

I was working on a solution for Project Euler's Question #4: 我正在为欧拉计划的问题#4寻求解决方案:

"Find the largest palindrome made from the product of two 3-digit numbers." “找出由两个3位数字的乘积组成的最大回文。”

I could just write a basic script and loop, but I tend to write things within classes. 我可以编写一个基本的脚本和循环,但是我倾向于在类中编写东西。

I've been out of python for a while, so I'm using these exercises to stay familiar with the language. 我已经离开python一段时间了,所以我正在使用这些练习来熟悉该语言。

While looping through the factors to figure out the answer, I receive this error: 在遍历各种因素找出答案时,我收到此错误:

File "p4.py", line 35, in is_palindrome
n = str(p)
RuntimeError: maximum recursion depth exceeded while getting the str of an object 

I'm guessing it's the way I formatted my recursive method, but I can't figure out how to fix it. 我猜想这是我格式化递归方法的方式,但是我不知道如何解决它。

Could someone explain to me what I'm doing wrong in terms of structuring my recursive method? 有人可以向我解释在构造递归方法方面我做错了什么吗?

The code: 编码:

import math

class PalindromeCalculator:

  def __init__(self, min_factor=100, max_factor=999):
    self.stable_factor = max_factor
    self.variable_factor = max_factor

  def find_max_palindrome(self):
    return self.check_next_product()

  def check_next_product(self):
    product = self.stable_factor * self.variable_factor;
    if self.is_palindrome(product):
      print("We found a palindrome! %s" % product)
      return str(product)
    else:
      # Reduce one of the factors by 1
      if self.variable_factor == 100:
        self.variable_factor = 999
        self.stable_factor -= 1
      else:
        self.variable_factor -= 1

      self.check_next_product()

  def is_palindrome(self, p):
    # To check palindrom, pop and shift numbers off each side and check if  they're equal
    n = str(p)
    length = len(n)

    if length % 2 == 0:
      iterations = length / 2
    else:
      iterations = (length - 1) / 2

    for i in range(0, iterations):
      first_char = n[i:i+1]
      last_char = n[-(i+1)]

      if first_char != last_char:
        return False

    return True

And to run the function: 并运行该功能:

start = time.time()
calculator = PalindromeCalculator();
M = calculator.find_max_palindrome()
elapsed = (time.time() - start)

print "My way: %s found in %s seconds" % (M, elapsed)

This is similar to a StackOverflowError in Java. 这类似于Java中的StackOverflowError Because check_next_product is calling itself so much there are too many nested function calls and Python has given up on keeping track of them all. 由于check_next_product调用自身太多,因此嵌套函数调用过多,Python放弃了对所有这些函数的跟踪。 You can increase the recursion limit but the fact that the recursion is so deep shows that you'd be much better off writing an iterative solution. 您可以增加递归限制,但是递归如此之深的事实表明,编写迭代解决方案要好得多。 Recursion isn't really suited to this problem. 递归并不真正适合此问题。

Check this: maximum recursion depth exceeded while calling a Python object 检查以下内容: 调用Python对象时超出了最大递归深度

anyway, it is quite simple to write an iterative algorithm for it so there is no need to use the recursion. 无论如何,为它编写一个迭代算法非常简单,因此不需要使用递归。

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

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