Skip to content
GitLab
Projects
Groups
Topics
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Vít Novotný
pb016-priklady
Commits
26f8ab79
Commit
26f8ab79
authored
Jan 17, 2017
by
Vít Novotný
Browse files
added example 2.2_4
parent
9560da8f
Changes
2
Hide whitespace changes
Inline
Side-by-side
2.2_4.pl
0 → 100644
View file @
26f8ab79
% nacteni:
/*
['2.2_4.pl'].
*/
del
(
_
,[],[]).
del
(
A
,[
A
|
T
],
V
)
:-
del
(
A
,
T
,
V
).
del
(
A
,[
H
|
T1
],[
H
|
T2
])
:-
A
\
=
H
,
del
(
A
,
T1
,
T2
).
del1
(
A
,[
A
|
T
],
T
).
del1
(
A
,[
H
|
T1
],[
H
|
T2
])
:-
del1
(
A
,
T1
,
T2
).
insert
(
A
,
L
,[
A
|
L
]).
insert
(
A
,[
H
|
T1
],[
H
|
T2
]):-
insert
(
A
,
T1
,
T2
).
insert1
(
X
,
List
,[
X
|
List
]).
% 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
(
'Prace se seznamy - del a insert'
),
nl
,
write
(
'-------------------------------'
),
nl
,
nl
,
write
(
'predikat del(A,L,Vysl) smaze vsechny vyskyty prvku A ze seznamu L'
),
nl
,
write
(
'Vysledek volani "del (1,[1,2,1,1,2,3,1,1], L)":'
),
nl
,
write_all_X
(
del
(
1
,[
1
,
2
,
1
,
1
,
2
,
3
,
1
,
1
],
L
),
L
,
'L'
),
nl
,
write
(
'del1(A,L,Vysl) smaze vzdy jeden (podle poradi) vyskyt prvku A v seznamu L'
),
nl
,
write
(
'Vysledek volani "del1(1,[1,2,1],L)":'
),
nl
,
write_all_X
(
del1
(
1
,[
1
,
2
,
1
],
L
),
L
,
'L'
),
nl
,
write
(
'insert(A,L,Vysl) vklada postupne (pri zadosti o dalsi reseni) na vsechny pozice seznamu L prvek A'
),
nl
,
write
(
'Vysledek volani "insert(4,[2,3,1], L)":'
),
nl
,
write_all_X
(
insert
(
4
,[
2
,
3
,
1
],
L
),
L
,
'L'
),
nl
,
write
(
'insert1(A,L,Vysl) vlozi A na zacatek seznamu L'
),
nl
,
write
(
'Vysledek volani "insert1(4,[2,3,1], L)":'
),
nl
,
write_all_X
(
insert1
(
4
,[
2
,
3
,
1
],
L
),
L
,
'L'
).
?-
start
.
:-
retractall
(
write_all_X
/
3
).
:-
retractall
(
start
/
0
).
2.2_4.py
0 → 100644
View file @
26f8ab79
#!/usr/bin/env python
# encoding=utf-8 (pep 0263)
def
del_
(
x
,
ys
):
result
=
[]
for
y
in
ys
:
if
x
!=
y
:
result
.
append
(
y
)
return
result
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
)
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
]
def
insert1
(
x
,
ys
):
return
[
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
(
"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
(
"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
(
"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
])))
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