educative.io

I have used recursion to solve this problem but it throws error every time can someone tell what is wrong with the code

from stack import stack
s=stack()

def convert_int_to_bin(dec_num):
if dec_num <1:
q=’’
while not s.is_empty():
q+=str(s.pop())

return q

else:
w=dec_num%2
s.push(w)
a=int(dec_num/2)
return convert_int_to_bin(a)

Hi @vvs_rohith,

I tried the following code provided by you but it worked perfectly fine. Make sure your indentation is correct.

from stack import Stack

s = Stack()
def convert_int_to_bin(dec_num):
    if dec_num < 1:
        q = ''
        while not s.is_empty():
          q+=str(s.pop())

        return q

    else:
        w=dec_num%2
        s.push(w)
        a=int(dec_num/2)
        return convert_int_to_bin(a)