Make a beautiful binary string
Clash Royale CLAN TAG#URR8PPP
up vote
3
down vote
favorite
This is the "Beautiful Binary String" problem at HackerRank:
Alice has a binary string. She thinks a binary string is beautiful if and only if it doesn't contain the substring 010.
In one step, Alice can change a 0 to a 1 or vice versa. Count and print the minimum number of steps needed to make Alice see the string as beautiful.
For example, if Alice's string is 010 she can change any one element and have a beautiful string.
This is my Python code:
def beautifulBinaryString(b):
temp = list(b)
count,i = 0,0
if len(b) == 3 and b == "010": count += 1
elif len(b) == 3 and b != "010": count = count
else:
while (i+3 <= len(temp)):
if temp[i:i+3] == ['0','1','0']:
count += 1
del temp[i:i+3]
else: i += 1
return count
It seems to me as having too many conditionals (though readable, I guess). Is there a more concise way to accomplish this?
Some test cases:
0101010
01100
2
0
python strings programming-challenge
add a comment |Â
up vote
3
down vote
favorite
This is the "Beautiful Binary String" problem at HackerRank:
Alice has a binary string. She thinks a binary string is beautiful if and only if it doesn't contain the substring 010.
In one step, Alice can change a 0 to a 1 or vice versa. Count and print the minimum number of steps needed to make Alice see the string as beautiful.
For example, if Alice's string is 010 she can change any one element and have a beautiful string.
This is my Python code:
def beautifulBinaryString(b):
temp = list(b)
count,i = 0,0
if len(b) == 3 and b == "010": count += 1
elif len(b) == 3 and b != "010": count = count
else:
while (i+3 <= len(temp)):
if temp[i:i+3] == ['0','1','0']:
count += 1
del temp[i:i+3]
else: i += 1
return count
It seems to me as having too many conditionals (though readable, I guess). Is there a more concise way to accomplish this?
Some test cases:
0101010
01100
2
0
python strings programming-challenge
add a comment |Â
up vote
3
down vote
favorite
up vote
3
down vote
favorite
This is the "Beautiful Binary String" problem at HackerRank:
Alice has a binary string. She thinks a binary string is beautiful if and only if it doesn't contain the substring 010.
In one step, Alice can change a 0 to a 1 or vice versa. Count and print the minimum number of steps needed to make Alice see the string as beautiful.
For example, if Alice's string is 010 she can change any one element and have a beautiful string.
This is my Python code:
def beautifulBinaryString(b):
temp = list(b)
count,i = 0,0
if len(b) == 3 and b == "010": count += 1
elif len(b) == 3 and b != "010": count = count
else:
while (i+3 <= len(temp)):
if temp[i:i+3] == ['0','1','0']:
count += 1
del temp[i:i+3]
else: i += 1
return count
It seems to me as having too many conditionals (though readable, I guess). Is there a more concise way to accomplish this?
Some test cases:
0101010
01100
2
0
python strings programming-challenge
This is the "Beautiful Binary String" problem at HackerRank:
Alice has a binary string. She thinks a binary string is beautiful if and only if it doesn't contain the substring 010.
In one step, Alice can change a 0 to a 1 or vice versa. Count and print the minimum number of steps needed to make Alice see the string as beautiful.
For example, if Alice's string is 010 she can change any one element and have a beautiful string.
This is my Python code:
def beautifulBinaryString(b):
temp = list(b)
count,i = 0,0
if len(b) == 3 and b == "010": count += 1
elif len(b) == 3 and b != "010": count = count
else:
while (i+3 <= len(temp)):
if temp[i:i+3] == ['0','1','0']:
count += 1
del temp[i:i+3]
else: i += 1
return count
It seems to me as having too many conditionals (though readable, I guess). Is there a more concise way to accomplish this?
Some test cases:
0101010
01100
2
0
python strings programming-challenge
python strings programming-challenge
edited 1 hour ago
Gareth Rees
43k394172
43k394172
asked 2 hours ago
db18
614
614
add a comment |Â
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
2
down vote
accepted
Style
Read the PEP8 style guide!
- Functions and variables should be
snake_case
- Conditions should be on the next line
if a: ...
is bad style - Conditions don't need parenthesis
while (a)
is the same aswhile a:
- Avoid
temp
variables
- Functions and variables should be
Algorithm
Your first 2 guard clauses seem very unnecessary
When I remove them, the code still works.
Consider writing docstring/tests or both with the doctest module
Alternative Code
You could use re.findall(substring, string)
for counting the occurrence,
OR string.count(substring)
making this practically a one-line
import doctest
def beautiful_binary_string(b):
"""
Returns the steps to make a binary string beautiful
>>> beautiful_binary_string("0101010")
2
>>> beautiful_binary_string("01100")
0
>>> beautiful_binary_string("0100101010100010110100100110110100011100111110101001011001110111110000101011011111011001111100011101")
10
"""
return b.count("010")
if __name__ == '__main__':
doctest.testmod()
add a comment |Â
up vote
1
down vote
Is there a more concise way to accomplish this?
Certainly.
For a start, the special cases are unnecessary. (They make me think that the code has been refactored from a recursive version).
Secondly, the expensive del temp[i:i+3]
could be replaced with i += 3
, and since the processing is no longer destructive temp
is unnecessary. This simplifies the code to
def beautifulBinaryString(b):
count, i = 0, 0
while (i+3 <= len(b)):
if b[i:i+3] == "010":
count, i = count+1, i+3
else: i += 1
return count
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
Style
Read the PEP8 style guide!
- Functions and variables should be
snake_case
- Conditions should be on the next line
if a: ...
is bad style - Conditions don't need parenthesis
while (a)
is the same aswhile a:
- Avoid
temp
variables
- Functions and variables should be
Algorithm
Your first 2 guard clauses seem very unnecessary
When I remove them, the code still works.
Consider writing docstring/tests or both with the doctest module
Alternative Code
You could use re.findall(substring, string)
for counting the occurrence,
OR string.count(substring)
making this practically a one-line
import doctest
def beautiful_binary_string(b):
"""
Returns the steps to make a binary string beautiful
>>> beautiful_binary_string("0101010")
2
>>> beautiful_binary_string("01100")
0
>>> beautiful_binary_string("0100101010100010110100100110110100011100111110101001011001110111110000101011011111011001111100011101")
10
"""
return b.count("010")
if __name__ == '__main__':
doctest.testmod()
add a comment |Â
up vote
2
down vote
accepted
Style
Read the PEP8 style guide!
- Functions and variables should be
snake_case
- Conditions should be on the next line
if a: ...
is bad style - Conditions don't need parenthesis
while (a)
is the same aswhile a:
- Avoid
temp
variables
- Functions and variables should be
Algorithm
Your first 2 guard clauses seem very unnecessary
When I remove them, the code still works.
Consider writing docstring/tests or both with the doctest module
Alternative Code
You could use re.findall(substring, string)
for counting the occurrence,
OR string.count(substring)
making this practically a one-line
import doctest
def beautiful_binary_string(b):
"""
Returns the steps to make a binary string beautiful
>>> beautiful_binary_string("0101010")
2
>>> beautiful_binary_string("01100")
0
>>> beautiful_binary_string("0100101010100010110100100110110100011100111110101001011001110111110000101011011111011001111100011101")
10
"""
return b.count("010")
if __name__ == '__main__':
doctest.testmod()
add a comment |Â
up vote
2
down vote
accepted
up vote
2
down vote
accepted
Style
Read the PEP8 style guide!
- Functions and variables should be
snake_case
- Conditions should be on the next line
if a: ...
is bad style - Conditions don't need parenthesis
while (a)
is the same aswhile a:
- Avoid
temp
variables
- Functions and variables should be
Algorithm
Your first 2 guard clauses seem very unnecessary
When I remove them, the code still works.
Consider writing docstring/tests or both with the doctest module
Alternative Code
You could use re.findall(substring, string)
for counting the occurrence,
OR string.count(substring)
making this practically a one-line
import doctest
def beautiful_binary_string(b):
"""
Returns the steps to make a binary string beautiful
>>> beautiful_binary_string("0101010")
2
>>> beautiful_binary_string("01100")
0
>>> beautiful_binary_string("0100101010100010110100100110110100011100111110101001011001110111110000101011011111011001111100011101")
10
"""
return b.count("010")
if __name__ == '__main__':
doctest.testmod()
Style
Read the PEP8 style guide!
- Functions and variables should be
snake_case
- Conditions should be on the next line
if a: ...
is bad style - Conditions don't need parenthesis
while (a)
is the same aswhile a:
- Avoid
temp
variables
- Functions and variables should be
Algorithm
Your first 2 guard clauses seem very unnecessary
When I remove them, the code still works.
Consider writing docstring/tests or both with the doctest module
Alternative Code
You could use re.findall(substring, string)
for counting the occurrence,
OR string.count(substring)
making this practically a one-line
import doctest
def beautiful_binary_string(b):
"""
Returns the steps to make a binary string beautiful
>>> beautiful_binary_string("0101010")
2
>>> beautiful_binary_string("01100")
0
>>> beautiful_binary_string("0100101010100010110100100110110100011100111110101001011001110111110000101011011111011001111100011101")
10
"""
return b.count("010")
if __name__ == '__main__':
doctest.testmod()
edited 1 hour ago
answered 1 hour ago
Ludisposed
6,41021858
6,41021858
add a comment |Â
add a comment |Â
up vote
1
down vote
Is there a more concise way to accomplish this?
Certainly.
For a start, the special cases are unnecessary. (They make me think that the code has been refactored from a recursive version).
Secondly, the expensive del temp[i:i+3]
could be replaced with i += 3
, and since the processing is no longer destructive temp
is unnecessary. This simplifies the code to
def beautifulBinaryString(b):
count, i = 0, 0
while (i+3 <= len(b)):
if b[i:i+3] == "010":
count, i = count+1, i+3
else: i += 1
return count
add a comment |Â
up vote
1
down vote
Is there a more concise way to accomplish this?
Certainly.
For a start, the special cases are unnecessary. (They make me think that the code has been refactored from a recursive version).
Secondly, the expensive del temp[i:i+3]
could be replaced with i += 3
, and since the processing is no longer destructive temp
is unnecessary. This simplifies the code to
def beautifulBinaryString(b):
count, i = 0, 0
while (i+3 <= len(b)):
if b[i:i+3] == "010":
count, i = count+1, i+3
else: i += 1
return count
add a comment |Â
up vote
1
down vote
up vote
1
down vote
Is there a more concise way to accomplish this?
Certainly.
For a start, the special cases are unnecessary. (They make me think that the code has been refactored from a recursive version).
Secondly, the expensive del temp[i:i+3]
could be replaced with i += 3
, and since the processing is no longer destructive temp
is unnecessary. This simplifies the code to
def beautifulBinaryString(b):
count, i = 0, 0
while (i+3 <= len(b)):
if b[i:i+3] == "010":
count, i = count+1, i+3
else: i += 1
return count
Is there a more concise way to accomplish this?
Certainly.
For a start, the special cases are unnecessary. (They make me think that the code has been refactored from a recursive version).
Secondly, the expensive del temp[i:i+3]
could be replaced with i += 3
, and since the processing is no longer destructive temp
is unnecessary. This simplifies the code to
def beautifulBinaryString(b):
count, i = 0, 0
while (i+3 <= len(b)):
if b[i:i+3] == "010":
count, i = count+1, i+3
else: i += 1
return count
answered 1 hour ago
Peter Taylor
14.5k2454
14.5k2454
add a comment |Â
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%2f204702%2fmake-a-beautiful-binary-string%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