2 kyu

Regular Expression - Check if divisible by 0b111 (7)

748 of 2,222Hacker Sakana
Description
Loading description...
Puzzles
Regular Expressions
Strings
Algorithms
  • Please sign in or sign up to leave a comment.
  • ahmet_popaj Avatar

    All the theory and technical knowledge behind these type of katas requires a lot more in-depth and thorough research.

  • user_nano Avatar

    I am not reading your api, the kata description is vague

  • blr_Oliver Avatar

    There is wider kata about computing regular expressions for divisibility tests. https://www.codewars.com/kata/5993c1d917bc97d05d000068

  • RJPlog Avatar

    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!

  • mohic Avatar

    Thank you very much for this kata. I learned a lot of things by trying to resolve it

  • Awesome A.D. Avatar

    Rust translation ready for review.

  • Forsash3451k Avatar

    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?

  • avermakov Avatar

    Python Fork

    • Updated to new test framework
    • Added empty string test case to sample tests
    • Replaced print statements with @test.it descriptions
    • Added binary representation to descriptions
  • jacksonzck Avatar

    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

  • Conrad_123 Avatar

    Took me awhile but great Kata, never heard of regex before this so I learned a lot.

  • ozichukwu Avatar

    Is it intentional that PCRE-style recursion using (?R) and (?<n>) is not supported?

  • midianDypans Avatar

    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!

  • bshore Avatar
  • bshore Avatar

    This comment has been hidden.

  • tadwilliams Avatar

    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.

  • Walter Liu Avatar

    Was my solution invalidated? It doesn't seem to count towards my honor points and rank...I did not cheat

  • garrypas Avatar

    This comment has been hidden.

  • el-f Avatar

    This comment has been hidden.

  • LearningFTW Avatar

    TypeScript translation

    No author (account suspended)

  • Madjosz Avatar

    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.

  • StefanM Avatar

    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 ....

  • nomennescio Avatar

    This comment has been hidden.

  • nomennescio Avatar

    Please review and approve my Haskell translation

  • eurydice5717 Avatar

    This comment has been hidden.

  • nomennescio Avatar

    Please review and approve my C translation

  • pa-m Avatar

    This comment has been hidden.

  • migueldim Avatar

    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?

  • vesperbot Avatar

    While 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 :)

  • RealKenshiro Avatar

    Very interesting Kata!

    But when I passed the tests, my honor is not updated (even after some minutes)

    Anyone knows the reason ?

  • za3k Avatar

    Would be slightly nicer if the test used python's 'fullmatch' rather than 'match'

  • Ddystopia Avatar

    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?

  • Faraon4 Avatar

    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 ?

  • Faraon4 Avatar

    What should I return in the end I mean if my number is divisible by 7 then return "OK"; or how should it be

  • jogarijo Avatar

    This comment has been hidden.

  • MichalZarzycki Avatar

    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.

  • IAMISSAM Avatar

    This comment has been hidden.

  • AyushBk Avatar

    I had to brush my regex skills for this solution..

  • evilhaxor Avatar

    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.

  • Whimpers Avatar

    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

  • gullymiles Avatar

    The initial code (in C#) said:

    // Write a *string* which represents a regular expression to detect whether a binary number is divisible by 7
    

    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).

  • zapakh Avatar

    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.

  • Loyal_Blanc Avatar

    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.

  • krzysztof.wolowski Avatar

    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.

  • lion999 Avatar

    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?

  • SandQueen Avatar

    I guess someone should look into this one

    Perhaps adding the Sample tests to Final tests should solve the issue. :)

  • hufftheweevil Avatar

    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...

  • Voile Avatar

    Approved

    I also wrote and approved a bunch of translations.

  • dlemvigh Avatar

    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.