+ 10
What can be regular expression for this statement
All string of 0's and 1's in which the total number of 0's to the right of each 1 is even [Edit] Thanks for all your answer and help this might be the final regular expression of I am right but didn't checked it yet though https://www.sololearn.com/post/156151/?ref=app
19 Respuestas
+ 11
Supplying examples of valid/invalid strings would have made it a lot easier to understand the required rules:
Do zeroes must have a leading 1's?
If the string can begin with zeroes, can their amount be odd since they don't have a leading 1?
here's a code that covers 3 variations (description in code + comments)
https://code.sololearn.com/WQAvh32qLvKT/?ref=app
+ 7
maybe it helps 🤷♂️😅
https://code.sololearn.com/cTeN6rAn7f22/?ref=app
+ 7
~ swim ~
I feel with you
regex is 😭😭😭to me.
+ 5
~ swim ~ you are an genius in other area of programming though 👍
+ 5
Oma Falk i came up with 3 😅
+ 4
what's your string ?
what i can break down from your current question is
you need an even number of 0. what is even ? i would say as long as its dividable by 2 its even. so
(00) come to mind. it simply means twozeroes.
but maybe you'll need 2 zeroes a few times. we can use + which is means one or more occurrence. now the regex is (00)+
now we need to make sure 1 is appear next. (00)+1 done.
if you need 1 appear once or more you can use 1+ instead of 1.
+ 4
Thanks Burey for all test casses answer.
And thanks all other too Taste , ~ swim ~
Anton Böhler thanks for an another approach 👍
+ 4
Preity I made it a coding challenge. Your prob is interesting and not too hard to code.
Anton Böhler will agree
Burey maybe too
I am sure, someone will come around with a regex.
Also this is a little tip:
Next time make it a coding challenge yourself 😁😁😁
+ 3
Lol you literally know all the answers ~ swim ~ 😎
+ 3
Burey better is it
"Wer vieles bringt, wird manchem etwas bringen"
Goethe, Faust Vorspiel auf dem Theater
+ 3
Oma Falk thank you so much about saying that coding challenge but I didn't make that one an coding challenge as I've making an code about designing the automata both deterministic and non deterministic from which whenever I get back it's easy to understand. But I stuck to make any regular expression from that automata the code from Burey quite clear some cases so I'm trying with those.
Let's see if some other regex can be created as you make that as post
I would make all of automata by that and post in an code or post later.
thanks for making that post as an challenge btw I didn't know about the challenge thing 👍 👍
+ 3
I would add that whenever an number in string occurs as 1 then it's right side will have even number of 0's for example:- 10010000100 -> true
1010010000 -> false
And all the cases some of which has solved by burey with nice regex.
Thank you all for your time and help
+ 3
Anton Böhler is this explain all things which I tried to make and show
after seeing here so many expressions I got to make this one as final not know it's perfect or not
https://www.sololearn.com/post/156151/?ref=app
+ 3
My bad, sorry.
+ 2
Preity
what of these are valid?
(it would minimize confusion alot probably)
1.) 100
2.) 1
3.) 1100
4.) 00
5.) 000
6.) 00100
7.) 001
8.) 0001
+ 2
~ swim ~ welcome.
But I would say that you deserve all thanks as you have solved many of my difficulties whenever me facing thanks a lot for that
And thanks to everyone who helped me till now. I think this section and code section is the best as compare to the challenge and other things
👍 👍
+ 1
~ swim ~
When an 0 apper at right side in an automata then it must be in even else the 1 without 0 on right side is acceptable by the automata.
I'm just designing the regex for automata working which later can help me to make Turing machine pattern matching easy with the regex.
Leading 0's to can appear when you make state diagram so I'm considering both the cases for evaluation
+ 1
regex for form validation
0
edited:
import re
def check_for_even_zeros(mystr):
patten = '10+'
for x in re.findall(patten, mystr):
if x.count('0') % 2 == 1:
return False
return True
mystring1 = '100100100100' # the one has all even zeros.
print(check_for_even_zeros(mystring1)) # so this prints true.
mystring2 = '1001001001000' # this one has odd number of zeros at the end.
print(check_for_even_zeros(mystring2)) # so this prints false