Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
Vít Novotný
pb016-priklady
Commits
3dc228e3
Commit
3dc228e3
authored
Jan 17, 2017
by
Vít Novotný
Browse files
rewrote the examples using linked lists
parent
2cee916f
Changes
6
Hide whitespace changes
Inline
Side-by-side
1.1_13.py
View file @
3dc228e3
...
...
@@ -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"
))
2.1.3_3.py
View file @
3dc228e3
#!/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"
))
)
2.2_4.py
View file @
3dc228e3
#!/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
=
=
y
s
.
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
)))
2.3_5.py
View file @
3dc228e3
#!/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
)
)))
2.4.1_6.py
View file @
3dc228e3
#!/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'
)
)))
linked_lists.py
0 → 100644
View file @
3dc228e3
#!/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
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment