binary to decimal using recursion,python

user4127524

Need help converting binary to decimal, using recursion.

So far I have :

(2* int(s[0])) + int(s[1])

with base cases for when s==0 and s==1.

I'm not sure how to pass this recursively so that the function will pass through all 1's and 0's in input,s.

royhowie

The basic idea is to pick off the last character of the string and convert that to a number, then multiply it by the appropriate power of 2. I've commented the code for you.

# we need to keep track of the current string,
# the power of two, and the total (decimal)
def placeToInt (str, pow, total):
    # if the length of the string is one,
    # we won't call the function anymore
    if (len(str) == 1):
        # return the number, 0 or 1, in the string
        # times 2 raised to the current power,
        # plus the already accumulated total
        return int(str) * (2 ** pow) + total
    else:
        # grab the last digit, a 0 or 1
        num = int(str[-1:])
        # the representation in binary is 2 raised to the given power,
        # times the number (0 or 1)
        # add this to the total
        total += (num * (2 ** pow))
        # return, since the string has more digits
        return placeToInt(str[:-1], pow + 1, total)

# test case
# appropriately returns 21
print(placeToInt("10101", 0, 0))

Now, let's go through it manually, so you understand why this works.

# n = 101 (in binary
# this can also be represented as 1*(2^2) + 0*(2^1) + 1*(2^0)
# alternatively, since there are three digits in this binary number
# 1*(2^(n-1)) + 0*(2^(n-2)) + 1*(2^(n-3))

So what does this mean? Well, the rightmost digit is 1 or 0 times 2 raised to the power of zero. In other words, it either adds 1 or 0 to the total. What about the second rightmost digit? It either adds 0 or 2 to the total. The next one? 0 or 4. See the pattern?

Let's write the pseudocode:

let n = input, in binary
total = 0
power of 2 = 0
while n has a length:
    lastDigit = last digit of n
    add (2^pow)*lastDigit to the current total

Since we start with a power and a total of 0, you can see why this works.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Binary to decimal using recursion

Binary to Decimal using recursion in C

Decimal to Binary conversion using recursion

Converting a Binary Integer to a Decimal Integer using Recursion

Recursion Binary to Decimal

Binary to Decimal in Haskell without using recursion or list comprehensions

Converting String of binary digits to decimal number... using recursion

any program Decimal to Binary by recursion

Decimal to binary with visualisation in python

Decimal to binary python algorithm

Decimal to Binary function, Python

Binary to Decimal conversion in Python

Binary Tree Traversal Using Recursion Explanation (python)

how to convert decimal to binary by using repeated division in python

How to convert a decimal number into a binary number using STACK in python

Program that calculates binary gap using recursion in python generates recursion error

Decimal to binary using Bitwise operator

binary to decimal using CharAt( ) and length( )

Decimal to binary using c language

Stuck in decimal to binary using C

How to capture the first digit while converting a decimal number to binary digit using naive recursion?

Parsing Binary files that contains BCD (Binary Coded Decimal) values using Python Numpy

Python program for converting decimal to binary

Python - Converting Binary (list) to Decimal

Binary to Int Using Recursion

Print Binary using recursion

Binary to Decimal Conversion in Haskell using Horners Algorithm

How to convert binary into decimal using <bitset> library?

Create a decimal to binary converter using Ruby?