Last active
February 27, 2019 10:31
-
-
Save dmarcosl/21ee4741b5df97a91f22e5ab5a6ea9d6 to your computer and use it in GitHub Desktop.
My solution to the "Haruhi Problem" http://mathsci.wikia.com/wiki/The_Haruhi_Problem
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
You have an n episode TV series. You want to watch the episodes in every order possible. | |
What is the least number of episodes that you would have to watch? | |
Overlapping is not allowed. For example, in the case of n=2, watching episode 1, then 2, then 1 again, | |
would not fit the criteria. | |
The orders must be continuous. For example, (1,3,2) does NOT contain the sequence (1,2,3) | |
""" | |
NUMBER_OF_EPISODES = 3 | |
def recursive_haruhi(episode_list, order): | |
""" Recursive method that creates a recursive loop in each position of the order to fill it with the | |
remaining episodes that are being added to the order en each iteration | |
:param episode_list: List of all remaining episodes | |
:param order: List of episodes in order being generated | |
""" | |
# Create a new instance of the episode list with the remaining episodes | |
new_episode_list = list(episode_list) | |
# Get the last episode added to the order and remove it from the new list | |
if order: | |
new_episode_list.pop(new_episode_list.index(order[-1])) | |
# When all the episodes are in the order, add it to the list | |
if len(new_episode_list) == 0: | |
order_list.append(order) | |
# Else, iterate the episodes and recall this function | |
else: | |
for obj in list(new_episode_list): | |
# Create a new instance of the order and add the new episode | |
new_order = list(order) | |
new_order.append(obj) | |
# Call this method again and continue until all the episode list are in the order | |
recursive_haruhi(new_episode_list, new_order) | |
# List that will contain all possible orders in which watch the episodes | |
order_list = list() | |
# Iterate the episodes | |
recursive_haruhi(range(NUMBER_OF_EPISODES), []) | |
# Print the orders | |
for order in order_list: | |
print(order) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment