Skip to content

Instantly share code, notes, and snippets.

@thatPamIAm
Last active July 24, 2020 19:19
Show Gist options
  • Save thatPamIAm/27d305234c2c357cb27c0b65f612d1d5 to your computer and use it in GitHub Desktop.
Save thatPamIAm/27d305234c2c357cb27c0b65f612d1d5 to your computer and use it in GitHub Desktop.

Pattern Generator

We want to generate serial numbers for each of our products as they roll off the manufacturing line. It's important that the patterns of the serial numbers are tightly defined.

The patterns follow these rules:

  • A . in a pattern represents a place that can be filled with a capital letter A-Z
  • A # in a pattern represents a place that can be filled with a number 0-9
  • Any letter or number in the pattern is a "literal" left in the output

Write a program which can:

Part 1: Verify Pattern Compliance

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("A3B", pattern)
# => true
pg.verify("AAB", pattern)
# => false

In JavaScript:

pattern = ".#.";
pattern_verify("A3B", pattern);
// => true
pattern_verify("AAB", pattern);
// => false

Part 2: Generate The Nth Value In the Pattern

Given 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)
# => "A0A"
pg.generate(27, pattern)
# => "A1B"

In JavaScript:

pattern = ".#.";
pattern_generate(0, pattern);
// => "A0A"
pattern_generate(27, pattern);
// => "A1B"

Part 3: Possibilities

Given a specific pattern, determine how many total numbers are in the set:

In Ruby:

pg = PatternGenerator.new
pattern = ".#."
pg.total_available(pattern)
# => 6760

In JavaScript:

pattern = ".#.";
pattern_total_available(pattern);
// => 6760
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment