How do I convert a decimal integer to a binary number using a recursive function? I am really stuck on this one. any help?
decimal to python using recursion
Collapse
X
-
Ok, first things first, do you know how to convert a decimal number to binary on paper and pencil?Originally posted by wagn31How do I convert a decimal integer to a binary number using a recursive function? I am really stuck on this one. any help? -
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:Originally posted by wagn31yeah i know how to do that but i am new to python and am unsure how the coding goesThis snippet is part of a function that returns the converted number as a string. String concatenation is used.Code:ans = '' while num != 0: num, rem = divmod(...........
The key operator is '%' (modulo).
Using recursion:
Code:....num, rem = divmod(................. return function_name(............Comment
-
[CODE=python]def print_binary1(d ecimal_string):
bStr = ''
while decimal_string > 0:
bStr = str(decimal_str ing % 2)
decimal_string = decimal_string >> 1
print bStr
return bStr
if decimal_string == 0:
print bStr
return bStr
print_binary1(d ecimal_string )
[/CODE]
this is what i have and i know i am not even close....pleas help!!!!Comment
-
decimal to python using recursion
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:
[CODE=python]
def print_binary1(d ecimal_string):
bStr = '0'
while decimal_string > 0:
bStr = str(decimal_str ing % 2)
decimal_string = decimal_string >> 1
print bStr
return bStr
if decimal_string == 0:
print bStr
return bStr
print_binary1( )
[/CODE]
this is what I have, i know I am wrong but cant figure it out....someone please help!!!!!Comment
-
First write down your algorithm for it. How would you do it manually?Originally posted by wagn31I 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:
[CODE=python]
def print_binary1(d ecimal_string):
bStr = '0'
while decimal_string > 0:
bStr = str(decimal_str ing % 2)
decimal_string = decimal_string >> 1
print bStr
return bStr
if decimal_string == 0:
print bStr
return bStr
print_binary1( )
[/CODE]
this is what I have, i know I am wrong but cant figure it out....someone please help!!!!!Comment
-
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.[code=Python]if decimal_string == 0: return ''[/code]You can build the result on a return statement:[code=Python]return print_binary1(d ecimal_string)+ bStr[/code]This should work for you:[code=Python]def print_binary1(d ecimal_string):Originally posted by wagn31def print_binary1(d ecimal_string):
bStr = ''
while decimal_string > 0:
bStr = str(decimal_str ing % 2)
decimal_string = decimal_string >> 1
print bStr
return bStr
if decimal_string == 0:
print bStr
return bStr
print_binary1(d ecimal_string )
this is what i have and i know i am not even close....pleas help!!!!
if decimal_string == 0:
return ''
............... ............... ........... # a line from your code
............... ............... ........... # a line from your code
return print_binary1(d ecimal_string)+ bStr[/code]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.Comment
-
Comment
-
Thanks BV. Sharp Eye. I've merged the threads.Originally posted by bvdetI just realized that this is a double posted thread. Please follow site guidelines: double post
wagn31:
[ CODE ] tags are another stipulation of the Posting Guidelines. Please read them and follow them.Comment
Comment