Commit 3dc228e3 authored by Vít Novotný's avatar Vít Novotný
Browse files

rewrote the examples using linked lists

parent 2cee916f
......@@ -5,21 +5,14 @@
# klici hasove tabulky jsou jmena potomku, hodnotami jmena rodicu
otec = dict(dana="milan", petr="milan", david="jan")
matka = dict(dana="pavla", petr="pavla", david="jana")
potomci = dict()
def _init_potomci():
for y in otec:
potomci[y] = True
for y in matka:
potomci[y] = True
_init_potomci()
potomci = set(otec.keys() + matka.keys())
# pravidla
def rodic_testuj(x, y):
def rodic(x, y):
return y in otec and otec[y] == x or \
y in matka and matka[y] == x
def rodic_generuj_Xs(y):
def rodic_anyX(y):
xs = []
if y in otec:
xs.append(otec[y])
......@@ -27,21 +20,21 @@ def rodic_generuj_Xs(y):
xs.append(matka[y])
return xs
def sourozenci_testuj(x, y):
def sourozenci(x, y):
return x in otec and y in otec and otec[x] == otec[y] and x != y and \
x in matka and y in matka and matka[x] == matka[y]
def sourozenci_generuj_Ys(x):
def sourozenci_anyY(x):
ys = []
for y in potomci:
if sourozenci_testuj(x, y):
if sourozenci(x, y):
ys.append(y)
return ys
# demonstracni vypis
if __name__ == "__main__":
print('Vysledek dotazu otec["dana"] je %s.' % [otec["dana"]])
print('Vysledek dotazu rodic_generuj_Xs("david") je %s.' % \
rodic_generuj_Xs("david"))
print('Vysledek dotazu sourozenci_generuj_Ys("dana") je %s.' % \
sourozenci_generuj_Ys("dana"))
print("Vysledek dotazu otec['dana'] je '%s'." % otec["dana"])
print("Vysledek dotazu rodic_anyX('david') je %s." % \
rodic_anyX("david"))
print("Vysledek dotazu sourozenci_anyY('dana') je %s." % \
sourozenci_anyY("dana"))
#!/usr/bin/env python
# encoding=utf-8 (pep 0263)
from linked_lists import LinkedList, Nil
def member(x, ys):
for y in ys:
if x == y:
return True
return False
if ys == Nil:
return False
if x == ys.head:
return True
return member(x, ys.tail)
# demonstracni vypis
if __name__ == "__main__":
print("Member - 3. varianta")
print('Vysledek volani member("a", ["a", "b", "a"]) je %d.' % \
member("a", ["a", "b", "a"]) and "True" or "False")
print("Vysledek volani member('a', LinkedList('a', 'b', 'a')) je %s." % \
member("a", LinkedList("a", "b", "a")))
#!/usr/bin/env python
# encoding=utf-8 (pep 0263)
from linked_lists import LinkedList, Cons, Nil
def del_(x, ys):
result = []
for y in ys:
if x != y:
result.append(y)
return result
if ys == Nil:
return Nil
if x == ys.head:
return del_(x, ys.tail)
return Cons(ys.head, del_(x, ys.tail))
def del1(x, ys):
result_head = []
result_tail = ys
for y in ys:
result_tail = result_tail[1:]
if x == y:
yield result_head + result_tail
result_head.append(y)
if ys == Nil:
return
if x == ys.head:
yield ys.tail
for zs in del1(x, ys.tail):
yield Cons(ys.head, zs)
def insert(x, ys):
if not ys:
yield [x]
else:
result_head = []
result_tail = ys
for y in ys:
yield result_head + [x] + result_tail
result_tail = result_tail[1:]
result_head.append(y)
yield ys + [x]
yield Cons(x, ys)
if not ys == Nil:
for zs in insert(x, ys.tail):
yield Cons(ys.head, zs)
def insert1(x, ys):
return [x] + ys
return Cons(x, ys)
# demonstracni vypis
if __name__ == "__main__":
print("Prace se seznamy - del a insert")
print("-------------------------------\n")
print("funkce del_(x, ys) smaze vsechny vyskyty prvku x ze seznamu ys")
print("Vysledek volani del_(1, [1, 2, 1, 1, 2, 3, 1, 1]): %s\n" % \
del_(1, [1, 2, 1, 1, 2, 3, 1, 1]))
print("Vysledek volani del_(1, LinkedList(1, 2, 1, 1, 2, 3, 1, 1)):\n\t%s\n" % \
del_(1, LinkedList(1, 2, 1, 1, 2, 3, 1, 1)))
print("del1(x, ys) smaze vzdy jeden (podle poradi) vyskyt prvku x v seznamu ys")
print("V_ysledek volani del1(1, [1, 2, 1]): %s\n" % \
list(del1(1, [1, 2, 1])))
print("Vysledek volani del1(1, LinkedList(1, 2, 1)): \n\t%s\n" % \
list(del1(1, LinkedList(1, 2, 1))))
print("insert(x, ys) vklada postupne na vsechny pozice seznamu ys prvek x")
print("Vysledek volani insert(4, [2, 3, 1]): %s\n" % \
list(insert(4, [2, 3, 1])))
print("Vysledek volani insert(4, LinkedList(2, 3, 1)): \n\t%s\n" % \
list(insert(4, LinkedList(2, 3, 1))))
print("insert1(x, ys) vlozi x na zacatek seznamu ys")
print("Vysledek volani insert1(4, [2, 3, 1]): %s\n" % \
list(insert1(4, [2, 3, 1])))
print("Vysledek volani insert1(4, LinkedList(2, 3, 1)): \n\t%s\n" % \
insert1(4, LinkedList(2, 3, 1)))
#!/usr/bin/env python
# encoding=utf-8 (pep 0263)
def del1_anyX(xs):
result_head = []
result_tail = xs
for x in xs:
result_tail = result_tail[1:]
yield (x, result_head + result_tail)
result_head.append(x)
from linked_lists import LinkedList, Cons, Nil
def del1_anyX(ys):
if ys == Nil:
return
yield (ys.head, ys.tail)
for (z, zs) in del1_anyX(ys.tail):
yield (z, Cons(ys.head, zs))
def insert(x, ys):
if not ys:
yield [x]
else:
result_head = []
result_tail = ys
for y in ys:
yield result_head + [x] + result_tail
result_tail = result_tail[1:]
result_head.append(y)
yield ys + [x]
yield Cons(x, ys)
if not ys == Nil:
for zs in insert(x, ys.tail):
yield Cons(ys.head, zs)
def perm1(xs):
if not xs:
yield []
if xs == Nil:
yield Nil
else:
for ys in perm1(xs[1:]):
for zs in insert(xs[0], ys):
for ys in perm1(xs.tail):
for zs in insert(xs.head, ys):
yield zs
def perm2(xs):
if not xs:
yield []
if xs == Nil:
yield Nil
else:
for y, ys in del1_anyX(xs):
for zs in perm2(ys):
yield [y] + zs
yield Cons(y, zs)
def perm3(xs):
if not xs:
yield []
if xs == Nil:
yield Nil
else:
result_head = []
result_tail = xs
for x in xs:
result_tail = result_tail[1:]
for ys in perm3(result_head + result_tail):
yield [x] + ys
result_head.append(x)
ys = Nil
while xs != Nil:
result = xs.tail
for y in ys:
result = Cons(y, result)
for zs in perm3(result):
yield Cons(xs.head, zs)
ys = Cons(xs.head, ys)
xs = xs.tail
# demonstracni vypis
if __name__ == "__main__":
print("Prace se seznamy - permutace")
print("-------------------------------\n")
print("perm1 napsany pomoci insert")
print("Vysledek volani perm1([1, 2, 3]): %s\n" % \
list(perm1([1, 2, 3])))
print("Vysledek volani perm1(LinkedList(1, 2, 3)): \n\t%s\n" % \
list(perm1(LinkedList(1, 2, 3))))
print("perm2 napsany pomoci del1_anyX")
print("Vysledek volani perm2([1, 2, 3]): %s\n" % \
list(perm2([1, 2, 3])))
print("Vysledek volani perm2(LinkedList(1, 2, 3)): \n\t%s\n" % \
list(perm2(LinkedList(1, 2, 3))))
print("perm3 napsany pomoci pruchodu seznamem")
print("Vysledek volani perm3([1, 2, 3]): %s\n" % \
list(perm3([1, 2, 3])))
print("Vysledek volani perm3(LinkedList(1, 2, 3)): \n\t%s\n" % \
list(perm3(LinkedList(1, 2, 3))))
#!/usr/bin/env python
# encoding=utf-8 (pep 0263)
from linked_lists import LinkedList, Cons, Nil
def append(xs, ys):
if not xs:
if xs == Nil:
return ys
else:
return [xs[0]] + append(xs[1:], ys)
return Cons(xs.head, append(xs.tail, ys))
def append_anyXs(ys, zs):
if ys == zs:
return []
else:
if zs:
return [zs[0]] + append_anyXs(ys, zs[1:])
else:
raise ValueError("ys neni sufixem zs")
return Nil
if zs == Nil:
raise ValueError("ys neni sufixem zs")
return Cons(zs.head, append_anyXs(ys, zs.tail))
def append_anyXsYs(zs):
yield ([], zs)
if zs:
for xs, ys in append_anyXsYs(zs[1:]):
yield ([zs[0]] + xs, ys)
if zs != Nil:
for xs, ys in append_anyXsYs(zs.tail):
yield (Cons(zs.head, xs), ys)
# demonstracni vypis
if __name__ == "__main__":
print("Prace se seznamy - append")
print("-------------------------------\n")
print("vicesmerne implementace funkce append:\n")
print("Vysledek volani append(['a', 'b'], ['c', 'd']): %s\n" % \
append(['a', 'b'], ['c', 'd']))
print("Vysledek volani append(LinkedList('a', 'b'), LinkedList('c', 'd')): \n\t%s\n" % \
append(LinkedList('a', 'b'), LinkedList('c', 'd')))
print("Vysledek volani append_anyXs(['c', 'd'], ['a', 'b', 'c', 'd']): %s\n" % \
append_anyXs(['c', 'd'], ['a', 'b', 'c', 'd']))
print("Vysledek volani append_anyXs(LinkedList('c', 'd')," +
"LinkedList('a', 'b', 'c', 'd')): \n\t%s\n" % \
append_anyXs(LinkedList('c', 'd'), LinkedList('a', 'b', 'c', 'd')))
print("Vysledek volani append_anyXsYs(['a', 'b', 'c']): %s\n" % \
list(append_anyXsYs(['a', 'b', 'c'])))
print("Vysledek volani append_anyXsYs(LinkedList('a', 'b', 'c')): \n\t%s\n" % \
list(append_anyXsYs(LinkedList('a', 'b', 'c'))))
#!/usr/bin/env python
# encoding=utf-8 (pep 0263)
"""Lisp-like linked list data structure"""
Nil = []
class Cons(object):
def __init__(self, x, xs):
self.head = x
self.tail = xs
def __eq__(self, other):
if isinstance(other, self.__class__):
return self.head == other.head and self.tail == other.tail
else:
return False
def __ne__(self, other):
return not self.__eq__(other)
def __iter__(self):
return _Iterator(self)
def __str__(self):
return "[" + ", ".join(str(x) for x in self) + "]"
def __repr__(self):
return str(self)
class _Iterator(object):
def __init__(self, linked_list):
self._linked_list = linked_list
def __iter__(self):
return self
def next(self):
if self._linked_list == Nil:
raise StopIteration
else:
head = self._linked_list.head
self._linked_list = self._linked_list.tail
return head
def __next__(self):
return next(self)
def LinkedList(*lst):
result = Nil
for x in reversed(lst):
result = Cons(x, result)
return result
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