4 kyu
Binary multiple of 3
2,495 of 7,640rsalgado
Description:
In this kata, your task is to create a regular expression capable of evaluating binary strings (strings with only 1
s and 0
s) and determining whether the given string represents a number divisible by 3.
Take into account that:
- An empty string might be evaluated to true (it's not going to be tested, so you don't need to worry about it - unless you want)
- The input should consist only of binary digits - no spaces, other digits, alphanumeric characters, etc.
- There might be leading
0
s.
Examples (Javascript)
multipleof3Regex.test('000')
should betrue
multipleof3Regex.test('001')
should befalse
multipleof3Regex.test('011')
should betrue
multipleof3Regex.test('110')
should betrue
multipleof3Regex.test(' abc ')
should befalse
You can check more in the example test cases
Note
There's a way to develop an automata (FSM) that evaluates if strings representing numbers in a given base are divisible by a given number. You might want to check an example of an automata for doing this same particular task here.
If you want to understand better the inner principles behind it, you might want to study how to get the modulo of an arbitrarily large number taking one digit at a time.
Regular Expressions
Algorithms
Puzzles
Number Theory
Similar Kata:
Stats:
Created | Feb 13, 2015 |
Published | Feb 13, 2015 |
Warriors Trained | 41674 |
Total Skips | 14609 |
Total Code Submissions | 33838 |
Total Times Completed | 7640 |
JavaScript Completions | 2495 |
Java Completions | 1475 |
Ruby Completions | 332 |
Python Completions | 2037 |
PHP Completions | 333 |
C# Completions | 546 |
Elixir Completions | 125 |
Go Completions | 323 |
PowerShell Completions | 69 |
Julia Completions | 69 |
C++ Completions | 642 |
CoffeeScript Completions | 24 |
Rust Completions | 132 |
Total Stars | 1000 |
% of votes with a positive feedback rating | 88% of 985 |
Total "Very Satisfied" Votes | 811 |
Total "Somewhat Satisfied" Votes | 115 |
Total "Not Satisfied" Votes | 59 |