So I am given a string of digits of variable length and I need to find all possible number combinations using the digits in that string where only the digits in between may be changed, for example:
if I am given 123, I need to find the combinations of 1x2y3 where x, y are any digits.
if I am given 5312, I need to find the combinations of 5a3b1c2 where a, b, c are any digits.
I'm thinking this is possible using python's re.escape function, this is as far as I've come:
#Given the digits '123' from STDIN
#create a string "1\d2\d3"
my_regex_string = '\d'.join(input().split())
#Set x as starting point, set y as limit (not the most efficient)
x = 10**(len(my_regex_string)-1) * int(my_regex_string[0])
y = 10**(len(my_regex_string)-1) * (int(my_regex_string[0]) + 1)
while x < y:
if bool(re.match(re.escape(p), str(x)))
print(x)
x+=1
I need feedback, does my approach make sense? Is this task doable with regex or do I need another approach?
itertools?itertools.product.1\d2\d3, etc. To generate, its just simple nested loops that you don't need anything special to do it with.