0

I want to enter as start input for example 22Ef and FFFF as end input and I want to print all string between these 2 input, the output should be like this

22Ef
22EA
22EB
.
.
.
FFFE
FFFF

and this is my string that I want to pick character

0123456789abcdefABCDEF
2
  • So you want to enumerate a range of numbers in base 22, encoded in a non-standard way? Commented Aug 19, 2021 at 10:34
  • yes............ Commented Aug 19, 2021 at 11:07

2 Answers 2

2

This solution avoids going through all of the combinations from 0000 to your starting input:

start = '22Ef'
end = 'FFFF'
characterset = '0123456789abcdefABCDEF'

base = len(characterset)
baseNnumber = [] # little endian list of integers each representing a digit

# find base N representation of start
exponent = 0
startval = 0
for i in start[::-1]:
    digit = characterset.index(i)
    baseNnumber.append(digit)
    startval += digit * base**exponent
    exponent += 1

baseNnumber.extend([0]*(len(end)-len(start)))

# find representation of end
exponent = 0
endval = 0
for i in end[::-1]:
    digit = characterset.index(i)
    endval += digit * base**exponent
    exponent += 1

# number of times to print
increments = endval - startval + 1

for i in range(increments):
    index = 0
    outstr = ''

    # remove leading zeros
    notzero = False
    for i in baseNnumber[::-1]:
        notzero = notzero | (i != 0)
        # add new character to string
        outstr += characterset[i] * notzero

    # the actual printing
    print(outstr)

    # increment baseNnumber by 1
    while True:
        baseNnumber[index] += 1
        if baseNnumber[index] == base:
            baseNnumber[index] = 0
            index += 1
        else:
            break

If you want the leading zeros included you may want to replace lines 34-38 with this as notzero is meant to remove them:

    for i in baseNnumber[::-1]:
        outstr += characterset[i]

Or if you are certain the start and end inputs are the same simply delete line 17 and make the replacement above as it's meant to solve the issue of start and end inputs being different lengths.

Sign up to request clarification or add additional context in comments.

Comments

0

The following (using the product function from itertools) should work:

from itertools import product
strings = []
start = "22Ef"
end = "FFFF"
include = False
for i in product("0123456789abcdefABCDEF", repeat=4):
   curstring = "".join(i)
   if curstring == start:
       include = True
   if curstring == end:
        strings.append(curstring)
        break
   if include:
        strings.append(curstring)

print(strings)

3 Comments

There is any way to work with long range as I have input with 64 character range? and thanks
Do you mean that both the start value and end value can have 64 characters? In that case I think the other answer may work, but it will take a very, very long time to compute.
For example if you want to find all the combinations between a start string of 64 "2"'s and and end string of 64 "3"'s there are about 3.9e84 different permutations.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.