Python Ternary Rescursion
<p>I'm creating two functions one, that returns the ternary representation for a base 10 number, and one that returns the base 10 representation for a ternary number using recursion. For example 52 would return 1221. Right now, I have this down, but I'm not sure how to make it. I'm mostly confused with the aspect of the 2 in ternary representation and how to implement that into code.</p>

<pre><code>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&lt;3:
        return str(n)
    return numToTernary(n//3)+
</code></pre>

<p>You were nearly there with your code. This should do the trick, according to <a href="http://stackoverflow.com/questions/2088201/integer-to-base-x-system-using-recursion-in-python">this question</a>.</p>

<p>However, you will have to do the search for "0" outside this function: as it was done in your code, the "0" digits were not skipped in the output, and a number that should have output "120011" would have instead output "1211" for example.</p>

<pre><code>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&lt;3:
        return str(n)
    return numToTernary(n//3)+str(n%3)
</code></pre>

python recursion ternary