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 1s and 0s) 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 0s.

Examples (Javascript)

  • multipleof3Regex.test('000') should be true
  • multipleof3Regex.test('001') should be false
  • multipleof3Regex.test('011') should be true
  • multipleof3Regex.test('110') should be true
  • multipleof3Regex.test(' abc ') should be false

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

Stats:

CreatedFeb 13, 2015
PublishedFeb 13, 2015
Warriors Trained41674
Total Skips14609
Total Code Submissions33838
Total Times Completed7640
JavaScript Completions2495
Java Completions1475
Ruby Completions332
Python Completions2037
PHP Completions333
C# Completions546
Elixir Completions125
Go Completions323
PowerShell Completions69
Julia Completions69
C++ Completions642
CoffeeScript Completions24
Rust Completions132
Total Stars1000
% of votes with a positive feedback rating88% of 985
Total "Very Satisfied" Votes811
Total "Somewhat Satisfied" Votes115
Total "Not Satisfied" Votes59
Ad
Contributors
  • rsalgado Avatar
  • Unnamed Avatar
  • CIS 122 Avatar
  • Klemsus Avatar
  • donaldsebleung Avatar
  • Blind4Basics Avatar
  • imatmati Avatar
  • prsmith Avatar
  • C-V Avatar
  • Awesome A.D. Avatar
  • hobovsky Avatar
  • Console Cowboy Avatar
  • stellartux Avatar
  • leofun01 Avatar
  • akar-0 Avatar
  • avermakov Avatar
  • XoRMiAS Avatar
Ad