Binary to decimal in python
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
0
down vote
favorite
binaryToDecimal = lambda binary: sum([[int(2**index) for index in range(len(binary))][-index-1] for index, char in enumerate(binary) if int(char)])
I wrote a simple function that converts binary strings into decimal integers. How can this be improved/simplified?
I know about the int
function, I wanted to implement the conversion myself.
python reinventing-the-wheel number-systems
 |Â
show 1 more comment
up vote
0
down vote
favorite
binaryToDecimal = lambda binary: sum([[int(2**index) for index in range(len(binary))][-index-1] for index, char in enumerate(binary) if int(char)])
I wrote a simple function that converts binary strings into decimal integers. How can this be improved/simplified?
I know about the int
function, I wanted to implement the conversion myself.
python reinventing-the-wheel number-systems
what about using python:binaryToDecimal = lambda x: str(int(x,2))
â Jean-François Fabre
May 9 at 18:41
I'm aware that you can do that :) I just wanted to implement it myself
â Hum4n01d
May 9 at 18:41
Well, readability could be improved a lot by not using too much list comprehensions together :)
â IEatBagels
May 9 at 18:42
1
Definitely, I did that on purpose though to make it as compact as possible
â Hum4n01d
May 9 at 18:43
2
I added reinventing-the-wheel, because that's what you're doing here: inventing this even though a standard implementation exists. In the future, you may want to add this tag yourself, to signify to other users that you're knowingly not using the standard implementation.
â Daniel
May 9 at 19:28
 |Â
show 1 more comment
up vote
0
down vote
favorite
up vote
0
down vote
favorite
binaryToDecimal = lambda binary: sum([[int(2**index) for index in range(len(binary))][-index-1] for index, char in enumerate(binary) if int(char)])
I wrote a simple function that converts binary strings into decimal integers. How can this be improved/simplified?
I know about the int
function, I wanted to implement the conversion myself.
python reinventing-the-wheel number-systems
binaryToDecimal = lambda binary: sum([[int(2**index) for index in range(len(binary))][-index-1] for index, char in enumerate(binary) if int(char)])
I wrote a simple function that converts binary strings into decimal integers. How can this be improved/simplified?
I know about the int
function, I wanted to implement the conversion myself.
python reinventing-the-wheel number-systems
edited May 9 at 19:24
Daniel
4,1132836
4,1132836
asked May 9 at 18:33
Hum4n01d
1065
1065
what about using python:binaryToDecimal = lambda x: str(int(x,2))
â Jean-François Fabre
May 9 at 18:41
I'm aware that you can do that :) I just wanted to implement it myself
â Hum4n01d
May 9 at 18:41
Well, readability could be improved a lot by not using too much list comprehensions together :)
â IEatBagels
May 9 at 18:42
1
Definitely, I did that on purpose though to make it as compact as possible
â Hum4n01d
May 9 at 18:43
2
I added reinventing-the-wheel, because that's what you're doing here: inventing this even though a standard implementation exists. In the future, you may want to add this tag yourself, to signify to other users that you're knowingly not using the standard implementation.
â Daniel
May 9 at 19:28
 |Â
show 1 more comment
what about using python:binaryToDecimal = lambda x: str(int(x,2))
â Jean-François Fabre
May 9 at 18:41
I'm aware that you can do that :) I just wanted to implement it myself
â Hum4n01d
May 9 at 18:41
Well, readability could be improved a lot by not using too much list comprehensions together :)
â IEatBagels
May 9 at 18:42
1
Definitely, I did that on purpose though to make it as compact as possible
â Hum4n01d
May 9 at 18:43
2
I added reinventing-the-wheel, because that's what you're doing here: inventing this even though a standard implementation exists. In the future, you may want to add this tag yourself, to signify to other users that you're knowingly not using the standard implementation.
â Daniel
May 9 at 19:28
what about using python:
binaryToDecimal = lambda x: str(int(x,2))
â Jean-François Fabre
May 9 at 18:41
what about using python:
binaryToDecimal = lambda x: str(int(x,2))
â Jean-François Fabre
May 9 at 18:41
I'm aware that you can do that :) I just wanted to implement it myself
â Hum4n01d
May 9 at 18:41
I'm aware that you can do that :) I just wanted to implement it myself
â Hum4n01d
May 9 at 18:41
Well, readability could be improved a lot by not using too much list comprehensions together :)
â IEatBagels
May 9 at 18:42
Well, readability could be improved a lot by not using too much list comprehensions together :)
â IEatBagels
May 9 at 18:42
1
1
Definitely, I did that on purpose though to make it as compact as possible
â Hum4n01d
May 9 at 18:43
Definitely, I did that on purpose though to make it as compact as possible
â Hum4n01d
May 9 at 18:43
2
2
I added reinventing-the-wheel, because that's what you're doing here: inventing this even though a standard implementation exists. In the future, you may want to add this tag yourself, to signify to other users that you're knowingly not using the standard implementation.
â Daniel
May 9 at 19:28
I added reinventing-the-wheel, because that's what you're doing here: inventing this even though a standard implementation exists. In the future, you may want to add this tag yourself, to signify to other users that you're knowingly not using the standard implementation.
â Daniel
May 9 at 19:28
 |Â
