hi各位大佬好,我是最帅的小明哥,今天依旧是搬砖的晚上,想到今天的题目还没搞定,因而记录下来,
For Recommendation in Deep learning QQ Group 277356808
For deep learning QQ Second Group 629530787
I'm here waiting for you
从网上搜索到一个简版,如下:(修改语言后)
def ten2eight(num): if num==0: return num else: j=num%8 i=num//8 ten2eight(i) print(j)print(ten2eight(32))print(ten2eight(100))
直接执行上述脚本可见正确答案,但比较Low,还有None出现
40None144None
1-去掉None
后来发现去不掉,因为执行时不满足条件相当于返回None,举例子如下:
>>> def func(num):..、 pass..、>>> print(func(8))None
所以下面仍旧有None打印出来
def ten2eight(num): if num: j=num%8 i=num//8 ten2eight(i) print(j)print(ten2eight(36))print(ten2eight(100))
留作坑。
2-class中函数不能用递归??
class Solution: def __init__(self): self.ans='' def ten2eight(self,num): if num: j=num%8 i=num//8 ten2eight(i)#这么做不对 self.ans+=str(j)sol=Solution()sol.ten2eight(32)print('result',sol.ans)$ python3 mycode.py 4result 0
更正后,可以正确执行了,如下:
class Solution: def __init__(self): self.ans='' def ten2eight(self,num): if num: j=num%8 i=num//8 self.ten2eight(i) self.ans+=str(j)sol=Solution()sol.ten2eight(302)print('result',sol.ans)$ python3 mycode.py result 456
3-class另一种形式
class Solution: def ten2eight(self,num): self.ans='' def func(num): if num: j=num%8 i=num//8 func(i) self.ans+=str(j) func(num) return self.anssol=Solution()res=sol.ten2eight(302)print("result2",res)result2 456
那么受上面启发,上面的坑可填??还是不行哦,还是class中的参数可以。
def ten2eight(num): ans='' def func(num): if num: j=num%8 i=num//8 func(i) ans+=str(j) func(num) return ansres2=ten2eight(698)print("results",res2)Traceback (most recent call last): File "mycode.py", line 103, in