Skip to content

Instantly share code, notes, and snippets.

@ekohilas
Created October 2, 2019 14:20
Show Gist options
  • Save ekohilas/a18f800bce39be76e4aa38dc62c7811a to your computer and use it in GitHub Desktop.
Save ekohilas/a18f800bce39be76e4aa38dc62c7811a to your computer and use it in GitHub Desktop.
Base Array Iteration
#!/usr/bin/python3
# Author: ekohilas
# Creates a list of lists containing the all the numbers of a certain base to
# the given length.
# i.e.
# base, length = 32, 4
# [1,1,1,1] ... [32,32,32,32]
def dec2base(n, b):
"""
Source: https://stackoverflow.com/a/28666223
"""
if n == 0:
return [0]
digits = []
while n:
digits.append(int(n % b))
n //= b
return digits[::-1]
def all_num(base, length):
return [
extend(dec2base(i, base), length)
for i in range(base ** length)
]
def extend(array, length):
return [0] * (length - len(array)) + array
def increment_all(array):
return [
item + 1
for item in array
]
def one_indexed_all_num(base, length):
return [
increment_all(array)
for array in all_num(base, length)
]
if __name__ == "__main__":
base = 32
length = 4
for num in one_indexed_all_num(base, length):
print(num)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment