-1

I've been searching for a while but didn't find any solution. For example, if given ranges are like [A0A0A0 -A0P1M9] - It should give all possible valid Canadian postal codes for a given range. TIA.

example: input From : A0A0A0 input To : A0A0A4 output : A0A0A0,A0A0A1,A0A0A2,A0A0A3,A0A0A4

1 Answers1

1

I'd convert the possible String codes to consecutive numbers according to something like a mixed base-10 and base-26 system, multiplying the character's value by a position-dependent factor:

  • char 0 by 10*26*10*26*10
  • char 1 by 26*10*26*10
  • char 2 by 10*26*10
  • char 3 by 26*10
  • char 4 by 10
  • char 5 by 1

Typically, I'd represent A..Z as 0..25, only (to avoid the gap at the missing W and Z character in the first place) I'd adjust the mapping at that position.

Then, I'd produce all numbers in between the two converted limits, and convert them back with the reverse scheme.

Be aware that in the worst case A0A0A0..Y9Z9Z9 you'll get 16 million strings.

Ralf Kleberhoff
  • 6,990
  • 1
  • 13
  • 7