#!/usr/bin/env perl use utf8; ############################################################################### # This program generates license plates. # # Copyright © 2020 Richard Lesh. All rights reserved. ############################################################################### use Utils; use strict; use warnings; our $LETTERS = "ABCDEFGHJKLMNPQRTUVWXYZ"; our $DIGITS = "0123456789"; our $plate_count = 0; sub get_choices { my ($which) = @_; if (index($LETTERS, $which) != -1) { return $LETTERS; } elsif (index($DIGITS, $which) != -1) { return $DIGITS; } else { return $which; } } sub expand_pattern { my ($prefix, $pattern) = @_; my $CHOICES = get_choices(substr($pattern, 0, 1)); my $REMAINING_PATTERN = substr($pattern, 1); my $IS_LAST = Utils::isEmpty($REMAINING_PATTERN); foreach my $c (split('', $CHOICES)) { my $NEW_PREFIX = $prefix . $c; if ($IS_LAST) { print $NEW_PREFIX, "\n"; ++$plate_count; } else { expand_pattern($NEW_PREFIX, $REMAINING_PATTERN); } } } MAIN: { my $pattern = "ABC 123"; if (scalar(@ARGV) == 1) { $pattern = $ARGV[0]; } else { print Utils::messageFormat("Syntax: \{0:s\} pattern", "LicensePlates2"), "\n"; exit 1; } expand_pattern("", $pattern); print "Total: ", $plate_count, "\n"; }