Skip to content

Instantly share code, notes, and snippets.

@JackHowa
Last active November 8, 2020 19:15
Show Gist options
  • Save JackHowa/99cbf637f06bd8a01d8723fe547b923f to your computer and use it in GitHub Desktop.
Save JackHowa/99cbf637f06bd8a01d8723fe547b923f to your computer and use it in GitHub Desktop.
def main():
portnames = ["PAN", "AMS", "CAS", "NYC", "HEL"]
# generate list of 1, 2, 3, 4
check = range(1,5)
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]
# all possible included in list of 0..4 possibilities
if set(check).issubset(set(route)):
# do not modify the print statement
print(' '.join([portnames[i] for i in route]))
main()
@JackHowa
Copy link
Author

JackHowa commented Nov 8, 2020

Adding the following long if statement

if 0 in route and 1 in route and 2 in route and 3 in route and 4 in route:
before the print statement does the job. You don't really have to check that 0 is included because it is always the starting point but it's good programming style to rather do more checks than too few in order to ensure that the program works even if we modify it later.
a pro style tip: you can check that the route includes all ports (numbered 0, 1, ..., 4) easily by using Python sets. The clumsy if statement in the example solution can be replaced by the much more elegant one:

if set(route) == set(range(5)):

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment