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

added example 2.9.1_21

parent c9b33f46
Loading
Loading
Loading
Loading

2.9.1_21.pl

0 → 100644
+26 −0
Original line number Original line Diff line number Diff line
% nacteni:
/* ['2.9.1_21.pl']. */


path(A,Z,Graph,Cesta) :- path1(A,[Z],Graph,Cesta).

path1(A,[A|Cesta1],_,[A|Cesta1]).
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)
                                    ;member(e(Y,X),Edges).

graph([a,b,c,d],[e(a,b),e(b,d),e(b,c),e(c,d)]).

% demonstracni vypis

start:- 
    write('Cesta v grafu'),nl, nl,
    write('path(a, c, graph, Cesta) : '),
    graph(Nodes,Edges),
    path(a, c, graph(Nodes,Edges), Cesta),
    write(Cesta), nl, nl.

?-start.

2.9.1_21.py

0 → 100644
+48 −0
Original line number Original line Diff line number Diff line
#!/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
    else:
        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:
        return
    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)))

2.9.1_21.py.out

0 → 100644
+3 −0
Original line number Original line Diff line number Diff line
Cesta v grafu

next(path("a", "c", _graph)) : [a, b, c]
+3 −3
Original line number Original line Diff line number Diff line
.PHONY: tests output pylint2 pylint3
.PHONY: tests output pylint2 pylint3


IGNORED_PYLINT_TESTS=invalid-name missing-docstring \
IGNORED_PYLINT_TESTS=invalid-name missing-docstring \
	redefined-variable-type too-few-public-methods
	redefined-variable-type too-few-public-methods duplicate-code
IGNORED_PYLINT2_TESTS=superfluous-parens duplicate-code
IGNORED_PYLINT2_TESTS=superfluous-parens
IGNORED_PYLINT3_TESTS=duplicate-code
IGNORED_PYLINT3_TESTS=


tests:
tests:
	# all tests
	# all tests