Normy - 8 months ago 62

Python Question

I want to generate all possible outputs of a list of size N that acts as a 3 digit odometer. For example, if N = 4, I want the following output:

0000

1000

2000

3000

0100

1100

...

3332

3333.

Here is my code, any help is much appreciated!

`odom = [0]*N ## initialize odometer`

print odom

while odom[N-1] <= 3:

idx = 1

odom[0] += 1

if odom[0] > 3:

while odom[idx] > 3:

idx += 1

for i in range(idx):

odom[i] = 0

print odom

Answer Source

```
def foo(n, digits = 4):
if digits == 0:
return ''
msb, lsb = divmod(n, 4)
return str(lsb) + foo(msb, digits - 1)
result = []
number_of_digits = 4
maxn = sum(3 * pow(number_of_digits,n) for n in range(number_of_digits))
for n in range(maxn + 1):
result.append(foo(n, number_of_digits))
```