Skip to content

Instantly share code, notes, and snippets.

@riceissa
Created September 20, 2020 05:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save riceissa/f05d72e2adffa3cbeb6587fe6439bcf3 to your computer and use it in GitHub Desktop.
Save riceissa/f05d72e2adffa3cbeb6587fe6439bcf3 to your computer and use it in GitHub Desktop.
Finding a match in PCP problem, Sipser problem 5.3
#!/usr/bin/env python3
dominoes = [("ab", "abab"), ("b", "a"), ("aba", "b"), ("aa", "a")]
def top_str(state):
return "".join(t[0] for t in state)
def bottom_str(state):
return "".join(t[1] for t in state)
def check_state(state):
"""Return True if state is valid, and False if state is invalid (contains a
top and bottom character that does not match)."""
top = top_str(state)
bottom = bottom_str(state)
min_length = min(len(top), len(bottom))
return top[:min_length] == bottom[:min_length]
def explore(state, depth):
for tile in dominoes:
new_state = state + [tile]
if not check_state(new_state):
continue
if top_str(new_state) == bottom_str(new_state):
print(new_state)
if depth >= 0:
# print("depth", depth, "; state", new_state)
explore(new_state, depth - 1)
explore([], 10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment