Solutions Listes

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Solutions (Listes)

tete([],[]).
tete([X|_], X).

fin([],[]).
fin([X|[]], X).
fin([_|L],Y):-last(L,Y).

ajoutdebut(X,L,[X|L]).

ajoutfin(X,[Y|L], R):- ajoutfin(X,L, R1), append([Y],R1,R).


ajoutfin(X,[], [X]).

suppdebut([],[]).
suppdebut([_|L],L).

suppfin([],[]).
suppfin([_],[]).
suppfin([Y|L],R):-suppfin(L,R1),append([Y],R1,R).

appartient([A|_],A).
appartient([_|R],B):-appartient(R,B).

long([],0).
long([_|R],L):-long(R,L1), L is L1+1.

somme([],0).
somme([A|R],L):-somme(R,L1), L is L1+A.

supp(A,[A|L], L).
supp(A,[B|L],R):-supp(A,L,R1), append([B],R1,R).

supptout(_, [], []).


supptout(A, [A|L], R):-supptout(A, L, R).
supptout(A, [B|L], R):-supptout(A, L, R1), append([B],R1,R).

fusion([],L,L).
fusion([T|R1],L,[T|R2]) :- fusion(R1,L,R2).

min([A,B|L],M):- A =< B, min([A|L], M).


min([A,B|L],M):- B < A, min([B|L], M).
min([A,B],A):- A =< B.
min([A,B],B):- B < A.
min([A],A).
min([],[]).

invers([],[]).
invers([X|L],R):-invers(L,L1), append(L1,[X],R).
pal([]).
pal([_]).
pal(L):-append([X|R],[X],L),pal(R).
% tri par bulles
delete([],[]).
delete([_],[]).
delete([X|L],L1):-delete(L,L2), append([X],L2,L1).
last([],[]).
last([X|[]], X).
last([_|L],Y):-last(L,Y).
add(X,L,[X|L]).

tri([],[]).
tri([X],[X]).
tri([X,Y|R],L):-X =< Y, tri([Y|R],L1), append([X],L1,L).
tri([X,Y|R],L):- Y < X, tri([X|R],L1), append([Y],L1,L).

main([],[], []).
main([],L,L).
main(L,R, R1):-tri(L,T), last(T, Y), add(Y,R,R2), delete(T,D), main(D,R2, R1).
% exemple ?- main([17,20,9,1,4,2],[],R).

% le plus court chemin


arret(s,a, 5).
arret(a,b, 10).
arret(b,c, 30).
arret(s,b, 50).
arret(c,d, 15).
arret(d,e, 20).
arret(e,f, 10).

chemin(X,Y,L, P):-arret(X,Y, L), P = [].


chemin(X,Y,L,P):- arret(X,Z, W),chemin(Z,Y,L1, P1), L is L1 +W, append([Z], P1, P).
findchemin(X,Y,L, P, Liste):-findall([X,P,Y,L],chemin(X,Y,L,P), Liste).

min1([[E1,[E2|L1],E3,A],[_,[_|_],_,B]|L],M):- A =< B, min1([[E1,[E2|L1],E3,A]|L], M).


min1([[_,[_|_],_,A],[E1,[E2|L1],E3,B]|L],M):- B < A, min1([[E1,[E2|L1],E3,B]|L], M).
min1([[E1,[E2|L1],E3,A],[_,[_|_],_,B]],[E1,[E2|L1],E3,A]):- A =< B.
min1([[_,[_|_],_,A],[E1,[E2|L1],E3,B]],[E1,[E2|L1],E3,B]):- B < A.
min1([[E1,[E2|L1],E3,A]],[E1,[E2|L1],E3,A]).
min1([[E1,[],E3,A]],[E1,[],E3,A]).
min1([],[]).

main1(X,Y,L,P,Liste, MIN):- findchemin(X,Y,L, P, Liste), min1(Liste,MIN).


% exemple ?- main1(s,f,L,P,Liste,Min).

You might also like