We want the ability to generate serial numbers for each of our products as they roll off the manufacturing line. It's important that the pattern of the serial numbers is tightly defined and numbers generated in a logical fashion.
A pattern follows these rules:
- A
.in a pattern represents a place that can be filled with a capital letterA-Z - A
#in a pattern represents a place that can be filled with a digit0-9 - A
@in a pattern represents a place that can be filled with a digit or letter0up toZ
Write a program which can:
Given a generated serial number, return true or false based on whether or not the input matches the pattern.
In Ruby:
pg = PatternGenerator.new
pattern = ".#.@"
pg.verify("A3B1", pattern)
# => true
pg.verify("AABA", pattern)
# => falseIn JavaScript:
pattern = ".#.@";
pattern_verify("A3B1", pattern);
// => true
pattern_verify("AABA", pattern);
// => falseGiven a specific pattern, assuming that incrementing happens from right to left (like normal numbers), generate the Nth value in the sequence:
In Ruby:
pg = PatternGenerator.new
pattern = "@.#."
pg.generate(0, pattern)
# => "0A0A"
pg.generate(27, pattern)
# => "0A1B"In JavaScript:
pattern = "@.#.";
pattern_generate(0, pattern);
// => "0A0A"
pattern_generate(27, pattern);
// => "0A1B"Given a specific pattern, determine how many total numbers are in the set:
In Ruby:
pg = PatternGenerator.new
pattern = ".#."
pg.total_available(pattern)
# => 6760
pattern = "@.#."
pg.total_available(pattern)
# => 243360In JavaScript:
pattern = ".#.";
pattern_total_available(pattern);
// => 6760
pattern = "@.#.";
pattern_total_available(pattern);
// => 243360Given a sample of three serial numbers, output a pattern that could generate them. Prefer . and # over @ if they fit the sample serial numbers.
pg = PatternGenerator.new
inputs = ["0A1B", "5C2Z", "9R9B"]
pg.find_pattern_for(inputs)
# => "#.#."
inputs_2 = ["0A1B", "5C2Z", "AR9B"]
pg.find_pattern_for(inputs_2)
# => "@.#."In JavaScript:
inputs = ["0A1B", "5C2Z", "9R9B"];
pattern_for_inputs(inputs);
// => "#.#."
inputs_2 = ["0A1B", "5C2Z", "AR9B"];
pattern_for_inputs(inputs_2);
// => "@.#."