Python:排列
Python: Permutations
我正在研究 a problem,我想在其中获取所有排列,但不允许修改打印语句。我也不允许使用 itertools
或其他软件包。
说明:
In order for the program to list out all possible routes it needs some work. Fix the program by adding an if statement that checks that the route includes all of the ports. In other words, check that each of the numbers 0, 1, 2, 3, 4 are included in the list route. Note that it isn't necessary to check that no port appears twice because if that were the case, then the route couldn't include all the five ports.
You do not need to change the print statement.
这是我的代码,但是它不起作用。如何解决?
def main():
portnames = ["PAN", "AMS", "CAS", "NYC", "HEL"]
# don't change this bit - provided in exercise
port1 = 0
for port2 in range(1, 5):
for port3 in range(1, 5):
for port4 in range(1, 5):
for port5 in range(1, 5):
route = [port1, port2, port3, port4, port5]
# instructions to add if statement that checks that the route includes all of the ports
# if "PAN" in route and "AMS" in route and "CAS" in route and "NYC" in route and "HEL" in route:
if all([val in route for val in portnames]):
# do not modify the print statement
print(' '.join([portnames[i] for i in route]))
else:
continue
main()
在if
条件前添加如下print
语句显示错误:
# code before
print(route)
if all([val in route for val in portnames]):
# code after
# Output:
# [0, 4, 3, 2, 2]
# [0, 4, 3, 2, 3]
# [0, 4, 3, 2, 4]
# ...
route
由range
生成的整数组成。您可以改为 if
声明:
if all([portnames.index(val) in route for val in portnames]):
输出:
PAN AMS CAS NYC HEL
PAN AMS CAS HEL NYC
PAN AMS NYC CAS HEL
PAN AMS NYC HEL CAS
PAN AMS HEL CAS NYC
PAN AMS HEL NYC CAS
PAN CAS AMS NYC HEL
PAN CAS AMS HEL NYC
PAN CAS NYC AMS HEL
PAN CAS NYC HEL AMS
PAN CAS HEL AMS NYC
PAN CAS HEL NYC AMS
PAN NYC AMS CAS HEL
PAN NYC AMS HEL CAS
PAN NYC CAS AMS HEL
PAN NYC CAS HEL AMS
PAN NYC HEL AMS CAS
PAN NYC HEL CAS AMS
PAN HEL AMS CAS NYC
PAN HEL AMS NYC CAS
PAN HEL CAS AMS NYC
PAN HEL CAS NYC AMS
PAN HEL NYC AMS CAS
PAN HEL NYC CAS AMS
你可以这样做而不是列表理解 -
if set(range(len(portnames))).issubset(route)
路由集(从 0-4 开始的索引)必须完全包含端口名称的索引集 (0-4)。如果这是 True
,那么每个端口名都将存在于路由中并因此被打印出来。
portnames = ["PAN", "AMS", "CAS", "NYC", "HEL"]
# don't change this bit - provided in exercise
port1 = 0
for port2 in range(1, 5):
for port3 in range(1, 5):
for port4 in range(1, 5):
for port5 in range(1, 5):
route = [port1, port2, port3, port4, port5]
# instructions to add if statement that checks that the route includes all of the ports
# if "PAN" in route and "AMS" in route and "CAS" in route and "NYC" in route and "HEL" in route:
if set(range(len(portnames))).issubset(route):
# do not modify the print statement
print(' '.join([portnames[i] for i in route]))
else:
continue
PAN AMS CAS NYC HEL
PAN AMS CAS HEL NYC
PAN AMS NYC CAS HEL
PAN AMS NYC HEL CAS
PAN AMS HEL CAS NYC
PAN AMS HEL NYC CAS
PAN CAS AMS NYC HEL
PAN CAS AMS HEL NYC
PAN CAS NYC AMS HEL
PAN CAS NYC HEL AMS
PAN CAS HEL AMS NYC
PAN CAS HEL NYC AMS
PAN NYC AMS CAS HEL
PAN NYC AMS HEL CAS
PAN NYC CAS AMS HEL
PAN NYC CAS HEL AMS
PAN NYC HEL AMS CAS
PAN NYC HEL CAS AMS
PAN HEL AMS CAS NYC
PAN HEL AMS NYC CAS
PAN HEL CAS AMS NYC
PAN HEL CAS NYC AMS
PAN HEL NYC AMS CAS
PAN HEL NYC CAS AMS
def main():
portnames = ["PAN", "AMS", "CAS", "NYC", "HEL"]
# https://sea-distances.org/
# nautical miles converted to km
D = [
[0,8943,8019,3652,10545],
[8943,0,2619,6317,2078],
[8019,2619,0,5836,4939],
[3652,6317,5836,0,7825],
[10545,2078,4939,7825,0]
]
# https://timeforchange.org/co2-emissions-shipping-goods
# assume 20g per km per metric ton (of pineapples)
co2 = 0.020
route = []
port1 = 0
for port2 in range(1,5):
for port3 in range(1,5):
for port4 in range(1,5):
for port5 in range(1,5):
result = [port1, port2, port3, port4, port5]
if 0 in result:
if 1 in result:
if 2 in result:
if 3 in result:
if 4 in result:
route.append(result)
for singleRoute in route:
distance = D[singleRoute[0]][singleRoute[1]] + D[singleRoute[1]][singleRoute[2]] + D[singleRoute[2]][singleRoute[3]] + D[singleRoute[3]][singleRoute[4]]
emissions = distance * co2
print(' '.join([portnames[i] for i in singleRoute]) + " %.1f kg" % emissions)
main()
专业提示:您可以使用 Python 集合轻松检查路由是否包含所有端口(编号为 0、1、...、4)。示例解决方案中笨拙的 if 语句可以替换为更优雅的语句:
if set(route) == set(range(5)):
我正在研究 a problem,我想在其中获取所有排列,但不允许修改打印语句。我也不允许使用 itertools
或其他软件包。
说明:
In order for the program to list out all possible routes it needs some work. Fix the program by adding an if statement that checks that the route includes all of the ports. In other words, check that each of the numbers 0, 1, 2, 3, 4 are included in the list route. Note that it isn't necessary to check that no port appears twice because if that were the case, then the route couldn't include all the five ports.
You do not need to change the print statement.
这是我的代码,但是它不起作用。如何解决?
def main():
portnames = ["PAN", "AMS", "CAS", "NYC", "HEL"]
# don't change this bit - provided in exercise
port1 = 0
for port2 in range(1, 5):
for port3 in range(1, 5):
for port4 in range(1, 5):
for port5 in range(1, 5):
route = [port1, port2, port3, port4, port5]
# instructions to add if statement that checks that the route includes all of the ports
# if "PAN" in route and "AMS" in route and "CAS" in route and "NYC" in route and "HEL" in route:
if all([val in route for val in portnames]):
# do not modify the print statement
print(' '.join([portnames[i] for i in route]))
else:
continue
main()
在if
条件前添加如下print
语句显示错误:
# code before
print(route)
if all([val in route for val in portnames]):
# code after
# Output:
# [0, 4, 3, 2, 2]
# [0, 4, 3, 2, 3]
# [0, 4, 3, 2, 4]
# ...
route
由range
生成的整数组成。您可以改为 if
声明:
if all([portnames.index(val) in route for val in portnames]):
输出:
PAN AMS CAS NYC HEL
PAN AMS CAS HEL NYC
PAN AMS NYC CAS HEL
PAN AMS NYC HEL CAS
PAN AMS HEL CAS NYC
PAN AMS HEL NYC CAS
PAN CAS AMS NYC HEL
PAN CAS AMS HEL NYC
PAN CAS NYC AMS HEL
PAN CAS NYC HEL AMS
PAN CAS HEL AMS NYC
PAN CAS HEL NYC AMS
PAN NYC AMS CAS HEL
PAN NYC AMS HEL CAS
PAN NYC CAS AMS HEL
PAN NYC CAS HEL AMS
PAN NYC HEL AMS CAS
PAN NYC HEL CAS AMS
PAN HEL AMS CAS NYC
PAN HEL AMS NYC CAS
PAN HEL CAS AMS NYC
PAN HEL CAS NYC AMS
PAN HEL NYC AMS CAS
PAN HEL NYC CAS AMS
你可以这样做而不是列表理解 -
if set(range(len(portnames))).issubset(route)
路由集(从 0-4 开始的索引)必须完全包含端口名称的索引集 (0-4)。如果这是 True
,那么每个端口名都将存在于路由中并因此被打印出来。
portnames = ["PAN", "AMS", "CAS", "NYC", "HEL"]
# don't change this bit - provided in exercise
port1 = 0
for port2 in range(1, 5):
for port3 in range(1, 5):
for port4 in range(1, 5):
for port5 in range(1, 5):
route = [port1, port2, port3, port4, port5]
# instructions to add if statement that checks that the route includes all of the ports
# if "PAN" in route and "AMS" in route and "CAS" in route and "NYC" in route and "HEL" in route:
if set(range(len(portnames))).issubset(route):
# do not modify the print statement
print(' '.join([portnames[i] for i in route]))
else:
continue
PAN AMS CAS NYC HEL
PAN AMS CAS HEL NYC
PAN AMS NYC CAS HEL
PAN AMS NYC HEL CAS
PAN AMS HEL CAS NYC
PAN AMS HEL NYC CAS
PAN CAS AMS NYC HEL
PAN CAS AMS HEL NYC
PAN CAS NYC AMS HEL
PAN CAS NYC HEL AMS
PAN CAS HEL AMS NYC
PAN CAS HEL NYC AMS
PAN NYC AMS CAS HEL
PAN NYC AMS HEL CAS
PAN NYC CAS AMS HEL
PAN NYC CAS HEL AMS
PAN NYC HEL AMS CAS
PAN NYC HEL CAS AMS
PAN HEL AMS CAS NYC
PAN HEL AMS NYC CAS
PAN HEL CAS AMS NYC
PAN HEL CAS NYC AMS
PAN HEL NYC AMS CAS
PAN HEL NYC CAS AMS
def main():
portnames = ["PAN", "AMS", "CAS", "NYC", "HEL"]
# https://sea-distances.org/
# nautical miles converted to km
D = [
[0,8943,8019,3652,10545],
[8943,0,2619,6317,2078],
[8019,2619,0,5836,4939],
[3652,6317,5836,0,7825],
[10545,2078,4939,7825,0]
]
# https://timeforchange.org/co2-emissions-shipping-goods
# assume 20g per km per metric ton (of pineapples)
co2 = 0.020
route = []
port1 = 0
for port2 in range(1,5):
for port3 in range(1,5):
for port4 in range(1,5):
for port5 in range(1,5):
result = [port1, port2, port3, port4, port5]
if 0 in result:
if 1 in result:
if 2 in result:
if 3 in result:
if 4 in result:
route.append(result)
for singleRoute in route:
distance = D[singleRoute[0]][singleRoute[1]] + D[singleRoute[1]][singleRoute[2]] + D[singleRoute[2]][singleRoute[3]] + D[singleRoute[3]][singleRoute[4]]
emissions = distance * co2
print(' '.join([portnames[i] for i in singleRoute]) + " %.1f kg" % emissions)
main()
专业提示:您可以使用 Python 集合轻松检查路由是否包含所有端口(编号为 0、1、...、4)。示例解决方案中笨拙的 if 语句可以替换为更优雅的语句:
if set(route) == set(range(5)):