2

I currently have the need for a certain type of Iterator / generator (not actually sure which is the appropriate term) that will generate a character sequence such as the following:


axxx
bxxx
cxxx
dxxx
...
aaxx
abxx
and so on

So for every iteration through the alphabet it moves to the next place and replaces 'x' and repeats...

I have tried Iterators and generators with Python but cant seem to get this fixed character functionality.

d ll
  • 65
  • 3

1 Answers1

0

OK, break this down into two problems. I'm cribbing from

http://code.activestate.com/recipes/65212-convert-from-decimal-to-any-base-number/

for the actual baseN code.

First, generate a sequence of numbers in base 26, encoded a=0, b=1, .. z=25. After an example from "trottler"

def basealpha(num,numerals="abcdefghijklmnopqrstuvwxyz"):
    return ((num == 0) and  "0" ) or ( basealpha(num // 26, 26).lstrip("0") + numerals[num % 26])

Loop through this, and then right pad out the string with 'x' to a length of four.

The problem I see on looking at this is that 'x' serves double duty, and you'll have a hard time telling what 'aaxx' means, since it could show up in several sequences:

aaxx
aayx
aazx

or

aaxx
abxx
acxx
vielmetti
  • 1,864
  • 16
  • 23
  • 1
    This is great! exactly what i was looking for! and yes, the duplicates are not an issue as it isnt a permutation. More just wanted a 4 character sequence with x as a placeholder for the future combinations – d ll Aug 11 '16 at 03:40