Graph Search: First working version & Shuffle problem
This commit is contained in:
@@ -8,14 +8,36 @@ Honor Code and Acknowledgments:
|
|||||||
Comments here on your code and submission.
|
Comments here on your code and submission.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from collections import deque
|
from collections import defaultdict, deque
|
||||||
|
|
||||||
|
|
||||||
def whatever_first_search(
|
def whatever_first_search(graph: dict[str, set[str]], start: str, stop: str):
|
||||||
graph: dict[str, set[str]], start: str, stop: str
|
|
||||||
) -> list[str]:
|
|
||||||
|
|
||||||
return []
|
bag: deque[tuple[str | None, str]] = deque()
|
||||||
|
bag.append((None, start))
|
||||||
|
visited: set[str] = set()
|
||||||
|
parent: dict[str, str | None] = {}
|
||||||
|
|
||||||
|
while len(bag) != 0:
|
||||||
|
parent_node, u = bag.pop()
|
||||||
|
if u in visited:
|
||||||
|
continue
|
||||||
|
visited.add(u)
|
||||||
|
parent[u] = parent_node
|
||||||
|
|
||||||
|
if u == stop:
|
||||||
|
path: list[str] = []
|
||||||
|
while u is not None:
|
||||||
|
path.append(u)
|
||||||
|
u = parent[u]
|
||||||
|
path.reverse()
|
||||||
|
return path
|
||||||
|
|
||||||
|
for v in graph[u]:
|
||||||
|
if v not in visited:
|
||||||
|
bag.append((u, v))
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
# All modules for CS 412 must include a main method that allows it
|
# All modules for CS 412 must include a main method that allows it
|
||||||
@@ -23,21 +45,18 @@ def whatever_first_search(
|
|||||||
def main():
|
def main():
|
||||||
n: int = int(input())
|
n: int = int(input())
|
||||||
|
|
||||||
graph: dict[str, set[str]] = {}
|
graph: dict[str, set[str]] = defaultdict(set)
|
||||||
|
|
||||||
for _ in range(0, n):
|
for _ in range(0, n):
|
||||||
u, v = input().split()
|
u, v = input().split()
|
||||||
|
|
||||||
if u not in graph:
|
|
||||||
graph[u] = set([v])
|
|
||||||
else:
|
|
||||||
graph[u].add(v)
|
graph[u].add(v)
|
||||||
|
graph[v].add(u)
|
||||||
|
|
||||||
target = input().split()
|
target = input().split()
|
||||||
|
|
||||||
path = whatever_first_search(graph, target[0], target[1])
|
path = whatever_first_search(graph, target[0], target[1])
|
||||||
|
|
||||||
if len(path) == 0:
|
if path is None:
|
||||||
print("no route possible")
|
print("no route possible")
|
||||||
else:
|
else:
|
||||||
print(" ".join(path))
|
print(" ".join(path))
|
||||||
|
|||||||
3
Shuffle/inputs/sample1.txt
Normal file
3
Shuffle/inputs/sample1.txt
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
ACF
|
||||||
|
BCF
|
||||||
|
ABCCFF
|
||||||
3
Shuffle/inputs/sample2.txt
Normal file
3
Shuffle/inputs/sample2.txt
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
ACF
|
||||||
|
BCF
|
||||||
|
ABFCCF
|
||||||
54
Shuffle/shuffle.py
Normal file
54
Shuffle/shuffle.py
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
"""
|
||||||
|
name: Nicholas Tamassia
|
||||||
|
|
||||||
|
Honor Code and Acknowledgments:
|
||||||
|
|
||||||
|
This work complies with the JMU Honor Code.
|
||||||
|
|
||||||
|
Comments here on your code and submission.
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
def valid_shuffle(a: str, b: str, c: str) -> bool:
|
||||||
|
|
||||||
|
memo: list[list[bool | None]] = [
|
||||||
|
([None] * (len(a) + 1)) for _ in range(0, len(b) + 1)
|
||||||
|
]
|
||||||
|
|
||||||
|
memo[len(a)][len(b)] = True
|
||||||
|
|
||||||
|
def recurse(i: int, j: int) -> bool:
|
||||||
|
if i >= len(a) or j >= len(b):
|
||||||
|
return False
|
||||||
|
|
||||||
|
k: int = i + j
|
||||||
|
if k == (len(c) - 2):
|
||||||
|
return True
|
||||||
|
|
||||||
|
a_val: bool | None = memo[i + 1][j]
|
||||||
|
if a_val is None:
|
||||||
|
a_val = recurse(i + 1, j) if a[i] == c[k] else False
|
||||||
|
|
||||||
|
b_val: bool | None = memo[i][j + 1]
|
||||||
|
if b_val is None:
|
||||||
|
b_val = recurse(i, j + 1) if b[j] == c[k] else False
|
||||||
|
|
||||||
|
memo[i][j] = a_val or b_val
|
||||||
|
|
||||||
|
return a_val or b_val
|
||||||
|
|
||||||
|
return recurse(0, 0)
|
||||||
|
|
||||||
|
|
||||||
|
# All modules for CS 412 must include a main method that allows it
|
||||||
|
# to imported and invoked from other python scripts
|
||||||
|
def main():
|
||||||
|
a: str = input()
|
||||||
|
b: str = input()
|
||||||
|
c: str = input()
|
||||||
|
|
||||||
|
print(valid_shuffle(a, b, c))
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
Reference in New Issue
Block a user