2 kyu
Regular Expression - Check if divisible by 0b111 (7)
748 of 2,222Hacker Sakana
Loading description...
Puzzles
Regular Expressions
Strings
Algorithms
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.
All the theory and technical knowledge behind these type of katas requires a lot more in-depth and thorough research.
I am not reading your api, the kata description is vague
There is wider kata about computing regular expressions for divisibility tests. https://www.codewars.com/kata/5993c1d917bc97d05d000068
Great Kata, took me a long time to figure out how to solve until I found something about DFA and regular expressions, but I learned a lot!
finally got it!
Thank you very much for this kata. I learned a lot of things by trying to resolve it
Rust translation ready for review.
Hello, I'm a little confused about what this kata requires to do. I understood the task as checking a binary number for divisibility by 7, but the function that is given to us initially does not have any number at the input with which the code will work. Please explain where we get this input number from and what does this kata want from the user in general?
The task is to construct and return a regular expression pattern, that will match a binary number only if it is divisible by 7. The numbers are never passed to the function to be implemented.
For example, if the task was to match binary numbers divisible by 2 (which is trivial), you would return:
Pattern.compile("^[01]*0$")
. This task is much more complex, of course.The method returns a string containing a regular expression. For understanding, look at the "Sample Tests" section under the task.
Python Fork
print
statements with@test.it
descriptionsLooks good, approved
I have a solution that works in C++ and python, but fails in C. Does anyone know what would be different in the implementations of regular expressions between C++ and C that would cause my expression to work in C++ but not in C? I am pretty sure I am following the POSIX extended standard, but if there are any small differences, I would love to be informed of such. Thanks :D
Took me awhile but great Kata, never heard of regex before this so I learned a lot.
Is it intentional that PCRE-style recursion using
(?R)
and(?<n>)
is not supported?Okay it's language-specific. PHP has PCRE implemented.
Rather hard for someone who dont know funite automaton or regular expression. I have to learn a lot about it through solving the problem. The process was difficult but it was interesting finally. Enjoy it!
Update for the Go translation https://www.codewars.com/kumite/608a5971aec3f6000be3ed1e?sel=6268cd608a33fe2c5cad8a1c
This comment has been hidden.
Is this now fixed in your update?
Yes, it should be
If you are like me and have never taken any comp sci courses or studied any theory then this one wont just pop out at you. I spent a few days thinking about patterns in binary numbers, etc and eventually stumbled upon Finite Automata. A few youtube videos and more importantly buying a 10$ book on amazon on "Finite automata and regular expressions" allowed me to solve it. But it was a lot of work and in my opinion as a hobby coder not worth my efforts. Still interesting though so kudos to anyone who solved it because the answer is bonkers.
After trying to find patterns in binary I've hacked the tests in javascript and after I saw what real solutions look like I'm glad I did it - because otherwise I'd go crazy :) Thanks to solutions and discussions I'm now exploring DFA which is a totally new thing for me and I still have no clue how do you build one with RegExp but I'm excited to learn that! Should probably buy that book you mentioned too :)
Was my solution invalidated? It doesn't seem to count towards my honor points and rank...I did not cheat
This comment has been hidden.
Done at last. My brain is fried.
This comment has been hidden.
TypeScript translation
No author (account suspended)
Ducktyping in Go makes non-regex solutions possible: https://www.codewars.com/kata/reviews/6137185f29bcb40001eb47a4/groups/61399138fbac520001abbd7c Maybe there are ways in Go to enforce that actually a regex is returned.
Seems to have been fixed in the meantime.
I cannot reproduce the failed test below on my local in js using node many versions or many browsers consoles with an instance of RegExp, the test is bogus:
Gentle fixed tests 100 fixed tests "10" is not divisible by 7: expected true to equal false Completed in 2ms
My tests: true for 0: 0 false for 1: 1 false for 2: 10 false for 3: 11 false for 4: 100 false for 5: 101 false for 6: 110 true for 7: 111 false for 8: 1000 false for 9: 1001 false for 10: 1010 false for 11: 1011 false for 12: 1100 false for 13: 1101 true for 14: 1110 false for 15: 1111 false for 16: 10000 false for 17: 10001 false for 18: 10010 false for 19: 10011 false for 20: 10100 true for 21: 10101 false for 22: 10110 false for 23: 10111 false for 24: 11000 false for 25: 11001 false for 26: 11010 false for 27: 11011 true for 28: 11100 false for 29: 11101 false for 30: 11110 false for 31: 11111 false for 32: 100000 false for 33: 100001 false for 34: 100010 true for 35: 100011 false for 36: 100100 false for 37: 100101 false for 38: 100110 false for 39: 100111 false for 40: 101000 false for 41: 101001 true for 42: 101010 false for 43: 101011 false for 44: 101100 false for 45: 101101 false for 46: 101110 false for 47: 101111 false for 48: 110000 true for 49: 110001 false for 50: 110010 false for 51: 110011 false for 52: 110100 false for 53: 110101 false for 54: 110110 false for 55: 110111 true for 56: 111000 false for 57: 111001 false for 58: 111010 false for 59: 111011 false for 60: 111100 false for 61: 111101 false for 62: 111110 true for 63: 111111 false for 64: 1000000 false for 65: 1000001 false for 66: 1000010 false for 67: 1000011 false for 68: 1000100 false for 69: 1000101 ....
This comment has been hidden.
Approved.
Please review and approve my Haskell translation
Approved.
This comment has been hidden.
Please review and approve my C translation
This comment has been hidden.
Approved some time ago
Not an expert in regex (who is anyway). I managed to make it work with re.fullmatch, however it doesn't with work with match. Any clue on how to convert the regex from one to another?
Check what
^
and$
doWhile studying the problem I've stumbled on an online tool to generate such regexes. It was an ordeal trying to make the regex by hand using principles explained in there, but at least I did my own in here :)
Care to share a link?!
Very interesting Kata!
But when I passed the tests, my honor is not updated (even after some minutes)
Anyone knows the reason ?
Im not 100% sure I understand but if you're reffering to the tests and not the attempt than you shouldn't get any honor you only get honor when you pass the attempt
If that's not what you mean maybe this can help: https://github.com/Codewars/codewars.com/wiki/Honor-&-Ranks
Would be slightly nicer if the test used python's 'fullmatch' rather than 'match'
I have a question: I made a state machine on my piece of paper, but I really don't want to manually convert it into a regular expression. Can I consider the kata completed if I use a third-party transformation tool?
Only if you solve Regular Expression - divisible by n in a proper way later tomorrow.
But come on, I converted DFA into answer for this kata manually, and got it right after 5th or 6th try, so it's not that hard ;) Later it turned out that expression generated by the solution to the other kata was half of its length :?
You are laughing at me, but thanks.
C̷h̷e̷a̷t̷e̷r̷ . Then I take it back. Pardon me sir.
While it's true that there's many cheating solutions for this kata, this particular user solved it in a legit way before 'experimenting' with this workaround.
Mind sharing which tool did you use for that? Since writing FSM is pretty easy however manual conversion doesn't sound appealing.
I understand that this kata is harder than my level (my level is 7) but I am interesting is solving it: And I have a question : Should I introduce expresion to check or this kata give expresion for checking ?
You've to "give" regular expression to check.
Ok , Thanks But I have other question When I write Console.ReadLine(); it said that Console Doesn't exist in the corent context
That's because
Console
is part ofSystem
namespace so to use it you either should writeSystem.Console
or to bring it into context add lineusing System;
at the very top.Also there's no need for you to read any input from standard stream, all that the tests require is
MultipleOf7
function, which only returns the regular expression as a string.What should I return in the end I mean if my number is divisible by 7 then return "OK"; or how should it be
Please mind that this kata being of 2 kyu rank is far from easy.
In this kata, you should return a regular expression, which, when matched against a binary form of a number, will match it or not, depending on whether the number is divisible by 7 or not.
If these instructions are not clear, it probably means you should currently focus more on easier tasks (white, yellow) and come back to this one later.
Good luck!
did you checked the trainer of this kata? You have to return your regular expression which you'll write in the inverted commas.
This comment has been hidden.
Very nice Kata, I learnt a lot, especially about DFA. Drawing the schema was the easy part, but turning it to regExp... let's say, I lost "few" days for that.
This comment has been hidden.
I had to brush my regex skills for this solution..
Just managed to do it. Finally learned how to do the whole process. I feel so happy about this!! But looking at it now, its not so difficult once you know how its done. Please checkout my (JS solution)[https://www.codewars.com/kata/reviews/56a73d2b94505c29f600002f/groups/5d8a0a27f9479b0001db8af8], let me know your thoghts.
My question might not be so clear. is there a way in regex to check for different things based on a previous tested string char. In this example I want to test for different things based on if i find a 1 or a 0 at a specific position
SOLVED! This kata was great and took a while but all worth it. Thanks for the great kata!
The initial code (in C#) said:
It wasn't clear to me whether the author meant:
(a) "you, personally, write a string" - I should author a string which represents that regular expression, and then just create a one-line function in my language of choice which returns that constant string; or
(b) "create a program to write a string" - I should author a program which will build that regular expression and return it as a string
in the end I went for (a) because it was a lot quicker, but I can see from the solutions that many people interpreted this as (b).
Why should the empty string be rejected? An empty binary string is the sum of no powers of two. The sum of no things is zero. Zero is divisible by 7.
Empty string is not zero, the OP explictly tests for zero.
It would be great if someone tells me what should I learn first to solve this Kata... I used to solve a similar problem which the number is 3, but the method doesn't work since the nested can be too complex.
the method is exactly the same. That's efectively a nightmare to make it by hand. So generally, "one" solves the 1 kyu verison first, then generate a regex from there... ;)
Or: you can write a solution using recursion, find that it works on regex101.com, then come back here to submit it and realise that in python this kata uses the re module, which does not support recursion -_-
Back to the drawing board...
This comment has been hidden.
Sample tests pass, empty string is not matched, but I get 12000ms timeout when attempting to send my solution. Had to switch from PHP to JavaScript which is more verbose as to what is wrong. Turns out that with my regex all tests are green, but only about 640 out of 1000 are completed before timeout kicks in.
too much parentheses, then.
Testing for: 84269425 expected: false but was: true
Test seems to be wrong.
Another question - I get true for 7 in my IDE, but my code somehow gets false in sample tests? What might be the reason for such different dehaviour?
84269425 / 7 = 12038489.285714285
Yes, you're right. Thank you very much!
Could you also say why I get true for 7 in my IDE, but my code gets false in the kata? What might be the reason for such different dehaviour?
In some languages the regex method used might require matching the entire string?
I'm not sure what language you use though, so I can't really tell if it's the case.
I guess someone should look into this one
Perhaps adding the Sample tests to Final tests should solve the issue. :)
Done.
I replaced the JS tests completely to be in line with the tests on every other language.
Wow - what a workout for my brain! The best part about this kata is that it appears the answer it not available if you search Google. Sure, there are plenty of sites that show a few others, but not 7. Finally found one that explained well enough how to do it, though. I still used many sheets of paper in the process. Thanks!
I'm quite curious now if a program could generate a regex for divisbility by any number in binary. Formulating ideas...
Yes, it's possible to generate such regexes. I know such a program (in Haskell) :)
Look at this kata: https://www.codewars.com/kata/5993c1d917bc97d05d000068 And it got 20 solutions.
Proud of one of them is mine :)
Errrr... hufftheweevil IS the author of the kata you linked... (this conversation is the birth of this kata!)
Approved
I also wrote and approved a bunch of translations.
Hi,
How is it you can approve translations? New feature??
Yes, jhoffner pushed a new feature that allows power users to approve translations.
https://github.com/Codewars/codewars.com/issues/1065
The "is not translation author" check is currently not working though, so I can write translations and then instant-approve them :P
sounds good... But it seems I'm not enough "poweruser" to approve mine there :o. Or the check is working, just now. Could you do it, please?
This one is waiting too, btw! (both authors inactive since 2 months at least)
You might have to use the preview site before you can see the approve button.
I can't approve the second translation yet (description conflict again), so please fork your translation so that it can be approved (I've already updated the main descriptions to be the same as yours so it should work pretty fine) ;-)
thanks!
What do you mean? I didn't understand that.
I went again to the second one and found the buttons there, this time! :o But nothing happens when I hit "approve" and confirm... :oo That's Related to the current server issue, I guess? (impossible to "attemp" anything, currently)
Preview site: https://preview.codewars.com/
When you hit "approve" on that kata right now it should instantly report "Description conflict" at the bottom of the window.
perview site: errr, actually, I just end up at a page of presentation of CW, but didn't find mention of "preview" anywhere in it :o
translation: I cannot submit/publish right now 'cause of the servers issue... We'll see to this later.
cheers!
Ok republished but this time, I do not see the buttons... x-/
can you approve it, please?
Done!
It should appear on the kata shortly.
cool, thanks! :)
A really cool brainteaser. At first glance I did not think it was possible to do with regular expressions. I've been unable to get this kata off my mind, and I am very happy to finally have solved it.
Congrats to you! :P
It would be great if you would tell me how you came up with your solution :) It does seem generated, but I do wonder how you made it so much shorter than, well, mine.