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

added example 3.2_4

parent b1beb40c
......@@ -13,5 +13,5 @@ def member(x, ys):
# demonstracni vypis
if __name__ == "__main__":
print("Member - 3. varianta")
print("Vysledek volani member('a', LinkedList('a', 'b', 'a')) je %s." % \
member("a", LinkedList("a", "b", "a")))
print("Vysledek volani member('a', LinkedList(['a', 'b', 'a'])) je %s." % \
member("a", LinkedList(["a", "b", "a"])))
Member - 3. varianta
Vysledek volani member('a', LinkedList('a', 'b', 'a')) je True.
Vysledek volani member('a', LinkedList(['a', 'b', 'a'])) je True.
......@@ -5,7 +5,7 @@ from linked_lists import LinkedList, Cons, Nil
def stree(graph):
for edge in member_anyX(graph):
for kostra in spread(LinkedList(edge), graph):
for kostra in spread(LinkedList([edge]), graph):
yield kostra
def spread(akumulator_kostry, graph):
......@@ -58,6 +58,6 @@ def member_anyX(xs):
# demonstracni vypis
if __name__ == "__main__":
print('Kostra grafu')
print('Volani stree(LinkedList(("a", "b"), ("b", "c"), ("b", "d"), ("c", "d"))) vrati:')
for kostra_ in stree(LinkedList(("a", "b"), ("b", "c"), ("b", "d"), ("c", "d"))):
print('Volani stree(LinkedList([("a", "b"), ("b", "c"), ("b", "d"), ("c", "d")])) vrati:')
for kostra_ in stree(LinkedList([("a", "b"), ("b", "c"), ("b", "d"), ("c", "d")])):
print(' %s' % kostra_)
Kostra grafu
Volani stree(LinkedList(("a", "b"), ("b", "c"), ("b", "d"), ("c", "d"))) vrati:
Volani stree(LinkedList([("a", "b"), ("b", "c"), ("b", "d"), ("c", "d")])) vrati:
[('b', 'd'), ('b', 'c'), ('a', 'b')]
[('c', 'd'), ('b', 'c'), ('a', 'b')]
[('b', 'c'), ('b', 'd'), ('a', 'b')]
......
......@@ -32,17 +32,17 @@ 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, 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("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("Vysledek volani del1(1, LinkedList(1, 2, 1)): \n\t%s\n" % \
list(del1(1, LinkedList(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, LinkedList(2, 3, 1)): \n\t%s\n" % \
list(insert(4, LinkedList(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, LinkedList(2, 3, 1)): \n\t%s\n" % \
insert1(4, LinkedList(2, 3, 1)))
print("Vysledek volani insert1(4, LinkedList([2, 3, 1])): \n\t%s\n" % \
insert1(4, LinkedList([2, 3, 1])))
......@@ -2,18 +2,18 @@ Prace se seznamy - del a insert
-------------------------------
funkce del_(x, ys) smaze vsechny vyskyty prvku x ze seznamu ys
Vysledek volani del_(1, LinkedList(1, 2, 1, 1, 2, 3, 1, 1)):
Vysledek volani del_(1, LinkedList([1, 2, 1, 1, 2, 3, 1, 1])):
[2, 2, 3]
del1(x, ys) smaze vzdy jeden (podle poradi) vyskyt prvku x v seznamu ys
Vysledek volani del1(1, LinkedList(1, 2, 1)):
Vysledek volani del1(1, LinkedList([1, 2, 1])):
[[2, 1], [1, 2]]
insert(x, ys) vklada postupne na vsechny pozice seznamu ys prvek x
Vysledek volani insert(4, LinkedList(2, 3, 1)):
Vysledek volani insert(4, LinkedList([2, 3, 1])):
[[4, 2, 3, 1], [2, 4, 3, 1], [2, 3, 4, 1], [2, 3, 1, 4]]
insert1(x, ys) vlozi x na zacatek seznamu ys
Vysledek volani insert1(4, LinkedList(2, 3, 1)):
Vysledek volani insert1(4, LinkedList([2, 3, 1])):
[4, 2, 3, 1]
......@@ -51,13 +51,13 @@ if __name__ == "__main__":
print("Prace se seznamy - permutace")
print("-------------------------------\n")
print("perm1 napsany pomoci insert")
print("Vysledek volani perm1(LinkedList(1, 2, 3)): \n\t%s\n" % \
list(perm1(LinkedList(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(LinkedList(1, 2, 3)): \n\t%s\n" % \
list(perm2(LinkedList(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(LinkedList(1, 2, 3)): \n\t%s\n" % \
list(perm3(LinkedList(1, 2, 3))))
print("Vysledek volani perm3(LinkedList([1, 2, 3])): \n\t%s\n" % \
list(perm3(LinkedList([1, 2, 3]))))
......@@ -2,14 +2,14 @@ Prace se seznamy - permutace
-------------------------------
perm1 napsany pomoci insert
Vysledek volani perm1(LinkedList(1, 2, 3)):
Vysledek volani perm1(LinkedList([1, 2, 3])):
[[1, 2, 3], [2, 1, 3], [2, 3, 1], [1, 3, 2], [3, 1, 2], [3, 2, 1]]
perm2 napsany pomoci del1_anyX
Vysledek volani perm2(LinkedList(1, 2, 3)):
Vysledek volani perm2(LinkedList([1, 2, 3])):
[[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]]
perm3 napsany pomoci pruchodu seznamem
Vysledek volani perm3(LinkedList(1, 2, 3)):
Vysledek volani perm3(LinkedList([1, 2, 3])):
[[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]]
......@@ -27,12 +27,12 @@ if __name__ == "__main__":
print("Prace se seznamy - append")
print("-------------------------------\n")
print("vicesmerne implementace funkce append:\n")
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(LinkedList(['a', 'b']), LinkedList(['c', 'd'])): \n\t%s\n" % \
append(LinkedList(['a', 'b']), LinkedList(['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_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(LinkedList('a', 'b', 'c')): \n\t%s\n" % \
list(append_anyXsYs(LinkedList('a', 'b', 'c'))))
print("Vysledek volani append_anyXsYs(LinkedList(['a', 'b', 'c'])): \n\t%s\n" % \
list(append_anyXsYs(LinkedList(['a', 'b', 'c']))))
......@@ -3,12 +3,12 @@ Prace se seznamy - append
vicesmerne implementace funkce append:
Vysledek volani append(LinkedList('a', 'b'), LinkedList('c', 'd')):
[a, b, c, d]
Vysledek volani append(LinkedList(['a', 'b']), LinkedList(['c', 'd'])):
['a', 'b', 'c', 'd']
Vysledek volani append_anyXs(LinkedList('c', 'd'),LinkedList('a', 'b', 'c', 'd')):
[a, b]
Vysledek volani append_anyXs(LinkedList(['c', 'd']),LinkedList(['a', 'b', 'c', 'd'])):
['a', 'b']
Vysledek volani append_anyXsYs(LinkedList('a', 'b', 'c')):
[([], [a, b, c]), ([a], [b, c]), ([a, b], [c]), ([a, b, c], [])]
Vysledek volani append_anyXsYs(LinkedList(['a', 'b', 'c'])):
[([], ['a', 'b', 'c']), (['a'], ['b', 'c']), (['a', 'b'], ['c']), (['a', 'b', 'c'], [])]
......@@ -29,5 +29,5 @@ def divide(h, xs):
# 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)))
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])))
Radici algoritmus QuickSort
qsort(LinkedList(5, 2, 8, 2, 654, 8, 3, 4)):
qsort(LinkedList([5, 2, 8, 2, 654, 8, 3, 4])):
[2, 2, 3, 4, 5, 8, 8, 654]
......@@ -4,7 +4,7 @@
from linked_lists import LinkedList, Cons, Nil
def path(a, z, graph):
for cesta in path1(a, LinkedList(z), graph, 1):
for cesta in path1(a, LinkedList([z]), graph, 1):
yield cesta
def path1(a, akumulator_cesty, graph, depth):
......@@ -39,8 +39,8 @@ def member_anyX(xs):
for x in member_anyX(xs.tail):
yield x
_graph = (LinkedList("a", "b", "c", "d"),
LinkedList(("a", "b"), ("b", "d"), ("b", "c"), ("c", "d")))
_graph = (LinkedList(["a", "b", "c", "d"]),
LinkedList([("a", "b"), ("b", "d"), ("b", "c"), ("c", "d")]))
# demonstracni vypis
if __name__ == "__main__":
......
Cesta v grafu
next(path("a", "c", _graph)) : [a, b, c]
next(path("a", "c", _graph)) : ['a', 'b', 'c']
......@@ -4,7 +4,7 @@
from linked_lists import LinkedList, Cons, Nil
def path(a, z, graph):
for cesta, cena in path1(a, LinkedList(z), 0, graph, 1):
for cesta, cena in path1(a, LinkedList([z]), 0, graph, 1):
yield (cesta, cena)
def path1(a, akumulator_cesty, akumulator_ceny, graph, depth):
......@@ -42,7 +42,7 @@ def member_anyX(xs):
# demonstracni vypis
if __name__ == "__main__":
print('Cesta v grafu\n')
cesta_, cena_ = next(path("s", "u", LinkedList(
("s", "t", 3), ("t", "v", 1), ("t", "u", 5), ("u", "t", 2), ("v", "u", 2))))
cesta_, cena_ = next(path("s", "u", LinkedList([
("s", "t", 3), ("t", "v", 1), ("t", "u", 5), ("u", "t", 2), ("v", "u", 2)])))
print('Cesta=%s' % cesta_)
print('Cena=%d\n' % cena_)
Cesta v grafu
Cesta=[s, t, u]
Cesta=['s', 't', 'u']
Cena=8
......@@ -8,8 +8,8 @@ def solution(n=8):
yield Nil
else:
for others in solution(n-1):
for x in range(8):
for y in range(8):
for x in range(1, 9):
for y in range(1, 9):
if noattack(x, y, others):
yield Cons((x, y), others)
......@@ -21,11 +21,7 @@ def noattack(x, y, others):
return x != x1 and y != y1 and y1-y != x1-x and y1-y != x-x1 and \
noattack(x, y, others.tail)
def tisknireseni(reseni):
return ["%d/%d" % (x+1, y+1) for (x, y) in reseni]
# demonstracni vypis
if __name__ == "__main__":
print("PROBLEM OSMI DAM I")
print("Volani tisknireseni(next(solution())) : %s" % \
tisknireseni(next(solution())))
print("Volani next(solution()) : %s" % next(solution()))
PROBLEM OSMI DAM I
Volani tisknireseni(next(solution())) : ['8/4', '7/2', '6/7', '5/3', '4/6', '3/8', '2/5', '1/1']
Volani next(solution()) : [(8, 4), (7, 2), (6, 7), (5, 3), (4, 6), (3, 8), (2, 5), (1, 1)]
......@@ -8,8 +8,8 @@ def solution(n=8):
yield Nil
else:
for others in solution(n-1):
x = 8 - n
for y in range(8):
x = 9 - n
for y in range(1, 9):
if noattack(x, y, others):
yield Cons((x, y), others)
......@@ -21,11 +21,7 @@ def noattack(x, y, others):
return x != x1 and y != y1 and y1-y != x1-x and y1-y != x-x1 and \
noattack(x, y, others.tail)
def tisknireseni(reseni):
return ["%d/%d" % (x+1, y+1) for (x, y) in reseni]
# demonstracni vypis
if __name__ == "__main__":
print("PROBLEM OSMI DAM I")
print("Volani tisknireseni(next(solution())) : %s" % \
tisknireseni(next(solution())))
print("Volani next(solution()) : %s" % next(solution()))
PROBLEM OSMI DAM I
Volani tisknireseni(next(solution())) : ['1/4', '2/2', '3/7', '4/3', '5/6', '6/8', '7/5', '8/1']
Volani next(solution()) : [(1, 4), (2, 2), (3, 7), (4, 3), (5, 6), (6, 8), (7, 5), (8, 1)]
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