decimal to python using recursion

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • wagn31
    New Member
    • Jun 2007
    • 6

    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?
  • ilikepython
    Recognized Expert Contributor
    • Feb 2007
    • 844

    #2
    Originally posted by wagn31
    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?

    Comment

    • wagn31
      New Member
      • Jun 2007
      • 6

      #3
      yeah i know how to do that but i am new to python and am unsure how the coding goes

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        Originally posted by wagn31
        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:
        Code:
         ans = ''
            while num != 0:
                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:
        Code:
        ....num, rem = divmod(.................
            return function_name(............

        Comment

        • wagn31
          New Member
          • Jun 2007
          • 6

          #5
          [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!!!!
          Last edited by bartonc; Jun 15 '07, 06:26 PM. Reason: Added [CODE=python][CODE] tags.

          Comment

          • wagn31
            New Member
            • Jun 2007
            • 6

            #6
            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!!!!!
            Last edited by bartonc; Jun 15 '07, 04:57 AM. Reason: Added [CODE=python][CODE] tags.

            Comment

            • bartonc
              Recognized Expert Expert
              • Sep 2006
              • 6478

              #7
              I think that you'll find what you are looking for here

              Comment

              • bartonc
                Recognized Expert Expert
                • Sep 2006
                • 6478

                #8
                Originally posted by bartonc
                I think that you'll find what you are looking for here
                Particularly, reply #11.

                Comment

                • r035198x
                  MVP
                  • Sep 2006
                  • 13225

                  #9
                  Originally posted by wagn31
                  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!!!!!
                  First write down your algorithm for it. How would you do it manually?

                  Comment

                  • bvdet
                    Recognized Expert Specialist
                    • Oct 2006
                    • 2851

                    #10
                    Originally posted by wagn31
                    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 )

                    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.[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):
                    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

                    • bvdet
                      Recognized Expert Specialist
                      • Oct 2006
                      • 2851

                      #11
                      I just realized that this is a double posted thread. Please follow site guidelines: double post

                      Comment

                      • bartonc
                        Recognized Expert Expert
                        • Sep 2006
                        • 6478

                        #12
                        Originally posted by bvdet
                        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.

                        Comment

                        Working...