show 1 more comment
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
Of course, the most efficient solution would be to let python parse the binary string as integer ;)
binaryToDecimal = lambda x: int(x,2)
But you already know that, so what about simplifying your code like this:
binaryToDecimal = lambda binary: sum(1<<(len(binary)-index-1) for index, char in enumerate(binary) if char=='1')
- removing the creation of the inner list comprehension just to pick one value
- not using power but bitshift (integer computation, faster, more accurate)
- don't create a list to pass to
sum
, pass a generator comprehension (it's faster, saves memory) - in your condition, don't convert your number to integer, just test against the char value (it's faster)
Keep in mind that your parsing has no error checking!
Funny enough, I tested the character at first but moved to int to save characters (I was challenging myself to use the least characters)
â Hum4n01d
May 10 at 0:35
yes, it's good for code golfing, but not really for performance :)
â Jean-François Fabre
May 10 at 15:12
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
Of course, the most efficient solution would be to let python parse the binary string as integer ;)
binaryToDecimal = lambda x: int(x,2)
But you already know that, so what about simplifying your code like this:
binaryToDecimal = lambda binary: sum(1<<(len(binary)-index-1) for index, char in enumerate(binary) if char=='1')
- removing the creation of the inner list comprehension just to pick one value
- not using power but bitshift (integer computation, faster, more accurate)
- don't create a list to pass to
sum
, pass a generator comprehension (it's faster, saves memory) - in your condition, don't convert your number to integer, just test against the char value (it's faster)
Keep in mind that your parsing has no error checking!
Funny enough, I tested the character at first but moved to int to save characters (I was challenging myself to use the least characters)
â Hum4n01d
May 10 at 0:35
yes, it's good for code golfing, but not really for performance :)
â Jean-François Fabre
May 10 at 15:12
add a comment |Â
up vote
2
down vote
accepted
Of course, the most efficient solution would be to let python parse the binary string as integer ;)
binaryToDecimal = lambda x: int(x,2)
But you already know that, so what about simplifying your code like this:
binaryToDecimal = lambda binary: sum(1<<(len(binary)-index-1) for index, char in enumerate(binary) if char=='1')
- removing the creation of the inner list comprehension just to pick one value
- not using power but bitshift (integer computation, faster, more accurate)
- don't create a list to pass to
sum
, pass a generator comprehension (it's faster, saves memory) - in your condition, don't convert your number to integer, just test against the char value (it's faster)
Keep in mind that your parsing has no error checking!
Funny enough, I tested the character at first but moved to int to save characters (I was challenging myself to use the least characters)
â Hum4n01d
May 10 at 0:35
yes, it's good for code golfing, but not really for performance :)
â Jean-François Fabre
May 10 at 15:12
add a comment |Â
up vote
2
down vote
accepted
up vote
2
down vote
accepted
Of course, the most efficient solution would be to let python parse the binary string as integer ;)
binaryToDecimal = lambda x: int(x,2)
But you already know that, so what about simplifying your code like this:
binaryToDecimal = lambda binary: sum(1<<(len(binary)-index-1) for index, char in enumerate(binary) if char=='1')
- removing the creation of the inner list comprehension just to pick one value
- not using power but bitshift (integer computation, faster, more accurate)
- don't create a list to pass to
sum
, pass a generator comprehension (it's faster, saves memory) - in your condition, don't convert your number to integer, just test against the char value (it's faster)
Keep in mind that your parsing has no error checking!
Of course, the most efficient solution would be to let python parse the binary string as integer ;)
binaryToDecimal = lambda x: int(x,2)
But you already know that, so what about simplifying your code like this:
binaryToDecimal = lambda binary: sum(1<<(len(binary)-index-1) for index, char in enumerate(binary) if char=='1')
- removing the creation of the inner list comprehension just to pick one value
- not using power but bitshift (integer computation, faster, more accurate)
- don't create a list to pass to
sum
, pass a generator comprehension (it's faster, saves memory) - in your condition, don't convert your number to integer, just test against the char value (it's faster)
Keep in mind that your parsing has no error checking!
answered May 9 at 18:46
Jean-François Fabre
783210
783210
Funny enough, I tested the character at first but moved to int to save characters (I was challenging myself to use the least characters)
â Hum4n01d
May 10 at 0:35
yes, it's good for code golfing, but not really for performance :)
â Jean-François Fabre
May 10 at 15:12
add a comment |Â
Funny enough, I tested the character at first but moved to int to save characters (I was challenging myself to use the least characters)
â Hum4n01d
May 10 at 0:35
yes, it's good for code golfing, but not really for performance :)
â Jean-François Fabre
May 10 at 15:12
Funny enough, I tested the character at first but moved to int to save characters (I was challenging myself to use the least characters)
â Hum4n01d
May 10 at 0:35
Funny enough, I tested the character at first but moved to int to save characters (I was challenging myself to use the least characters)
â Hum4n01d
May 10 at 0:35
yes, it's good for code golfing, but not really for performance :)
â Jean-François Fabre
May 10 at 15:12
yes, it's good for code golfing, but not really for performance :)
â Jean-François Fabre
May 10 at 15:12
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f194037%2fbinary-to-decimal-in-python%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
what about using python:
binaryToDecimal = lambda x: str(int(x,2))
â Jean-François Fabre
May 9 at 18:41
I'm aware that you can do that :) I just wanted to implement it myself
â Hum4n01d
May 9 at 18:41
Well, readability could be improved a lot by not using too much list comprehensions together :)
â IEatBagels
May 9 at 18:42
1
Definitely, I did that on purpose though to make it as compact as possible
â Hum4n01d
May 9 at 18:43
2
I added reinventing-the-wheel, because that's what you're doing here: inventing this even though a standard implementation exists. In the future, you may want to add this tag yourself, to signify to other users that you're knowingly not using the standard implementation.
â Daniel
May 9 at 19:28