totalLen = int(sys.argv[2])
low = 2 ** oneBits - 1
end = 2 ** totalLen
print('oneBits:', oneBits)
print('totalLen:', totalLen)
print('Range:', low, '-', end)
print() 
format = '{0:0%db}' % totalLen
index = 0
print('Index Pattern Value')
for i in range(low, end):
    val = format.format(i)
if val.count('1') == oneBits:
    print('%5d %s %5d' % (index, val, i))
index += 1
Поиск перестановки набора 0 и 1 по заданному индексу с O (N)