-1

Here is my code so far:

import string  
def convert(num,base):
    if num==0:
        return 
    else:
        remainder = num%base
        num = num//base
        b=(str(remainder))
        print(b[::-1],end="")
        return convert(num,base)

Instead of printing this:

>>> convert(29,3)
2001

I need to print it backward like this (and it needs to be done by strings):

>>> convert(29,3)
1002

Seems like string does not work well with recursion:

>>> convert(29,3)
 ('2', ('0', ('0', ('1', None))))
Sardorbek Imomaliev
  • 14,861
  • 2
  • 51
  • 63
Mark
  • 11
  • 3

3 Answers3

3

Just change the order in which you print and recurse:

import string  
def convert(num,base):
    if num==0:
        return 
    remainder = num % base
    num = num // base
    convert(num, base)
    print(remainder, end="")

>>> convert(29,3)
1002

No idea what convertBase() is.

AChampion
  • 29,683
  • 4
  • 59
  • 75
  • A +1 for you since the real problem was simply the order in which the recursive frames were evaluated. – sberry Oct 26 '16 at 04:30
1

Throwing in my $0.02. I would opt for returning the entire string, instead of printing it as you go.

def convert(num, base):
    if num == 0:
        return ''
    num, remainder = divmod(num, base)
    return convert(num, base) + str(remainder)

EDIT might as well use the built-in divmod for computing the num and remainder.

sberry
  • 128,281
  • 18
  • 138
  • 165
0

Your recursive function is correct in the sense that it has a exit clause (if num == 0:), however due to the nature of recursive functions the order of execution will be the reverse of what you wish to print out. What needs to be done here is to keep adding the remainders to a list and then finally when you hit the exit clause, reverse that list and print it.

import string  
def convert(num, base, rem_list=None):
    if not rem_list:
        rem_list = []
    if num == 0:
        rem_list.reverse()
        print "".join(rem_list) 
    else:
        remainder = num % base
        num = num // base
        rem_list.append(str(remainder))
        return convert(num, base, rem_list)



>>> convert(29, 3)
1002
>>> convert(29, 3)
1002
RFV
  • 831
  • 8
  • 22
  • And if you call this function twice what happens - you really should look at the consequences of having mutable default types. – AChampion Oct 26 '16 at 04:10
  • If you call `convert(29, 3); convert(29, 3)` the second time you get `10022001`. – AChampion Oct 26 '16 at 04:14
  • Why does that happen since we are calling `convert(num, base, rem_list=[])` with a default `rem_list=[]` ? – RFV Oct 26 '16 at 04:14
  • 1
    Please check out: http://stackoverflow.com/questions/1132941/least-astonishment-and-the-mutable-default-argument. And your solution really doesn't reflect a recursive solution which uses the stack, but more forcing an iterative solution into recursion. – AChampion Oct 26 '16 at 04:16
  • As an ugly hack I added more code. Your answer is better and I leaned something new today. – RFV Oct 26 '16 at 04:28
  • The more common approach is, `convert(x, y, rem_list=None, ...): if rem_list is None: rem_list = []`. This could be a very good way to do it in another language that does tail-call optimization, but Python does not so there is no gain. – sberry Oct 26 '16 at 04:29