I want to write a function permutations(route, cities)
. It should take a list ( cities
), append every possible permutations of the cities
to the route
list, and print each permutation in route
on a new line. Every permutation must start with the first city, ie "Boston".
I am using recursion for this implementation, but can't get it to work.
def permutations(route, cities):
def recurs(cities_temp):
if len(cities_temp) == 0:
return []
elif len(cities_temp) == 1:
return [cities_temp]
else:
route_temp = []
for i in range(len(cities_temp)):
x = cities_temp[i] #x is item i in the input list
y = cities_temp[:i] + cities_temp[1+i:] #y is the remaining (everything but item i)
for j in recurs(y):
route_temp.append([x] + j)
return route_temp
route = recurs(cities)
print(' '.join([city_names[i] for i in route]))
city_names = ["Boston", "Seattle", "Chicago", "Dallas"]
permutations([0], list(range(1, len(city_names)))) #calling the permutations function
Can you all please take a look at it and let me know what I am doing wrong?
I don't know what's wrong with the code in your question, but think you're "reinventing-the-wheel" since you can make use of the itertools.permutations() function to get them:
from itertools import permutations
from pprint import pprint
city_names = ["Boston", "Seattle", "Chicago", "Dallas"]
permutes = [(city_names[0],)+p for p in permutations(city_names[1:])]
pprint(permutes)
Output:
[('Boston', 'Seattle', 'Chicago', 'Dallas'),
('Boston', 'Seattle', 'Dallas', 'Chicago'),
('Boston', 'Chicago', 'Seattle', 'Dallas'),
('Boston', 'Chicago', 'Dallas', 'Seattle'),
('Boston', 'Dallas', 'Seattle', 'Chicago'),
('Boston', 'Dallas', 'Chicago', 'Seattle')]
I had ignored this exercise until I am in a better mindset but finally was able to solve it just like the instructor wished.
Here is the code. Thanks for all the responses to my original question.
portnames = ["PAN", "AMS", "CAS", "NYC", "HEL"]
def permutations(route, ports):
n = len(ports) - 1
def recursion(ports):
if len(ports) == 1:
return [ports]
else:
lst = []
for i in range(len(ports)):
x = [ports[i]]
xs = ports[:i] + ports[i+1:]
for j in recursion(xs):
if len(j) == n:
lst.append(route + x + j)
else:
lst.append(x + j)
return lst
for p in recursion(list(range(1, len(portnames)))):
print(' '.join([portnames[i] for i in p]))
return recursion(ports)
permutations([0], list(range(1, len(portnames))))
The problem was with mapping the permuted index with the city name..I've edited the code using map function. Have a look.
def permutations(route, cities):
def recurs(cities_temp):
if len(cities_temp) == 0:
return []
elif len(cities_temp) == 1:
return [cities_temp]
else:
route_temp = []
for i in range(len(cities_temp)):
x = cities_temp[i] #x is item i in the input list
y = cities_temp[:i] + cities_temp[1+i:] #y is the remaining (everything but item I)
for j in recurs(y):
route_temp.append([x] + j)
return route_temp
route = recurs(cities)
# mapping index with city names from city_names list
mapped_route = [list(map(lambda x : city_names[x],r)) for r in route]
# adding boston in front of every permutation
final_route = list(map(lambda x : "Boston "+x,[' '.join(r) for r in mapped_route]))
print(final_route)
city_names = ["Boston", "Seattle", "Chicago", "Dallas"]
permutations([0], list(range(1, len(city_names))))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.