Commit 6d4c7dae authored by Vít Novotný's avatar Vít Novotný
Browse files

added example 2.9.1_21

parent c9b33f46
% nacteni:
/* ['']. */
path(A,Z,Graph,Cesta) :- path1(A,[Z],Graph,Cesta).
path1(A,[Y|Cesta1],Graph,Cesta) :- adjacent(X,Y,Graph),
\+ member(X,Cesta1),path1(A,[X,Y|Cesta1],Graph,Cesta).
adjacent(X,Y,graph(_,Edges)) :- member(e(X,Y),Edges)
% demonstracni vypis
write('Cesta v grafu'),nl, nl,
write('path(a, c, graph, Cesta) : '),
path(a, c, graph(Nodes,Edges), Cesta),
write(Cesta), nl, nl.
#!/usr/bin/env python
# encoding=utf-8 (pep 0263)
from linked_lists import LinkedList, Cons, Nil
def path(a, z, graph):
for cesta in path1(a, LinkedList(z), graph, 1):
yield cesta
def path1(a, akumulator_cesty, graph, depth):
if akumulator_cesty.head == a:
yield akumulator_cesty
y = akumulator_cesty.head
for x in adjacent_anyX(y, graph):
if not member(x, akumulator_cesty):
for cesta in path1(a, Cons(x, akumulator_cesty), graph, depth+1):
yield cesta
def adjacent_anyX(y, graph):
_, edges = graph
for a, b in member_anyX(edges):
if y == a:
yield b
if y == b:
yield a
def member(x, xs):
if xs == Nil:
return False
if x == xs.head:
return True
return member(x, xs.tail)
def member_anyX(xs):
if xs == Nil:
yield xs.head
for x in member_anyX(xs.tail):
yield x
_graph = (LinkedList("a", "b", "c", "d"),
LinkedList(("a", "b"), ("b", "d"), ("b", "c"), ("c", "d")))
# demonstracni vypis
if __name__ == "__main__":
print('Cesta v grafu\n')
print('next(path("a", "c", _graph)) : %s' % next(path("a", "c", _graph)))
Cesta v grafu
next(path("a", "c", _graph)) : [a, b, c]
.PHONY: tests output pylint2 pylint3
IGNORED_PYLINT_TESTS=invalid-name missing-docstring \
redefined-variable-type too-few-public-methods
IGNORED_PYLINT2_TESTS=superfluous-parens duplicate-code
redefined-variable-type too-few-public-methods duplicate-code
# all tests
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment