d ll - 1 year ago 84

Python Question

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.

Answer Source

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
```