-1

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.

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)+
Efferalgan
  • 1,681
  • 1
  • 14
  • 24
Phil Cho
  • 131
  • 3
  • 14
  • I'm not sure what the actual question is. Also you can just have a check `if n<0` in your code instead of having it as part of your docstring – UnholySheep Oct 14 '16 at 09:48

2 Answers2

0

You were nearly there with your code. This should do the trick, according to this question.

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.

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<3:
        return str(n)
    return numToTernary(n//3)+str(n%3)
Community
  • 1
  • 1
Efferalgan
  • 1,681
  • 1
  • 14
  • 24
0

So the big idea with all base change is the following:

You take a number n written in base b as this 123. That means n in base 10 is equal to 1*b² + 2*b + 3 . So convertion from base b to base 10 is straigtforward: you take all digits and multiply them by the base at the right power.

Now the for the reverse operation: you have a number n in base 10 and want to turn it in base b. The operation is simply a matter of calculating each digit in the new base. (I'll assume my result has only three digits for the following example) So I am looking for d2,d1,d0 the digits in base b of n. I know that d2*b² + d1*b + d0 = n. That means that (d2*b + d1)*b + d0 = n so we recognize the result of an euclidian division where d0 is the remainder of the euclidian division of n by d : d0=n%d. We have identified d0 as the remainder so the expression in parentheses is the quotien q, q=n//b so we have a new equation to solve using the exact same method (hence the recursion) d2*b + d1 = q.

And all that translate to the code you almost had :

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)+str(n%3)

print(numToTernary(10))
Out[1]: '101'
jadsq
  • 3,033
  • 3
  • 20
  • 32