473,327 Members | 2,112 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

decimal to python using recursion

6
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 4319
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
wagn31
6
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
wagn31
6
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
wagn31
6
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
Particularly, reply #11.
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
I just realized that this is a double posted thread. Please follow site guidelines: double post
Jun 15 '07 #11
bartonc
6,596 Expert 4TB
I just realized that this is a double posted thread. Please follow site guidelines: double post
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

Sign in to post your reply or Sign up for a free account.

Similar topics

21
by: Batista, Facundo | last post by:
Here I send it. Suggestions and all kinds of recomendations are more than welcomed. If it all goes ok, it'll be a PEP when I finish writing/modifying the code. Thank you. .. Facundo
0
by: Kurt B. Kaiser | last post by:
Patch / Bug Summary ___________________ Patches : 349 open ( +7) / 3737 closed (+25) / 4086 total (+32) Bugs : 939 open (-12) / 6648 closed (+60) / 7587 total (+48) RFE : 249 open...
206
by: WaterWalk | last post by:
I've just read an article "Building Robust System" by Gerald Jay Sussman. The article is here: http://swiss.csail.mit.edu/classes/symbolic/spring07/readings/robust-systems.pdf In it there is a...
3
by: zgfareed | last post by:
My program converts decimal numbers from to binary and hexadecimal. I am having trouble with my output which is supposed to be in a certain format. Binary needs to be in the format of XXXX XXXX...
25
by: Lennart Benschop | last post by:
Python has had the Decimal data type for some time now. The Decimal data type is ideal for financial calculations. Using this data type would be more intuitive to computer novices than float as its...
6
by: Terry Reedy | last post by:
Gerhard Häring wrote: The new fractions module acts differently, which is to say, as most would want. True Traceback (most recent call last): File "<pyshell#20>", line 1, in <module> F(1.0)...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.