【发布时间】:2016-10-14 09:41:25
【问题描述】:
我正在创建两个函数,一个返回以 10 为基数的三进制表示,另一个使用递归返回三进制数的以 10 为基数的表示。例如 52 将返回 1221。现在,我已经完成了,但我不知道如何制作它。我对三元表示中的 2 的方面以及如何将其实现到代码中感到困惑。
def numToTernary(n):
'''Precondition: integer argument is non-negative.
Returns the string with the ternary representation of non-negative integer
n. If n is 0, the empty string is returned.'''
if n==0:
return ''
if n<3:
return str(n)
return numToTernary(n//3)+
【问题讨论】:
-
我不确定实际的问题是什么。你也可以在你的代码中检查
if n<0,而不是把它作为你的文档字符串的一部分