472,991 Members | 2,917 Online

# decimal to python using recursion

How do I convert a decimal integer to a binary number using a recursive function? I am really stuck on this one. any help?
Jun 13 '07 #1
11 4304
ilikepython
844 Expert 512MB
How do I convert a decimal integer to a binary number using a recursive function? I am really stuck on this one. any help?
Ok, first things first, do you know how to convert a decimal number to binary on paper and pencil?
Jun 13 '07 #2
yeah i know how to do that but i am new to python and am unsure how the coding goes
Jun 13 '07 #3
bvdet
2,851 Expert Mod 2GB
yeah i know how to do that but i am new to python and am unsure how the coding goes
We can help you with your code, but you need to show us some effort to solve the problem yourself. Is recursion required? This code snippet does not use recursion, but may give you a start:
Expand|Select|Wrap|Line Numbers
1.  ans = ''
2.     while num != 0:
3.         num, rem = divmod(...........
This snippet is part of a function that returns the converted number as a string. String concatenation is used.

The key operator is '%' (modulo).

Using recursion:
Expand|Select|Wrap|Line Numbers
1. ....num, rem = divmod(.................
2.     return function_name(............
Jun 13 '07 #4
Expand|Select|Wrap|Line Numbers
1. def print_binary1(decimal_string):
2.     bStr = ''
3.     while decimal_string > 0:
4.         bStr = str(decimal_string % 2)
5.         decimal_string = decimal_string >> 1
6.         print bStr
7.         return bStr
8.     if decimal_string == 0:
9.         print bStr
10.         return bStr
11.     print_binary1(decimal_string )
12.
this is what i have and i know i am not even close....pleas help!!!!
Jun 15 '07 #5
I need to write a code that take a decimal string and turns it into a binary number and i am new at python and have no clue how to do this:
Expand|Select|Wrap|Line Numbers
1. def print_binary1(decimal_string):
2.     bStr = '0'
3.     while decimal_string > 0:
4.         bStr = str(decimal_string % 2)
5.         decimal_string = decimal_string >> 1
6.         print bStr
7.         return bStr
8.     if decimal_string == 0:
9.         print bStr
10.         return bStr
11.     print_binary1( )
12.
this is what I have, i know I am wrong but cant figure it out....someone please help!!!!!
Jun 15 '07 #6
bartonc
6,596 Expert 4TB
I think that you'll find what you are looking for here
Jun 15 '07 #7
bartonc
6,596 Expert 4TB
I think that you'll find what you are looking for here
Jun 15 '07 #8
r035198x
13,262 8TB
I need to write a code that take a decimal string and turns it into a binary number and i am new at python and have no clue how to do this:
Expand|Select|Wrap|Line Numbers
1. def print_binary1(decimal_string):
2.     bStr = '0'
3.     while decimal_string > 0:
4.         bStr = str(decimal_string % 2)
5.         decimal_string = decimal_string >> 1
6.         print bStr
7.         return bStr
8.     if decimal_string == 0:
9.         print bStr
10.         return bStr
11.     print_binary1( )
12.
this is what I have, i know I am wrong but cant figure it out....someone please help!!!!!
First write down your algorithm for it. How would you do it manually?
Jun 15 '07 #9
bvdet
2,851 Expert Mod 2GB
def print_binary1(decimal_string):
bStr = ''
while decimal_string > 0:
bStr = str(decimal_string % 2)
decimal_string = decimal_string >> 1
print bStr
return bStr
if decimal_string == 0:
print bStr
return bStr
print_binary1(decimal_string )

this is what i have and i know i am not even close....pleas help!!!!
You are not that far off. Since you are using recursion, you will not need a while loop nor should you assign 'bStr' to ''. You WILL need a way to end the recursion.
Expand|Select|Wrap|Line Numbers
1. if decimal_string == 0: return ''
You can build the result on a return statement:
Expand|Select|Wrap|Line Numbers
1. return print_binary1(decimal_string)+bStr
This should work for you:
Expand|Select|Wrap|Line Numbers
1. def print_binary1(decimal_string):
2.     if decimal_string == 0:
3.         return ''
4.     ......................................... # a line from your code
5.     ......................................... # a line from your code
6.     return print_binary1(decimal_string)+bStr
I do not like your choice of name for decimal_string. It implies that the variable is a string, but it must be an integer for this code to work.

You need to place code tags around your code when you post. This is an open code tag: [code=Python]
This is a close code tag: [/ c o d e ]
I put spaces in the close tag so it would display.
Jun 15 '07 #10
bvdet
2,851 Expert Mod 2GB
Jun 15 '07 #11
bartonc
6,596 Expert 4TB
Thanks BV. Sharp Eye. I've merged the threads.

wagn31:
[ CODE ] tags are another stipulation of the Posting Guidelines. Please read them and follow them.
Jun 15 '07 #12