Commit 01a32079 authored by Vít Novotný's avatar Vít Novotný
Browse files

added examples 1.2_16 and 2.5.1_10

parent 3dc228e3
% nacteni:
/* ['1.2_16.pl']. */
tiskniseznam(S):-write('seznam=['),nl,tiskniseznam(S,1).
tiskniseznam([],_):-write(']'),nl.
tiskniseznam([H|T],N):- tab(4),write(N),write(': '),write(H),nl,N1 is N+1,tiskniseznam(T,N1).
% demonstracni vypis
% abychom se vyhli varovanim "Redefined static procedure ..."
:- dynamic
start/0.
start:-
write('Program vytiskne zadany seznam. Napr.:'),nl,nl,
write('Vysledek dotazu "tiskniseznam([a, b, c])." je:'),nl,
tiskniseznam([a, b, c]),nl,
write('Vysledek dotazu "tiskniseznam([a, b, [c1, c2, c3], d, `mezi d a e`, e])." je:'),nl,
tiskniseznam([a, b, [c1, c2, c3], d, 'mezi d a e', e]).
?-start.
:- retractall(write_all_X/3).
:- retractall(start/0).
#!/usr/bin/env python
# encoding=utf-8 (pep 0263)
def tiskniseznam(xs):
i = 1
print("seznam=[")
while xs != None:
print(" %d: %s" % (i, xs[0]))
i = i + 1
xs = xs[1]
print("]")
# demonstracni vypis
if __name__ == "__main__":
print("Program vytiskne zadany seznam. Napr.:\n")
print("Vysledek volani tiskniseznam(('a', ('b', ('c', None)))) je:\n")
tiskniseznam(('a', ('b', ('c', None))))
print("\n\nVysledek volani tiskniseznam(('a', ('b', (['c1', 'c2', 'c3'], " +
"('d', ('mezi d a e', ('e', None))))))). je:\n")
tiskniseznam(('a', ('b', (['c1', 'c2', 'c3'], ('d', ('mezi d a e', ('e', None)))))))
print("")
% nacteni:
/* ['2.5.1_10.pl']. */
:- retractall(start/0).
:- retractall(qsort/2).
:- retractall(divide/4).
:- dynamic
qsort/2,
divide/4.
qsort([],[]) :- !.
qsort([H],[H]) :- !.
qsort([H|T],L) :- divide(H,T,M,V),
qsort(M,M1), qsort(V,V1),
append(M1,[H|V1],L).
divide(_,[],[],[]) :- !.
divide(H,[K|T],[K|M],V) :- K=<H, !, divide(H,T,M,V).
divide(H,[K|T],M,[K|V]) :- K>H, divide(H,T,M,V).
% demonstracni vypis
% abychom se vyhli varovanim "Redefined static procedure ..."
:- dynamic
start/0.
start:-
write('Radici algoritmus QuickSort'),nl,nl,
write('Vysledek volani "qsort([5, 2, 8, 2, 654, 8, 3, 4], L)":'),nl,
qsort([5, 2, 8, 2, 654, 8, 3, 4], L), write('L = '),write(L),nl.
?-start.
:- retractall(start/0).
#!/usr/bin/env python
# encoding=utf-8 (pep 0263)
from linked_lists import LinkedList, Cons, Nil
def append(xs, ys):
if xs == Nil:
return ys
else:
return Cons(xs.head, append(xs.tail, ys))
def qsort(xs):
if xs == Nil:
return Nil
if xs.tail == Nil:
return xs
ms, vs = divide(xs.head, xs.tail)
return append(qsort(ms), Cons(xs.head, qsort(vs)))
def divide(h, xs):
if xs == Nil:
return (Nil, Nil)
ms, vs = divide(h, xs.tail)
if xs.head <= h:
return (Cons(xs.head, ms), vs)
else:
return (ms, Cons(xs.head, vs))
# demonstracni vypis
if __name__ == "__main__":
print("Radici algoritmus QuickSort\n")
print("qsort(LinkedList(5, 2, 8, 2, 654, 8, 3, 4)): \n\t%s\n" % \
qsort(LinkedList(5, 2, 8, 2, 654, 8, 3, 4)))
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