I m 试图用电梯进行电解,而电解器是一种非功能的类别,但I m 正在发现错误
TypeError: seqLength() takes exactly 2 arguments (1 given)
我猜想这一点与课堂有某种关系,但并不肯定会有什么错误。
The code:
import sys
class memoize(object):
memoize decorator
def __init__(self, func):
self.func = func
self.cache = {}
def __call__(self, *args):
try:
return self.cache[args]
except KeyError:
value = self.func(self, *args)
self.cache[args] = value
return value
class collatz(object):
def __init__(self, n):
self.max = 1
self.n = n
@memoize
def seqLength(self, n):
if n>1:
if n%2 == 0:
return 1+self.seqLength(n/2)
else:
return 1+self.seqLength(3*n+1)
else:
return 1
def maxLength(self):
for n in xrange(1, self.n):
l = self.seqLength(n)
if l > self.max:
self.max = n
return self.max
n = int(sys.argv[1])
c = collatz(n)
print c.maxLength()