class LongestIncreasingSubsequence:
def getLIS(self, A, n):
# write code here
dp=[0 for i in range(n)]
dp[0]=1
max=0
print dp
for i in range(n):
now=0
if i!=0:
res=1
for j in range(i):
if A[i]>A[j]:
res=dp[j]
now=now +1
if now>=max:
max=now
dp[i]=res+1
else:
dp[i]=res
print dp
#return max(dp)
kk=LongestIncreasingSubsequence()
kk.getLIS([1,4,2,5,3],5)
其中dp 是一个以int类型为成员的list
而使用max()函数时却会报错
TypeError: 'int' object is not callable
是由什么原因导致的?
Your max function is assigned a value of 0 in the fifth line, and the max function is overwritten. Rename your variable so that it does not have the same name as the library function
The reason is as @rayleisure said, you use a variable of
max
作為參考到int
type here, resulting inNot only will the build-in function fail, but it will also cause:
This is because you made a call to an integer
max
,In short, the naming of variable must not be inconsistent with:
keywords
build-in functions
The name of the standard library or any package/module in use
Same name.
Off topic
The following are all off topic
Judging from your current code, it seems that you only need to write a function. Writing this class seems redundant (unless you are doing an online question bank?)
There is no need to pass in the length of the list. Asking about the length of the list in Python can be easily done with
len()
For the LIS problem, I simplified your code:
Only find the length of LIS:
The whole LIS is asking to come out:
Test:
Result:
Questions I answered: Python-QA