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
fe8559da
Commit
fe8559da
authored
Jan 22, 2017
by
Vít Novotný
Browse files
added example 5.1_5
parent
35a8877a
Changes
3
Hide whitespace changes
Inline
Side-by-side
5.1_5.pl
0 → 100644
View file @
fe8559da
% nacteni:
/*
['5.1_5.pl'].
*/
:-
retractall
(
start
/
0
).
:-
retractall
(
write_all_X
/
3
).
?-
op
(
100
,
xfx
,
to
),
dynamic
(
hanoi
/
5
).
hanoi
(
1
,
A
,
B
,
_
,[
A
to
B
]).
hanoi
(
N
,
A
,
B
,
C
,
Moves
)
:-
N
>
1
,
N1
is
N
-
1
,
lemma
(
hanoi
(
N1
,
A
,
C
,
B
,
Ms1
)),
hanoi
(
N1
,
C
,
B
,
A
,
Ms2
),
append
(
Ms1
,[
A
to
B
|
Ms2
],
Moves
).
lemma
(
P
)
:-
P
,
asserta
((
P
:-
!)).
% demonstracni vypis
% abychom se vyhli varovanim "Redefined static procedure ..."
:-
dynamic
write_all_X
/
3
,
start
/
0
.
write_all_X
(
Goal
,
X
,
Name
):-
call
(
Goal
),
write
(
' '
),
write
(
Name
),
write
(
' = '
),
write
(
X
),
nl
,
fail
.
write_all_X
(
_
,
_
,
_
).
start
:-
write
(
'Demostrace programu Hanoi'
),
nl
,
write
(
'hanoi(+PocetTaliru,+z_tyce,+na_tyc,+pomoci_tyce,-Tahy)'
),
nl
,
write
(
'Vysledek dotazu "hanoi(2,a,b,c,M)".'
),
nl
,
write_all_X
(
hanoi
(
2
,
a
,
b
,
c
,
M
),
M
,
'M'
),
write
(
'Vysledek dotazu "hanoi(3,a,b,c,M)".'
),
nl
,
write_all_X
(
hanoi
(
3
,
a
,
b
,
c
,
M
),
M
,
'M'
),
write
(
'Vysledek dotazu "hanoi(4,a,b,c,M)".'
),
nl
,
write_all_X
(
hanoi
(
4
,
a
,
b
,
c
,
M
),
M
,
'M'
).
?-
start
.
:-
retractall
(
start
/
0
).
:-
retractall
(
write_all_X
/
3
).
5.1_5.py
0 → 100644
View file @
fe8559da
#!/usr/bin/env python
# encoding=utf-8 (pep 0263)
from
linked_lists
import
LinkedList
,
Cons
,
Nil
hanoi_mem
=
dict
()
def
hanoi
(
n
,
a
,
b
,
c
):
if
n
==
1
:
return
LinkedList
([
"%s to %s"
%
(
a
,
b
)])
if
n
<
1
:
raise
ValueError
(
"n musi byt kladne"
)
if
(
n
+
1
,
a
,
c
,
b
)
not
in
hanoi_mem
:
hanoi_mem
[(
n
-
1
,
a
,
c
,
b
)]
=
hanoi
(
n
-
1
,
a
,
c
,
b
)
if
(
n
+
1
,
a
,
c
,
b
)
not
in
hanoi_mem
:
hanoi_mem
[(
n
-
1
,
c
,
b
,
a
)]
=
hanoi
(
n
-
1
,
c
,
b
,
a
)
ms1
=
hanoi_mem
[(
n
-
1
,
a
,
c
,
b
)]
ms2
=
hanoi_mem
[(
n
-
1
,
c
,
b
,
a
)]
return
append
(
ms1
,
Cons
(
"%s to %s"
%
(
a
,
b
),
ms2
))
def
append
(
xs
,
ys
):
if
xs
==
Nil
:
return
ys
else
:
return
Cons
(
xs
.
head
,
append
(
xs
.
tail
,
ys
))
# demonstracni vypis
if
__name__
==
"__main__"
:
print
(
"Demonstrace programu Hanoi"
)
print
(
"hanoi(pocet_taliru, z_tyce, na_tyc, pomoci_tyce)"
)
print
(
"
\n
Vysledek dotazu hanoi(2, 'a', 'b', 'c'): "
)
print
(
hanoi
(
2
,
'a'
,
'b'
,
'c'
))
print
(
"
\n
Vysledek dotazu hanoi(3, 'a', 'b', 'c'): "
)
print
(
hanoi
(
3
,
'a'
,
'b'
,
'c'
))
print
(
"
\n
Vysledek dotazu hanoi(4, 'a', 'b', 'c'): "
)
print
(
hanoi
(
4
,
'a'
,
'b'
,
'c'
))
5.1_5.py.out
0 → 100644
View file @
fe8559da
Demonstrace programu Hanoi
hanoi(pocet_taliru, z_tyce, na_tyc, pomoci_tyce)
Vysledek dotazu hanoi(2, 'a', 'b', 'c'):
['a to c', 'a to b', 'c to b']
Vysledek dotazu hanoi(3, 'a', 'b', 'c'):
['a to b', 'a to c', 'b to c', 'a to b', 'c to a', 'c to b', 'a to b']
Vysledek dotazu hanoi(4, 'a', 'b', 'c'):
['a to c', 'a to b', 'c to b', 'a to c', 'b to a', 'b to c', 'a to c', 'a to b', 'c to b', 'c to a', 'b to a', 'c to b', 'a to c', 'a to b', 'c to b']
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