472,991 Members | 2,917 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,991 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 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
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)...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.