5 kyu
Mod4 Regex
703 of 1,931wthit56
Loading description...
Regular Expressions
Strings
Fundamentals
View
This comment has been reported as {{ abuseKindText }}.
Show
This comment has been hidden. You can view it now .
This comment can not be viewed.
- |
- Reply
- Edit
- View Solution
- Expand 1 Reply Expand {{ comments?.length }} replies
- Collapse
- Spoiler
- Remove
- Remove comment & replies
- Report
{{ fetchSolutionsError }}
-
-
Your rendered github-flavored markdown will appear here.
-
Label this discussion...
-
No Label
Keep the comment unlabeled if none of the below applies.
-
Issue
Use the issue label when reporting problems with the kata.
Be sure to explain the problem clearly and include the steps to reproduce. -
Suggestion
Use the suggestion label if you have feedback on how this kata can be improved.
-
Question
Use the question label if you have questions and/or need help solving the kata.
Don't forget to mention the language you're using, and mark as having spoiler if you include your solution.
-
No Label
- Cancel
Commenting is not allowed on this discussion
You cannot view this solution
There is no solution to show
Please sign in or sign up to leave a comment.
This kata is a subject to deduplication process here: https://github.com/codewars/content-issues/issues/228.
Please join the discussion to help us identify duplicate kata and retire them.
Fancy kata...
Potential duplicate of Regex for a decimal number divisible by 4
Nice kata! :D
Very nice kata. I compiled a regex very quickly but the 'match' function in Python gave me hell, so I learned something new today - the difference between 'match' and 'search' regex methods in Python :)
I get "No module named 'codewars_test'", am I doing something wrong?
If you are using Python, make sure you select 3.8 version at the top of the trainer page.
Thanks, I did not even notice that version 3.6 was installed
This kata made me feel suicidal. +1 would recommend
This comment has been hidden.
OK the problem was the g flag that we don't need to use here !!
https://stackoverflow.com/questions/2851308/why-does-my-javascript-regex-test-give-alternating-results
Good kata, enjoyed it
Has this kata been modified? The test suite uses the "match" method on a Python re.compile object, but match only checks for matches at the start of a string. I don't think that is what the kata is asking for.
I used the "search" method, which scans the entire string, and sure enough it is failing only on the randomized cases where the target is embedded in the middle of the string whereas any of these randomized cases pass in the code on my machine.
This comment has been hidden.
I did understand the kata correctly. My expression simply needed a very minor correction to properly work with "match" instead of "search". Thank you.
Lack of edge test cases make https://www.codewars.com/kata/reviews/54747107cfe40042f40000d8/groups/5fa22010cef02800019b0842 pass~~
Nice kata, it would be nicer if there was a way to read the input in each test.
g flag!!! in my node 10.15.12 it's absulutely important for a decent result but on CW - not really. - I've spent a lot of time to figure out that all the deal is g flag at the end.
python:
the assertion in the random tests is strictly equivalent to the use of
Test.expect
, which is definitely bad. => They need to be rewritten with a "true" use of assert_equals.done
"767d98ef-edca-436a-9ce0-d3d2bb9ad675[-518]debdcec6-5c93-4790-9bb0-083533e39aeb[913]53d03fc5-1e27-4521-a5e3-018c5f74dbe4[~129]" 518 is not divisible by 4, 913 isn't either and neither is 129.
Issue not specified.
You are to write a Regular Expression that matches any string with at least one number divisible by 4 (with no remainder). In most languages, you could do this easily by using number % 4 == 0. How would you do it with Regex? A number will start with [ and end with ]. According to the kata description numbers will start and end with []... and those numbers should all be divisible by 4 ... but as you can see the numbers that are in brackets are not divisible by 4 and yet the expected answer was True and not false.
see above. The assertions are made with something like
assert_equals(bool(user(...))==bool(ref(...)), True, "Useless string message")
. So:I just updated the test suite. It should be clearer, now.
thanks
This comment has been hidden.
could someone please explain this:
I got this from the tests: 2504592b-156a-43d0-8f01-414f1c0c3f35[-34]bb70f6cf-edf2-4b35-8d91-e8ecbba03a78[890]7221eab8-2e54-47d4-b62b-228605826dee[~870] was incorrect: False should equal True
I don't understand why it should be true.. what's the number that's divisible by 4 here?
or here.. e477467b-94a1-4da7-8e51-fa7f163cef34[-490]2a37525e-bb0c-4f3b-be10-c126f1d978d1[566]81977699-c377-4255-b06a-c5d644c69559[~717] was incorrect: False should equal True
This comment has been hidden.
The tests should be correct. Looking at how complicated yout solution is, I'm sure there's a mistake in it. If you want somebody to help you, post your regex enclosed in backticks (``) so that it would be formatted correctly.
This comment has been hidden.
These were easy to spot. Maybe there're more issues with it.
mod4
, but divisibility checkingC# Translation added.Please review and approve~
Looks good. I would recommend:
All done but last one.Beacause the desc is not only useful for js but also C#.Hope you will like it~
That's great news - I'm at work at the moment, but I'll take a final look when I get home and then approve it.
Approved! ✅
Hello, I think I now have a good grasp of this kata. I pass all the tests in Python (using Jupyter Notebook) but in Codewars the following test fails: "the beginning [-0] the end" as it produces no match (but it does produce a match in my Jupyter Notebook...). Please advise, would really like to get this kata completed.
How are you recreating the tests? How do you know you're using the same regex engine as CW?
I'd recommend coding directly in CW, to make sure it works as it would on CW as you code it.
I must revisit here to say thank you, it has opened my mind about regexes, you links are really helpful! Keep up the good work!
Hey--thanks for letting me know! ^^
Need explanation! My code is not passing the all valid test. It is like it failled on "the beginning [-0] the end" and "~[4]" but my output is showing something different
PYTHON output on valid and invalid tests: valid test <_sre.SRE_Match object; span=(0, 8), match='[+05620]'> <_sre.SRE_Match object; span=(0, 8), match='[005624]'> <_sre.SRE_Match object; span=(0, 8), match='[-05628]'> <_sre.SRE_Match object; span=(0, 8), match='[005632]'> <_sre.SRE_Match object; span=(0, 8), match='[555636]'> <_sre.SRE_Match object; span=(0, 8), match='[+05640]'> <_sre.SRE_Match object; span=(0, 8), match='[005600]'> <_sre.SRE_Match object; span=(14, 18), match='[-0]'> <_sre.SRE_Match object; span=(1, 4), match='[4]'> <_sre.SRE_Match object; span=(0, 4), match='[32]'> <_sre.SRE_Match object; span=(14, 17), match='[0]'> <_sre.SRE_Match object; span=(10, 19), match='[+002016]'> invalid test None None None None None None
So i am confused as i understood we have simply to provide the regex expression
Your code needs to allow text before and after the square brackets also there can't be anything but the digits in the square brackets in order to be valid. The Python tests are working for me; post your code if you're still confused.
Thank you very. Fixed.
Very clever! Didn't realise regex could work in this way!
This comment has been hidden.
Make sure the "global" regex flag is off. That can do weird things, so let me know if that fixes it...
Does Java have that flag? I had tried find this, but only see it with JavaScript.
Oh, okay. I don't really know Java, to be honest; that just the first thing to try.
Looks like the Java tests use
.find()
to see if it found anything. Maybe that helps?Hah, sorry, that's my bad. I didn't see that number included in [...]. Sorry again
No worries ^^
I enjoyed this kata, thanks wthit56!
I have tried many regular expression problems before but not one like this. I actually learned something new because of it.
So happy to hear that! There are many that don't see the point in a task like this, but I wrote it so that people would stretch their understanding of Regex and learn something in the process. Thanks!
This comment has been hidden.
In the JavaScript version (the original)
.test
is used. I would imagine.search
would be problematic, as 0 could be returned and maybe mess up the test; perhaps that's what you're experiencing... Which language are you using?This comment has been hidden.
I suggest making a function of it in the Java version, rather than refering to the Pattern. This will still make it hard and you do not have to solve it with DFA theory. Now you have to due to the Matcher class being final and thus unable to extend.
If I understand your comment correctly, you're suggesting a function be used instead of forcing the use of a pattern/regex. The thing is "Mod 4" is really straightforward with normal code, so it's kind of a pointless exercise to let people do that. This kata is aimed at showing what can be done with regex, beyond what you'd normally use it for. It's a bit of a dummy example, but it should stretch regexers out there to come up with some way of solving this problem.
I am Super confused of what needs to be the output. Can someone explain me with an clear example?
There is no "output" for this kata. A given string is tested against the regex (or vis versa, depending on how you look at it). So if you match anything, the test will be successful.
So for example:
What should I understand from this: the beginning [-0] the end is valid, but no match was made.: False should equal True? I'm passing some tests, but not this one.
Not sure. Which language are you using? And could you add backticks (`) before and after the test message itslf, just so I know exactly which part is being printed out?
Why would you ever want to do this? I feel like this encourages bad approaches to problem solving.
You may not want to do this specifically, but I think the principle is interesting. I'm showing that it's possible to do these kinds of things with a bit of lateral thinking and just regex.
Hi, I am enjoying this kata but don't understand what you want the output to be? string 'valid' or 'invalid'?
Your job is to write a regular expression that matches any integer divisible by 4. So you don't really return anything. Here's an example of how it will be used (JS)
Does that help?
This comment has been hidden.
Could you give me the regex itself, please? Without the comments and such. I tried to just remove the comments, but it seems I didn't do it right...
I figured out what the issue was. I was thinking in terms of .search and not .match. Thanks tho!
I suggest adding a test for '[8]' (particularly to Python, as there is at least one solution which fails it).
In JavaScript there is a test on the string
"[8]<<"
. Would that cover it? I don't know about python, though. @jhoffner wrote that part.Yeah, this would cover the case.
Python translation was written by @JS01, as far as I can see (it can be checked via the 'plus' icon in the language icons row).
But nevermind who wrote it, you will easily add this case, just take a look at the Python test cases in Kata editor and add the string in the appropriate section.
Ah, okay. Done.
If there is a competition for the worst solution, I think I will win that one without a doubt for this Kata. Ashamed of my solution post seeing everyone's. This was amazingly difficult. Brilliant Kata, lots to improve for me in these places. Damn and I thought my Regex was brilliant. :(
Sounds intriguing! ;P Did you submit your final solution? Doesn't seem to show up here.
Hahaha I forgot to submit it, and then when I clicked on submit final, I was like WHAT HAVE I DONE. Too ashamed of my solution. Going to find more regex Katas and train.
Still not seeing it? Really curious, now! ;P
This comment has been hidden.
This comment has been hidden.
Ah--I assumed it was in JavaScript. I had to switch to python to see the python solutions, and yours is there. Sorry about that.
And man, I see what you mean! Yeah, it takes a bit of math-ing to figure out shortcuts for this--ways of simplifying so you don't have to test the entire expression, things like that. Well done on finishing it, though. Even if it's a bit hacky ;P
Does the solution need to contain a Class? Or can I write a function?
You need to write a regular expression, and store it in the Mod4 variable. So no functions, no classes, just a plain old regular expression object. Does that help?
Thanks. I'm not really familiar with Regex hence the confusion.
Ah, okay. There's a good MDN wiki on the subject (https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/RegExp), and you can learn the ins and outs of Regex here (http://www.regular-expressions.info/). I would recommend you learn regex before tackling this kata, however. I'll add a note to the description to that effect for others in the same boat.
I keep getting "Submission timed out. Please try again.". Is it because my regex is inefficeint? I did ran timeit for 10000 strings and it took less than .02 secs.
I am using Python.
I think the servers must be under heavy load or something. I tried other katas with solutions I know work (I've done them before) and they're having the same problem. I'd recommend just waiting a little bit (maybe a few hours) and see if it clears up.
Yes, I guess servers were loaded. I was able to submit after few hours. Thanks!!
Cool. No problem.
This comment has been hidden.
You don't have to match the whole string; you just have to make any kind of match. For the JavaScript version,
Mod4.test(str)
is used, so as long as some kind of match is made, you'll pass. Not sure about the other language versions (I didn't write them), but they should be the same.In the Python vrsion of the kata, your regex has to match the whole string. That must be a bug in the translation?
Yeah... I've had a look, but without knowing Python, it's a bit tricky guessing what the problem is. @jhoffner was the one that wrote it, so perhaps he can have a look..?
Python's match() only returns true if the regex matches at the beginning of the string (this is, IMO, insane). The tests should be rewritten to use search() instead.
This comment has been hidden.
Which language? I only wrote the original JavaScript version...
Sorry about that :)
That's one of the tests for python in the 'Random Tests' section (I'd check but need to make sure I'm not making a mistake on my end and pass first before I can see the test cases)
How has this developed? Is is still an issue?
Wonderful kata, thank you very much!
Probably, CodeWars should consider adding regexp as a separate kata language. ;)
Thanks, Jolaf. That would be cool! ;P
That would be great, but they would also need to build their own parser/engine.
I must say that this would be much easier using binary. I had an idea for a slightly harder kata around the same concept: Digital Root of a Number using Regex. There are 10 equivalence classes to deal with, so it would result in a pretty massive regular expression, whereas there are only 4 equivalence classes with this one.
It would be, wouldn't it? ;P
I hate regex, they never do what I want.
I think I will need help on that because I have difficulties to understand why isn't matching. First question is why when I'm testing on some online regex testing results are sometimes different. I'm using https://www.debuggex.com/ that is really great to visualize what you are doing but on some tests it wasn't matching here but it was there.
Rest of the question in spoiler next
This comment has been hidden.
I think it's just the "global" flag you've set. That only works properly when expecting one or more match in a string. When you're just running it as a test, it can get confused about where it should be starting from. It's complicated, but basically, take off the "g" and you should be good.
Argh...Thanks
Just shown me that 8 isn't tested...
8 isn't tested
Not sure what you mean? Are you saying I am not testing "8"?Yes, there isn't test "[8]"
Ah, I see. Fair point. I've added a test for that, now. Thanks for your feedback!
Give a few more examples in the example problems as edge cases - for instance, "sdfd[4]sdfdsf" should have a match. It is stated in the description that this is possibe, but it'd be more clear if listed as an example
I'm not sure what edge case you are referring to? There is a
"No, [2014] isn't a multiple of 4..."
example in the description already. Is your example not covered by that?I've added a kata translation to Java (so few Java katas).
Awesome. Thank you!
Fun little kata. I have two small change proposals:
The given tests already contain this information (therefore I have not tagged this as an issue), but I was genuinely confused reading just the description.
Fair comments. Thanks for bringing this to my attention. I've also fixed some typos. How does it look to you now?
Looking good. Good idea making the meaning of valid/invalid more explicit.
Past the third example including the numbers in a comment probably isn't necessary though. You did forget to actually tag 5600. But anyway, I think it's clear enough now.
Cool beans. Thanks for your help!
This comment has been hidden.
Thanks for your suggestions. I've added and changed the parts you mentioned. Thanks for your help! ^^