Skip to content

Latest commit

 

History

History
49 lines (40 loc) · 3.66 KB

15-regular-expression-for-binary-numbers-divisible-by-5.md

File metadata and controls

49 lines (40 loc) · 3.66 KB

Problem:

Define a regular expression which tests if a given string representing a binary number is divisible by 5.

Examples:

// 5 divisable by 5
Regex.IsMatch('101', DivisibleByFive) == true

// 135 divisable by 5 Regex.IsMatch('10000111', DivisibleByFive) == true

// 666 not divisable by 5 Regex.IsMatch('0000001010011010', DivisibleByFive) == false

// 5 divisable by 5
divisibleByFive.test('101') === true

// 135 divisable by 5
divisibleByFive.test('10000111') === true

// 666 not divisable by 5
divisibleByFive.test('0000001010011010') === false
// 5 is divisible by 5
preg_match($pattern, '101'); // => 1
// 135 is divisible by 5
preg_match($pattern, '10000111'); // => 1
// 666 is not divisible by 5
preg_match($pattern, '0000001010011010'); // => 0
# 5 divisible by 5
PATTERN.match('101') == true

# 135 divisible by 5
PATTERN.match('10000111') == true

# 666 not divisible by 5
PATTERN.match('0000001010011010') == false
// 5 divisible by 5
DivisibleByFive.pattern().matcher('101').matches() == true

// 135 divisible by 5
DivisibleByFive.pattern().matcher('10000111').matches() == true

// 666 not divisible by 5
DivisibleByFive.pattern().matcher('0000001010011010').matches() == false

Note:

This can be solved by creating a Finite State Machine that evaluates if a string representing a number in binary base is divisible by given number.

The detailed explanation for dividing by 3 is here

The FSM diagram for dividing by 5 is here

Solution