Skip to content

Instantly share code, notes, and snippets.

@toanant
Created May 10, 2016 04:04
Show Gist options
  • Save toanant/7f8da52cb59b7e494428560851f07b8d to your computer and use it in GitHub Desktop.
Save toanant/7f8da52cb59b7e494428560851f07b8d to your computer and use it in GitHub Desktop.
# Enter your code here. Read input from STDIN. Print output to STDOUT
import copy
all_valid_routes = {
'A': ['AB', ],
'B': ['BC', 'BC', 'BC', 'BD', 'BD', 'BA'],
'C': ['CD', 'CD', 'CD', 'CB', 'CB', 'CB'],
'D': ['DE', 'DC', 'DC', 'DC', 'DB', 'DB'],
'E': ['ED'],
}
def check_for_valid_routes(string):
valid_routes = copy.deepcopy(all_valid_routes)
string_length = len(string)
is_valid = True
path_count = 0
for i in range(string_length):
starting_node = string[i]
edge = string[i: i+2]
reverse_edge = edge[::-1]
ending_node = reverse_edge[0]
available_valid_path = valid_routes[starting_node]
if len(edge) > 1:
if edge in available_valid_path:
path_count += 1
_ = available_valid_path.pop(available_valid_path.index(edge))
valid_routes[starting_node] = available_valid_path
if reverse_edge in valid_routes[ending_node]:
available_valid_path = valid_routes[ending_node]
_ = available_valid_path.pop(
available_valid_path.index(reverse_edge))
valid_routes[ending_node] = available_valid_path
else:
is_valid = False
print(is_valid)
print(path_count)
if __name__ == '__main__':
string = input()
check_for_valid_routes(string)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment