Permutation of all characters in strings
Question:
I am trying to generate a sequence using characters in a different string.
Example:
- Input:
["abcdef", "xyz", "-/", "98765"]
- Output:
ax-9,ax-8,ax-7,ax-6,ax-5,ax/9,ax/8,ax/7,ax/6,ax/5,ay-9,ay-8,ay-7,ay-6,ay-5
Code:
def print_taxi_serial_numbers(params, code, level, offsets, n):
params_length = len(params)
print("TT11 :: ", code, level, offsets)
if level == params_length-1:
for c in params[level]:
print("{}{}".format(code,c))
else:
while True:
print("LEVEL :: ", level)
if level == 0:
code = ""
code = "{}{}".format(code, params[level][offsets[level]])
offsets[level] += 1
level = level + 1
if level >= params_length:
print("LEVEL1 :: ", level, " params_length :: ", params_length, " yy :: ", offsets, " zz ", params)
return
print_taxi_serial_numbers(params, code, level, offsets, n)
print("TT :: ", code, level, offsets)
#Function Call
params = ["abcdef", "xyz", "-/", "98765"]
print_taxi_serial_numbers(params, "", 0, len(params)*[0], 10)
I am making some mistake in resetting the params.
Answers:
You don’t really need recursion and carry around a bunch of arguments. Just use the cartesian product (for which there are utils):
from itertools import product
def print_taxi_serial_numbers(params):
for p in product(*params):
print(''.join(p))
>>> print_taxi_serial_numbers(["abcdef", "xyz", "-/", "98765"])
ax-9
ax-8
ax-7
ax-6
ax-5
# ...
If you are hell-bent on doing it yourself, you can still keep it simple:
def taxi_serial_numbers(params):
if not params:
return [""]
result = []
first, *params = params
for char in first:
for tsn in taxi_serial_numbers(params):
result.append(char + tsn)
return result
>>> taxi_serial_numbers(["abcdef", "xyz", "-/", "98765"])
['ax-9', 'ax-8', 'ax-7', 'ax-6', 'ax-5', 'ax/9', 'ax/8', ... ]
Note that the recursion is much easier when your function returns something meaningful instead of just having side-effects (like console prints). You can still use the function return value to get your print display:
for tsn in print_taxi_serial_numbers(params):
print(tsn)
I am trying to generate a sequence using characters in a different string.
Example:
- Input:
["abcdef", "xyz", "-/", "98765"]
- Output:
ax-9,ax-8,ax-7,ax-6,ax-5,ax/9,ax/8,ax/7,ax/6,ax/5,ay-9,ay-8,ay-7,ay-6,ay-5
Code:
def print_taxi_serial_numbers(params, code, level, offsets, n):
params_length = len(params)
print("TT11 :: ", code, level, offsets)
if level == params_length-1:
for c in params[level]:
print("{}{}".format(code,c))
else:
while True:
print("LEVEL :: ", level)
if level == 0:
code = ""
code = "{}{}".format(code, params[level][offsets[level]])
offsets[level] += 1
level = level + 1
if level >= params_length:
print("LEVEL1 :: ", level, " params_length :: ", params_length, " yy :: ", offsets, " zz ", params)
return
print_taxi_serial_numbers(params, code, level, offsets, n)
print("TT :: ", code, level, offsets)
#Function Call
params = ["abcdef", "xyz", "-/", "98765"]
print_taxi_serial_numbers(params, "", 0, len(params)*[0], 10)
I am making some mistake in resetting the params.
You don’t really need recursion and carry around a bunch of arguments. Just use the cartesian product (for which there are utils):
from itertools import product
def print_taxi_serial_numbers(params):
for p in product(*params):
print(''.join(p))
>>> print_taxi_serial_numbers(["abcdef", "xyz", "-/", "98765"])
ax-9
ax-8
ax-7
ax-6
ax-5
# ...
If you are hell-bent on doing it yourself, you can still keep it simple:
def taxi_serial_numbers(params):
if not params:
return [""]
result = []
first, *params = params
for char in first:
for tsn in taxi_serial_numbers(params):
result.append(char + tsn)
return result
>>> taxi_serial_numbers(["abcdef", "xyz", "-/", "98765"])
['ax-9', 'ax-8', 'ax-7', 'ax-6', 'ax-5', 'ax/9', 'ax/8', ... ]
Note that the recursion is much easier when your function returns something meaningful instead of just having side-effects (like console prints). You can still use the function return value to get your print display:
for tsn in print_taxi_serial_numbers(params):
print(tsn)