Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- fact(0,1).
- fact(X,R) :- X1 is X-1, fact(X1,R1), R is R1*X.
- fact1(X,R) :- fact1(X,1,R).
- fact1(0, Acc, Acc).
- fact1(X,Acc,R) :- X>0, Acc1 is Acc*X, X1 is X-1, fact1(X1,Acc1,R).
- minim([H|L],R):- minim(L,R),R<H,!.
- minim([H|_],H).
- deletef(H,[H|T],T).
- deletef(X,[H|T],[H|R]) :- deletef(X,T,R).
- deletef(_,[],[]).
- deleteall(X,[X|T],R) :- delteall(X,T,R).
- deleteall(X,[H|T],[H|R]) :- deleteall(X,T,R).
- deleteall(_,[],[]).
- add_first(X,L,[X|L]).
- member(X,[X|_]).
- member(X,[_|T]) :- member(X,T).
- remove_duplicates([],[]).
- remove_duplicates([H|T],R) :- member(H,T), remove_duplicates(T,R).
- remove_duplicates([H|T],[H|R]) :- remove_duplicates(T,R).
- inters([],_,[]).
- inters([H|T], L2, [H|R]) :- member(H,L2), !, inters(T,L2,R).
- inters([_|T], L2, R) :- inters(T,L2,R).
- sel_sort(L, [M|R]) :- minim(L,M), deletef(M,L,L1), sel_sort(L1,R).
- sel_sort([],[]).
- insert_ord(X, [H|T], [H|R]):- X>H, !, insert_ord(X,T,R).
- insert_ord(X,T,[X|T]).
- ins_sort([H|T], R) :- ins_sort(T,R1), insert_ord(H,R1,R).
- ins_sort([],[]).
- max(X,Y,X) :- X>Y.
- max(_,Y,Y).
- depth([],1).
- depth([H|T],R) :- atomic(H), !, depth(T,R).
- depth([H|T],R) :- depth(H,R1), depth(T,R2), R3 is R1+1, max(R3,R2,R).
- append2([],L2,L2).
- append2([H|T],L2,[H|R]):- append2(T,L2,R).
- append3(L1,L2,L3,R) :- append2(L2,L3,R1), append2(L1,R1,R).
- memberdeep(H,[H|_]).
- memberdeep(X,[H|_]) :- memberdeep(X,H).
- memberdeep(X,[_|T]) :- memberdeep(X,T).
- flatten([],[]).
- flatten([H|T],[H|R]) :- atomic(H), !, flatten(T,R).
- flatten([H|T],R):- flatten(H,R1), flatten(T,R2), append(R1,R2,R).
- tree1(t(6, t(4,t(2,nil,nil),t(5,nil,nil)), t(9,t(7,nil,nil),nil))).
- tree2(t(8, t(5, nil, t(7, nil, nil)), t(9, nil, t(11, nil, nil)))).
- inorder(t(K,L,R), List) :- inorder(L,LL), inorder(R,LR), append2(LL, [K|LR], List).
- inorder(nil, []).
- preorder(t(K,L,R),List) :- preorder(L,LL), preorder(R,LR), append2([K|LL], LR, List).
- preorder(nil, []).
- postorder(t(K,L,R),List) :- postorder(L,LL), postorder(R,LR), append2(LL,LR,R1), append2(R1,[K],List).
- postorder(nil, []).
- searchkey(Key, t(Key,_,_)) :- !.
- searchkey(Key, t(K,L,_)) :- Key<K, !, searchkey(Key,L).
- searchkey(Key, t(K,_,R)) :- searchkey(Key,R).
- insertkey(Key,nil,t(Key,nil,nil)).
- insertkey(Key,t(Key,L,R),t(Key,L,R)):-!.
- insertkey(Key,t(Key,L,R),t(Key,NL,R)) :- Key<K, !, insertkey(Key,L,NL).
- insertkey(Key,t(Key,L,R),t(Key,L,NR)) :- insertkey(Key,R,NR).
- height(nil,0).
- height(t(_,L,R),H) :- height(L,H1), height(R,H2), max(H1,H2,H3), H is H3+1;
- memberil(_,L) ;- var(L), !, fail.
- memberil(X,[X|_]) :- !.
- memberil(X,[_|T]) :- memberil(X,T).
- insertil(X,L) :- var(L), !, L=[X|_].
- insertil(X, [X|_]) :- !.
- insertil(X, [_|T]) :- inseril(X,T).
- deleteil(X,L,L) :- var(L), !.
- deleteil(X,[X|T],R) :- !.
- deleteil(X,[H|T],[H|R]) :- deleteil(X,T,R).
- appendil(L1,L2,L2) :- var(L1), !.
- appendil([H|L1],L2,[H|R]) :- appendil(L1,L2,R).
- convertI2C(L1,[]) :- var(L1), !.
- convertI2C([H|L1],[H|R]) :- convertI2C(L1,R).
- convertC2I([],_) :- !.
- convertC2I([H|T],[H|R]) :- convertC2I(T,R).
- % 1. Stergerea ultimului elem. dintr-o lista incompleta
- sterg_ultim_il([X|T],T):-var(T),!.
- sterg_ultim_il([X|T],[X|R]):- sterg_ultim_il(T,R).
- % Apel: sterg_ultim_il([1,2,3|_],R).
- % 2. Stergerea ultimului elem. dintr-o lista completa
- sterg_ultim([X|[]],[]):-!.
- sterg_ultim([X|T],[X|R]):- sterg_ultim(T,R).
- sterg_ultim1([X],[]):-!.
- sterg_ultim1([X|T],[X|R]):- sterg_ultim1(T,R).
- % Apel: sterg_ultim1([1,2,3],R).
- % 3.Stergerea ultimului element dintr-o lista diferenta
- sterg_ultim_dl([F],L,RL,RL):-!.
- sterg_ultim_dl([X|T],L,[X|RF],RL):- sterg_ultim_dl(T,L,RF,RL).
- % Apel: sterg_ultim_dl([1,2,3|LE],LE,RF,RL).
- % 4. Concatenarea a doua liste diferenta
- append_dl([],L1,F2,L2,F2,L2):-!.
- append_dl([H|L],L1,F2,L2,[H|RF],RL):-append_dl(L,L1,F2,L2,RF,RL).
- % Apel: append_dl([1,2,3|L1],L1,[4,5|L2],L2,RF,RL).
- % 5. Concatenare liste incomplete
- append_il(L1,L2,L2):-var(L1),!.
- append_il([H|T],L2,[H|R]):-append_il(T,L2,R).
- % Apel: append_il([1,2,3|_],[4,5|_],R).
- % 6. Lista completa in lista diferenta
- cl_dl([],Last,Last):-!.
- cl_dl([H|T],[H|First],Last):-cl_dl(T,First,Last).
- % Apel: cl_dl([1,2,3,4],F,L).
- % 7. Lista completa in lista incompleta
- cl_il([],_):-!.
- cl_il([H|T],[H|R]):-cl_il(T,R).
- % Apel: cl_il([1,2,3],R).
- % 8. Lista diferenta in lista completa
- dl_cl([],L,[]):-!.
- dl_cl([X|T],L,[X|R]):- dl_cl(T,L,R).
- % Apel: dl_cl([1,2,3|L],L,R).
- % 9. Lista incompleta in lista completa
- il_cl(T,[]):-var(T),!.
- il_cl([X|T],[X|R]):- il_cl(T,R).
- % Apel: il_cl([1,2,3|_],R).
- % 10. Lista incompleta in lista diferenta
- il_dl(T,L,L):-var(T),!.
- il_dl([X|T],[X|F],L):- il_dl(T,F,L).
- % Apel: il_dl([1,2,3|_],F,L).
- % 11. Lista diferenta in lista incompleta
- dl_il([],L,_):-!.
- dl_il([X|T],L,[X|R]):- dl_il(T,L,R).
- % Apel: dl_il([1,2,3|L],L,R).
- % 12. Inversarea unei liste incomplete.
- invers_il(T,_):-var(T),!.
- invers_il([H|T],R):-invers_il(T,R1),append_il(R1,[H|_],R).
- % 13. Inversarea unei liste diferenta.
- invers_dl([],L,RL,RL):-!.
- invers_dl([H|T],L,RF,RL):-invers_dl(T,L,F1,L1),append_dl(F1,L1,[H|PL],PL,RF,RL).
- % Apel: invers_dl([1,2,3|L],L,RF,RL).
- % 14. Inversarea unei liste complete.
- invers([],[]).
- invers([H|T],R):-invers(T,R1),append(R1,[H],R).
- % 15. Concatenarea a doua liste complete
- append_cl([],L2,L2):-!.
- append_cl([H|T],L2,[H|R]):-append_cl(T,L2,R).
- % 16. Gasirea elementului max dintr-o lista cu recursivitate inapoi
- max_bw([H],H).
- max_bw([H|T],H):-max_bw(T,M),H>M,!.
- max_bw([H|T],M):-max_bw(T,M).
- % 17. Gasirea elementului max dintr-o lista cu recursivitate inainte
- max_fw([],A,A).
- max_fw([H|T],A,R):-H>A,!,max_fw(T,H,R).
- max_fw([H|T],A,R):-max_fw(T,A,R).
- max_fw([H|T],R):-max_fw(T,H,R).
- % 18. Se da o lista. Sa se descomp in 2 liste dupa un pivot P dat.
- pivot([],_,[],[]).
- pivot([H|T],P,[H|Mic],Mare):-H<P,!, pivot(T,P,Mic,Mare).
- pivot([H|T],P,Mic,[H|Mare]):-pivot(T,P,Mic,Mare).
- % 19. Folosind for cu decrementare sa se genereze o lista de genul [N,...,1] cu N dat
- for_dec(0,[]).
- for_dec(Index,[Index|Out]):-Index>0, NewIndex is Index -1, for_dec(NewIndex,Out).
- % 20. append3 - versiune eficienta
- append3(L1,L2,L3,R):-append(L2,L3,I), append(L1,I,R).
- % 21. Inlocuirea primei aparitii a unui elelemnt U cu un alt element N dintr-o lista completa
- replace_first(U,N,[U|T],[N|T]).
- replace_first(U,N,[H|T],[H|R]):-replace_first(U,N,T,R).
- % 22. Inlocuirea primei aparitii a unui elelemnt U cu un alt element N dintr-o lista termina in variabila
- replace_first_il(_,_,T,T):-var(T),!.
- replace_first_il(U,N,[U|T],[N|T]).
- replace_first_il(U,N,[H|T],[H|R]):-replace_first_il(U,N,T,R).
- % 23. Inlocuirea aparitiilor a unui elelemnt U cu un alt element N dintr-o lista completa
- replace_all(_,_,[],[]).
- replace_all(U,N,[U|T],[N|R]):-replace_all(U,N,T,R).
- replace_all(U,N,[H|T],[H|R]):-replace_all(U,N,T,R).
- % 24. Inlocuirea aparitiilor a unui elelemnt U cu un alt element N dintr-o lista incompleta
- replace_all_il(_,_,T,T):-var(T),!.
- replace_all_il(U,N,[U|T],[N|R]):-replace_all_il(U,N,T,R).
- replace_all_il(U,N,[H|T],[H|R]):-replace_all_il(U,N,T,R).
- %1. Count the number of lists in a deep list.
- count_lists([],0).
- count_lists([H|T],R):-atomic(H),count_lists(T,R).
- count_lists([H|T], R):-count_lists(H, RH),count_lists(T, RT),R is RH + RT + 1.
- append1([], L, L).
- append1([H|T], L, [H|R]):-append(T, L, R).
- %2. Double the odd numbers and square the even.
- numbers([],[]).
- numbers([H|T],[H*H|R]):- 0 is H mod 2,numbers(T,R).
- numbers([H|T],[2*H|R]):- 1 is H mod 2,numbers(T,R).
- %3. Convert a number to binary
- dec_bin(0,'0').
- dec_bin(1,'1').
- dec_bin(N,B):-N>1,X is N mod 2,Y is N//2,dec_bin(Y,B1),atom_concat(B1, X, B).
- %5. Delete the occurrences of x on even positions
- del_pos_even([],_,_,[]).
- del_pos_even([H|T],C,H,R):-0 is C mod 2,!,C1 is C+1,del_pos_even(T,C1,H,R).
- del_pos_even([H|T],C,Nr,[H|R]):-C1 is C+1,del_pos_even(T,C1,Nr,R).
- %6 Compute the divisors of a natural number.
- divisor(NR,NR,DIVS,[NR|DIVS]):-!.
- divisor(NR,ACC,DIVS,R):-0 is NR mod ACC,!,ACC1 is ACC + 1,append([ACC],DIVS,DIVS1),divisor(NR,ACC1,DIVS1,R).
- divisor(NR,ACC,DIVS,R):-ACC1 is ACC+1,divisor(NR,ACC1,DIVS,R).
- %7. Reverse a natural number.
- reverse_n(0,0).
- reverse_n(Nr,R1):-M is Nr mod 10, D is Nr div 10,reverse_n(D,R),atom_concat(M,R,R1).
- %8. Delete each kth element from the end of the list.
- delete_k([],_,_,[]).
- delete_k([H|T],K,Count,R):-Count is K-1,!,delete_k(T,K,0,R).
- delete_k([H|T],K,Count,[H|R]):-Count1 is Count + 1, delete_k(T,K,Count1,R).
- reverse_list([],[]).
- reverse_list([H|T],R):-reverse(T,R1),append(R1,[H],R).
- delete_kend(L,K,Count,R):-reverse_list(L,NewL),delete_k(NewL,K,Count,R1),reverse_list(R1,R).
- % 9. Separate the even elements on odd positions from the rest
- separate1([],_,[],[]).
- separate1([H|T],Count,[H|Even],Rest):-0 is H mod 2,1 is Count mod 2,!,Count1 is Count + 1,separate1(T,Count1,Even,Rest).
- separate1([H|T],Count,Even,[H|Rest]):-Count1 is Count + 1,separate1(T,Count1,Even,Rest).
- separate11(L,Even,Rest):-separate1(L,1,Even,Rest).
- %10. Binary incomplete tree. Collect odd nodes with 1 child in an incomplete list.
- collect_odd_from_1child(t(_, L, R), _):-
- var(L),
- var(R), !.
- collect_odd_from_1child(t(K, N, R), [K|List]):-
- var(N),
- 1 is K mod 2, !,
- collect_odd_from_1child(R, List).
- collect_odd_from_1child(t(K, L, N), List):-
- var(N),
- 1 is K mod 2, !,
- collect_odd_from_1child(L, List),
- insert_il(K, List).
- collect_odd_from_1child(t(_, L, R), List):-
- collect_odd_from_1child(L, LL),
- collect_odd_from_1child(R, RL),
- append_il(LL, RL),
- List = LL.
- insert_il(X, L):-
- var(L), !,
- L = [X|_].
- insert_il(X, [_|T]):-
- insert_il(X, T).
- append_il(L1, L2):-
- var(L1), !,
- L1 = L2.
- append_il([_|T1], L2):-
- append_il(T1, L2).
- %12. Binary Tree. Collect even keys from leaves in a difference list.
- collect_even_from_leaf(nil,L,L).
- collect_even_from_leaf(t(K,nil,nil),[K|LS],LS):-0 is K mod 2,!.
- collect_even_from_leaf(t(K,L,R),LS,LE):-collect_even_from_leaf(L,LS,LM),collect_even_from_leaf(R,LM,LE).
- %14. Collect all the nodes at odd depth from a binary incomplete tree (the root has depth 0).
- collect_all_odd_depth(N, _, []):-
- var(N),!.
- collect_all_odd_depth(t(K, L, R), N, [K|List]):-
- 1 is N mod 2, !,
- N1 is N + 1,
- collect_all_odd_depth(L, N1, LL),
- collect_all_odd_depth(R, N1, LR),
- append(LL, LR, List).
- collect_all_odd_depth(t(_, L, R), N, List):-
- N1 is N + 1,
- collect_all_odd_depth(L, N1, LL),
- collect_all_odd_depth(R, N1, LR),
- append(LL, LR, List).
- %15. Flatten only the elements at depth X from a deep list.
- max(A,B,A):-A>B,!.
- max(A,B,B).
- depth([],1).
- depth([H|T],R):-atomic(H),!,depth(T,R).
- depth([H|T],R):- depth(H,R1), depth(T,R2), R3 is R1+1, max(R3,R2,R).
- flatten([],[]).
- flatten([H|T], [H|R]) :- atomic(H),!, flatten(T,R).
- flatten([H|T], R) :- flatten(H,R1), flatten(T,R2), append(R1,R2,R).
- flatten_depth([],_,_,[]).
- flatten_depth([H|T],Depth,Count,[H|R]):-atomic(H),!,Count is Depth,flatten_depth(T,Depth,Count,R).
- flatten_depth([H|T],Depth,Count,R):-atomic(H),!,flatten_depth(T,Depth,Count,R).
- flatten_depth([H|T],Depth,Count,R):-Count1 is Count +1,flatten_depth(H,Depth,Count1,R1),flatten_depth(T,Depth,Count,R2),append(R1,R2,R).
- %16. Delete duplicate elements that are on an odd position in a list
- %count_dups(List,Elemen,Count,R)
- count_dups([],El,Count,[Count]).
- count_dups([H|T],H,Count,R):-Count1 is Count+1,count_dups(T,H,Count1,R).
- count_dups([H|T],El,Count,R):-count_dups(T,El,Count,R).
- %remove_odd(List,Elem,Count,R)
- remove_odd([],_,_,[]).
- remove_odd([H|T],H,Count,R):-1 is Count mod 2,!,Count1 is Count + 1, remove_odd(T,H,Count1,R).
- remove_odd([H|T],Elem,Count,[H|R]):-Count1 is Count + 1, remove_odd(T,Elem,Count1,R).
- %remove_odd_dups(List,R)
- remove_odd_dups([],[]).
- remove_odd_dups([H|T],[H|R]):-count_dups(T,H,0,C),C>1,remove_odd(T,H,0,NewT),remove_odd_dups(NewT,R).
- remove_odd_dups([H|T],[H|R]):-remove_odd_dups(T,R).
- %11. Ternary incomplete tree. Collect the keys between X and Y (closed interval) in a difference list.
- %collect_ternary(T,X,Y,R).
- collect_ternary(L,_,_,LS,LS):-var(L),!.
- collect_ternary(t(K,L,M,R),X,Y,[K|LS],LE):-K>=X,K=<Y,!,collect_ternary(L,X,Y,LS,RE1),collect_ternary(M,X,Y,RE1,RE2),collect_ternary(R,X,Y,RE2,LE).
- collect_ternary(t(K,L,M,R),X,Y,LS,LE):-collect_ternary(L,X,Y,LS,RE1),collect_ternary(M,X,Y,RE1,RE2),collect_ternary(R,X,Y,RE2,LE).
- tree(t(2,t(8,_,_,_),t(3,_,_,t(4,_,_,_)),t(5,t(7,_,_,_),t(6,_,_,_),t(1,_,_,t(9,_,_,_))))).
- %13. Replace the min element from a ternary incomplete tree with the root.
- inorder_ternary(T,_):-var(T),!.
- inorder_ternary(t(K,L,M,R), List):-inorder_ternary(L,LL), inorder_ternary(M,LM), inorder_ternary(R,RL),
- append(LL, [K|LM],List1), append(List1,RL,List).
- minim([],MIN,MIN).
- minim([H|T],MIN,R):-H<MIN,!,MINP is H,minim(T,MINP,R).
- minim([H|T],MIN,R):-minim(T,MIN,R).
- min_ternary(T,List):-inorder_ternary(T,L),minim(L,100,List).
- %copac(t(2,t(8,_,_,_),t(3,_,_,t(1,_,_,_)),t(5,t(7,_,_,_),t(6,_,_,_),t(1,_,_,t(9,_,_,_))))).
- replacemin(t(K,L,M,R),t(NK,NL,NM,NR)):-min_ternary(t(K,L,M,R),Min),replace_min_tree(t(K,L,M,R),Min,K,t(NK,NL,NM,NR)).
- replace_min_tree(L,_,_,_):-var(L),!.
- replace_min_tree(t(K,L,M,R),Min,Root,t(Root,NL,NM,NR)):-Min=K,!,
- replace_min_tree(L,Min,Root,NL),
- replace_min_tree(M,Min,Root,NM),
- replace_min_tree(R,Min,Root,NR).
- replace_min_tree(t(K,L,M,R),Min,Root,t(K,NL,NM,NR)):-
- replace_min_tree(L,Min,Root,NL),
- replace_min_tree(M,Min,Root,NM),
- replace_min_tree(R,Min,Root,NR).
- %18. Replace each node with its height in a binary incomplete tree
- % predicate which computes the maximum between 2 numbers
- max(A, B, A):-A>B, !.
- max(_, B, B).
- % predicate which computes the height of a binary tree
- height(L, 0):-var(L),!.
- height(t(_, L, R), H):-height(L, H1), height(R, H2), max(H1, H2, H3), H is H3+1.
- replaceheight(T,R):-height(T,H),replace_height(T,H,R).
- replace_height(L,_,L):-var(L).
- replace_height(t(K,L,R),Height,t(Height,NL,NR)):-H1 is Height-1,replace_height(L,H1,NL),replace_height(R,H1,NR).
- altcopac(t(2,t(4,t(5,_,_),t(7,_,_)),t(3,t(0,t(4,_,_),_),t(8,_,t(5,_,_))))).
- %21. Encode a list with RLE.
- encode([],X,Count,[[X,Count]]).
- encode([H|T],H,Count,R):-Count1 is Count + 1,encode(T,H,Count1,R).
- encode([H|T],X,Count,[[X,Count]|R]):-encode(T,H,1,R).
- rle_encode([H|T],R):-encode(T,H,1,R).
- %20. Decode a list encoded with RLE.
- printn([Nr,Count],Count,List,[List]).
- printn([Nr,Count],Acc,List,R):-Acc<Count,Acc1 is Acc + 1, append([Nr],List,List1),printn([Nr,Count],Acc1,List1,R).
- decode([],List,[List]).
- decode([[Nr,Count]|T],List,R):-printn([Nr,Count],0,[],R1),append(List,R1,R2),decode(T,R2,R).
- %[1,2,3,4]
- %[5,6,4,6]
- %[4,5,6,3]
- %[1,2,3,3]
- %[1,5,4,1]
- %[2,6,5,2]
- %[3,4,6,3]
- %[4,6,3,3]
- extractk([],_,_,[]).
- extractk([H|T],Count,K,[H|R]):-Count is K,!,Count1 is Count + 1,extractk(T,Count1,K,R).
- extractk([H|T],Count,K,R):-Count1 is Count +1,extractk(T,Count1,K,R).
- %%%%%%%%%%%
- count_dups1(L,El,Count,[Count]):-var(L),!.
- count_dups1([H|T],H,Count,R):-Count1 is Count+1,count_dups1(T,H,Count1,R).
- count_dups1([H|T],El,Count,R):-count_dups1(T,El,Count,R).
- %%%%%%%%%%%%%%
- postorder(t(K,L,R),List):-postorder(L,LL),postorder(R,LR),append(LL,LR,Rez1),append(Rez1,[K],List).
- postorder(nil,[]).
- sum([],0).
- sum([H|T],R):-sum(T,T1),R is T1 + H.
- suma(L,Partial,Partial):-var(L),!.
- suma([H|T],Partial,R):-sum(H,Suma),append(Partial,[Suma],Partial1),suma(T,Partial1,R).
- sumaminim(L,M):-suma(L,[],R),minim(R,100,M).
- %In lista incompleta, nodurile de la o anumita inaltime
- heightC(nil, 0).
- heightC(t(_, L, R), H):-heightC(L, H1), heightC(R, H2), max(H1, H2, H3), H is H3+1.
- collect(nil,_,_,_).
- collect(t(K,L,R),WantedHeight,LocalHeight,[K|List]):-LocalHeight is WantedHeight,!,LocalHeight1 is LocalHeight - 1,
- collect(L,WantedHeight,LocalHeight1,List1),
- collect(R,WantedHeight,LocalHeight1,List2),
- append(List1,List2,List).
- collect(t(K,L,R),WantedHeight,LocalHeight,List):-LocalHeight1 is LocalHeight - 1,
- collect(L,WantedHeight,LocalHeight1,List1),
- collect(R,WantedHeight,LocalHeight1,Lis2),
- append(List1,List2,List).
- collectFinal(T,WantedHeight,List):-heightC(T,Height),collect(T,WantedHeight,Height,List).
- copacel(t(6, t(4, t(2, nil, nil), t(5, nil, nil)), t(9, t(7, nil, nil), nil))).
- %%%%%%%% Sum from all elems from root to leaf
- rootleaf(nil,_,[]).
- rootleaf(t(K,nil,nil),S,[Res]):-
- Res is K+S.
- rootleaf(t(K,L,R),S,Res):-
- S1 is S+K,
- rootleaf(L,S1,R1),
- S2 is S+K,
- rootleaf(R,S2,R2),
- append(R1,R2,Res).
- %%%%%%%%%%%%%%
- rootleaf(nil,_,LS,LS).
- rootleaf(t(K,nil,nil),S,[Res|LS],LS):-
- Res is K+S.
- rootleaf(t(K,L,R),S,LS,LE):-
- S1 is S+K,
- rootleaf(L,S1,LS,LM),
- S2 is S+K,
- rootleaf(R,S2,LM,LE).
- %%%%%%%prime factors
- decompose(Nr,Nr,Count,[[Nr,Count1]]):-Count1 is Count + 1.
- decompose(Nr,Divider,Count,R):-0 is Nr mod Divider,!,Nr1 is Nr//Divider,Count1 is Count + 1,decompose(Nr1,Divider,Count1,R).
- decompose(Nr,Divider,Count,[[Divider,Count]|R]):- Divider1 is Divider + 1,decompose(Nr,Divider1,0,R).
- decomposee(Nr,R):-decompose(Nr,2,0,R).
- %%%%%%%%%%%%%
- maxim([],MIN,MIN).
- maxim([H|T],MIN,R):-H>MIN,!,MINP is H,maxim(T,MINP,R).
- maxim([H|T],MIN,R):-maxim(T,MIN,R).
- find_maxi(L,M,M):-var(L),!.
- find_maxi([H|T],MP,M):-maxim(H,0,MPP),MPP>MP,!,MP1 is MPP,find_maxi(T,MP1,M).
- find_maxi([H|T],MP,M):-find_maxi(T,MP,M).
- member_il(_, L):-var(L), !, fail.
- % these 2 clauses are the same as for the member1 predicate
- member_il(X, [X|_]):-!.
- member_il(X, [_|T]):-member_il(X, T).
- maxfinal(L,_,Partial,Partial):-var(L),!.
- maxfinal([H|T],M,Partial,R):-member_il(M,H),!,append(H,Partial,Partial1),maxfinal(T,M,Partial1,R).
- maxfinal([H|T],M,Partial,R):-maxfinal(T,M,Partial,R).
- maxfinalCall(L,R):-find_maxi(L,0,M),maxfinal(L,M,[],R).
- %%%%%%%%%%%%%%%%%%%%%%%%
- collect_tmiddle(nil,[],LS).
- collect_tmiddle(t(K,L,nil,R),LS,LE):-collect_tmiddle(L,LS,LM),collect_tmiddle(R,LM,LE).
- collect_tmiddle(t(K,L,M,R),[K|LS],LE):-collect_tmiddle(L,LS,LM),collect_tmiddle(M,LM,LX),collect_tmiddle(R,LX,LE).
- copacelul(t(2,t(8,nil,nil,nil),t(3,nil,nil,t(1,nil,1,nil)),t(5,t(7,nil,nil,nil),t(6,nil,nil,nil),t(1,nil,nil,t(9,nil,10,11))))).
- %1. Count the number of lists in a deep list.
- count_lists([],0).
- count_lists([H|T],R):-atomic(H),count_lists(T,R).
- count_lists([H|T], R):-count_lists(H, RH),count_lists(T, RT),R is RH + RT + 1.
- append1([], L, L).
- append1([H|T], L, [H|R]):-append(T, L, R).
- %2. Double the odd numbers and square the even.
- numbers([],[]).
- numbers([H|T],[H*H|R]):- 0 is H mod 2,numbers(T,R).
- numbers([H|T],[2*H|R]):- 1 is H mod 2,numbers(T,R).
- %3. Convert a number to binary
- dec_bin(0,'0').
- dec_bin(1,'1').
- dec_bin(N,B):-N>1,X is N mod 2,Y is N//2,dec_bin(Y,B1),atom_concat(B1, X, B).
- %5. Delete the occurrences of x on even positions
- del_pos_even([],_,_,[]).
- del_pos_even([H|T],C,H,R):-0 is C mod 2,!,C1 is C+1,del_pos_even(T,C1,H,R).
- del_pos_even([H|T],C,Nr,[H|R]):-C1 is C+1,del_pos_even(T,C1,Nr,R).
- %6 Compute the divisors of a natural number.
- divisor(NR,NR,DIVS,[NR|DIVS]):-!.
- divisor(NR,ACC,DIVS,R):-0 is NR mod ACC,!,ACC1 is ACC + 1,append([ACC],DIVS,DIVS1),divisor(NR,ACC1,DIVS1,R).
- divisor(NR,ACC,DIVS,R):-ACC1 is ACC+1,divisor(NR,ACC1,DIVS,R).
- %7. Reverse a natural number.
- reverse_n(0,0).
- reverse_n(Nr,R1):-M is Nr mod 10, D is Nr div 10,reverse_n(D,R),atom_concat(M,R,R1).
- %8. Delete each kth element from the end of the list.
- delete_k([],_,_,[]).
- delete_k([H|T],K,Count,R):-Count is K-1,!,delete_k(T,K,0,R).
- delete_k([H|T],K,Count,[H|R]):-Count1 is Count + 1, delete_k(T,K,Count1,R).
- reverse_list([],[]).
- reverse_list([H|T],R):-reverse(T,R1),append(R1,[H],R).
- delete_kend(L,K,Count,R):-reverse_list(L,NewL),delete_k(NewL,K,Count,R1),reverse_list(R1,R).
- % 9. Separate the even elements on odd positions from the rest
- separate1([],_,[],[]).
- separate1([H|T],Count,[H|Even],Rest):-0 is H mod 2,1 is Count mod 2,!,Count1 is Count + 1,separate1(T,Count1,Even,Rest).
- separate1([H|T],Count,Even,[H|Rest]):-Count1 is Count + 1,separate1(T,Count1,Even,Rest).
- separate11(L,Even,Rest):-separate1(L,1,Even,Rest).
- %10. Binary incomplete tree. Collect odd nodes with 1 child in an incomplete list.
- collect_odd_from_1child(t(_, L, R), _):-
- var(L),
- var(R), !.
- collect_odd_from_1child(t(K, N, R), [K|List]):-
- var(N),
- 1 is K mod 2, !,
- collect_odd_from_1child(R, List).
- collect_odd_from_1child(t(K, L, N), List):-
- var(N),
- 1 is K mod 2, !,
- collect_odd_from_1child(L, List),
- insert_il(K, List).
- collect_odd_from_1child(t(_, L, R), List):-
- collect_odd_from_1child(L, LL),
- collect_odd_from_1child(R, RL),
- append_il(LL, RL),
- List = LL.
- insert_il(X, L):-
- var(L), !,
- L = [X|_].
- insert_il(X, [_|T]):-
- insert_il(X, T).
- append_il(L1, L2):-
- var(L1), !,
- L1 = L2.
- append_il([_|T1], L2):-
- append_il(T1, L2).
- %12. Binary Tree. Collect even keys from leaves in a difference list.
- collect_even_from_leaf(nil,L,L).
- collect_even_from_leaf(t(K,nil,nil),[K|LS],LS):-0 is K mod 2,!.
- collect_even_from_leaf(t(K,L,R),LS,LE):-collect_even_from_leaf(L,LS,LM),collect_even_from_leaf(R,LM,LE).
- %14. Collect all the nodes at odd depth from a binary incomplete tree (the root has depth 0).
- collect_all_odd_depth(N, _, []):-
- var(N),!.
- collect_all_odd_depth(t(K, L, R), N, [K|List]):-
- 1 is N mod 2, !,
- N1 is N + 1,
- collect_all_odd_depth(L, N1, LL),
- collect_all_odd_depth(R, N1, LR),
- append(LL, LR, List).
- collect_all_odd_depth(t(_, L, R), N, List):-
- N1 is N + 1,
- collect_all_odd_depth(L, N1, LL),
- collect_all_odd_depth(R, N1, LR),
- append(LL, LR, List).
- %15. Flatten only the elements at depth X from a deep list.
- max(A,B,A):-A>B,!.
- max(A,B,B).
- depth([],1).
- depth([H|T],R):-atomic(H),!,depth(T,R).
- depth([H|T],R):- depth(H,R1), depth(T,R2), R3 is R1+1, max(R3,R2,R).
- flatten([],[]).
- flatten([H|T], [H|R]) :- atomic(H),!, flatten(T,R).
- flatten([H|T], R) :- flatten(H,R1), flatten(T,R2), append(R1,R2,R).
- flatten_depth([],_,_,[]).
- flatten_depth([H|T],Depth,Count,[H|R]):-atomic(H),!,Count is Depth,flatten_depth(T,Depth,Count,R).
- flatten_depth([H|T],Depth,Count,R):-atomic(H),!,flatten_depth(T,Depth,Count,R).
- flatten_depth([H|T],Depth,Count,R):-Count1 is Count +1,flatten_depth(H,Depth,Count1,R1),flatten_depth(T,Depth,Count,R2),append(R1,R2,R).
- %16. Delete duplicate elements that are on an odd position in a list
- %count_dups(List,Elemen,Count,R)
- count_dups([],El,Count,[Count]).
- count_dups([H|T],H,Count,R):-Count1 is Count+1,count_dups(T,H,Count1,R).
- count_dups([H|T],El,Count,R):-count_dups(T,El,Count,R).
- %remove_odd(List,Elem,Count,R)
- remove_odd([],_,_,[]).
- remove_odd([H|T],H,Count,R):-1 is Count mod 2,!,Count1 is Count + 1, remove_odd(T,H,Count1,R).
- remove_odd([H|T],Elem,Count,[H|R]):-Count1 is Count + 1, remove_odd(T,Elem,Count1,R).
- %remove_odd_dups(List,R)
- remove_odd_dups([],[]).
- remove_odd_dups([H|T],[H|R]):-count_dups(T,H,0,C),C>1,remove_odd(T,H,0,NewT),remove_odd_dups(NewT,R).
- remove_odd_dups([H|T],[H|R]):-remove_odd_dups(T,R).
- %11. Ternary incomplete tree. Collect the keys between X and Y (closed interval) in a difference list.
- %collect_ternary(T,X,Y,R).
- collect_ternary(L,_,_,LS,LS):-var(L),!.
- collect_ternary(t(K,L,M,R),X,Y,[K|LS],LE):-K>=X,K=<Y,!,collect_ternary(L,X,Y,LS,RE1),collect_ternary(M,X,Y,RE1,RE2),collect_ternary(R,X,Y,RE2,LE).
- collect_ternary(t(K,L,M,R),X,Y,LS,LE):-collect_ternary(L,X,Y,LS,RE1),collect_ternary(M,X,Y,RE1,RE2),collect_ternary(R,X,Y,RE2,LE).
- tree(t(2,t(8,_,_,_),t(3,_,_,t(4,_,_,_)),t(5,t(7,_,_,_),t(6,_,_,_),t(1,_,_,t(9,_,_,_))))).
- %13. Replace the min element from a ternary incomplete tree with the root.
- inorder_ternary(T,_):-var(T),!.
- inorder_ternary(t(K,L,M,R), List):-inorder_ternary(L,LL), inorder_ternary(M,LM), inorder_ternary(R,RL),
- append(LL, [K|LM],List1), append(List1,RL,List).
- minim([],MIN,MIN).
- minim([H|T],MIN,R):-H<MIN,!,MINP is H,minim(T,MINP,R).
- minim([H|T],MIN,R):-minim(T,MIN,R).
- min_ternary(T,List):-inorder_ternary(T,L),minim(L,100,List).
- %copac(t(2,t(8,_,_,_),t(3,_,_,t(1,_,_,_)),t(5,t(7,_,_,_),t(6,_,_,_),t(1,_,_,t(9,_,_,_))))).
- replacemin(t(K,L,M,R),t(NK,NL,NM,NR)):-min_ternary(t(K,L,M,R),Min),replace_min_tree(t(K,L,M,R),Min,K,t(NK,NL,NM,NR)).
- replace_min_tree(L,_,_,_):-var(L),!.
- replace_min_tree(t(K,L,M,R),Min,Root,t(Root,NL,NM,NR)):-Min=K,!,
- replace_min_tree(L,Min,Root,NL),
- replace_min_tree(M,Min,Root,NM),
- replace_min_tree(R,Min,Root,NR).
- replace_min_tree(t(K,L,M,R),Min,Root,t(K,NL,NM,NR)):-
- replace_min_tree(L,Min,Root,NL),
- replace_min_tree(M,Min,Root,NM),
- replace_min_tree(R,Min,Root,NR).
- %18. Replace each node with its height in a binary incomplete tree
- % predicate which computes the maximum between 2 numbers
- max(A, B, A):-A>B, !.
- max(_, B, B).
- % predicate which computes the height of a binary tree
- height(L, 0):-var(L),!.
- height(t(_, L, R), H):-height(L, H1), height(R, H2), max(H1, H2, H3), H is H3+1.
- replaceheight(T,R):-height(T,H),replace_height(T,H,R).
- replace_height(L,_,L):-var(L).
- replace_height(t(K,L,R),Height,t(Height,NL,NR)):-H1 is Height-1,replace_height(L,H1,NL),replace_height(R,H1,NR).
- altcopac(t(2,t(4,t(5,_,_),t(7,_,_)),t(3,t(0,t(4,_,_),_),t(8,_,t(5,_,_))))).
- %21. Encode a list with RLE.
- encode([],X,Count,[[X,Count]]).
- encode([H|T],H,Count,R):-Count1 is Count + 1,encode(T,H,Count1,R).
- encode([H|T],X,Count,[[X,Count]|R]):-encode(T,H,1,R).
- rle_encode([H|T],R):-encode(T,H,1,R).
- %20. Decode a list encoded with RLE.
- printn([Nr,Count],Count,List,[List]).
- printn([Nr,Count],Acc,List,R):-Acc<Count,Acc1 is Acc + 1, append([Nr],List,List1),printn([Nr,Count],Acc1,List1,R).
- decode([],List,[List]).
- decode([[Nr,Count]|T],List,R):-printn([Nr,Count],0,[],R1),append(List,R1,R2),decode(T,R2,R).
- %[1,2,3,4]
- %[5,6,4,6]
- %[4,5,6,3]
- %[1,2,3,3]
- %[1,5,4,1]
- %[2,6,5,2]
- %[3,4,6,3]
- %[4,6,3,3]
- extractk([],_,_,[]).
- extractk([H|T],Count,K,[H|R]):-Count is K,!,Count1 is Count + 1,extractk(T,Count1,K,R).
- extractk([H|T],Count,K,R):-Count1 is Count +1,extractk(T,Count1,K,R).
- %%%%%%%%%%%
- count_dups1(L,El,Count,[Count]):-var(L),!.
- count_dups1([H|T],H,Count,R):-Count1 is Count+1,count_dups1(T,H,Count1,R).
- count_dups1([H|T],El,Count,R):-count_dups1(T,El,Count,R).
- %%%%%%%%%%%%%%
- postorder(t(K,L,R),List):-postorder(L,LL),postorder(R,LR),append(LL,LR,Rez1),append(Rez1,[K],List).
- postorder(nil,[]).
- sum([],0).
- sum([H|T],R):-sum(T,T1),R is T1 + H.
- suma(L,Partial,Partial):-var(L),!.
- suma([H|T],Partial,R):-sum(H,Suma),append(Partial,[Suma],Partial1),suma(T,Partial1,R).
- sumaminim(L,M):-suma(L,[],R),minim(R,100,M).
- %In lista incompleta, nodurile de la o anumita inaltime
- heightC(nil, 0).
- heightC(t(_, L, R), H):-heightC(L, H1), heightC(R, H2), max(H1, H2, H3), H is H3+1.
- collect(nil,_,_,_).
- collect(t(K,L,R),WantedHeight,LocalHeight,[K|List]):-LocalHeight is WantedHeight,!,LocalHeight1 is LocalHeight - 1,
- collect(L,WantedHeight,LocalHeight1,List1),
- collect(R,WantedHeight,LocalHeight1,List2),
- append(List1,List2,List).
- collect(t(K,L,R),WantedHeight,LocalHeight,List):-LocalHeight1 is LocalHeight - 1,
- collect(L,WantedHeight,LocalHeight1,List1),
- collect(R,WantedHeight,LocalHeight1,Lis2),
- append(List1,List2,List).
- collectFinal(T,WantedHeight,List):-heightC(T,Height),collect(T,WantedHeight,Height,List).
- copacel(t(6, t(4, t(2, nil, nil), t(5, nil, nil)), t(9, t(7, nil, nil), nil))).
- %%%%%%%% Sum from all elems from root to leaf
- rootleaf(nil,_,[]).
- rootleaf(t(K,nil,nil),S,[Res]):-
- Res is K+S.
- rootleaf(t(K,L,R),S,Res):-
- S1 is S+K,
- rootleaf(L,S1,R1),
- S2 is S+K,
- rootleaf(R,S2,R2),
- append(R1,R2,Res).
- %%%%%%%%%%%%%%
- rootleaf(nil,_,LS,LS).
- rootleaf(t(K,nil,nil),S,[Res|LS],LS):-
- Res is K+S.
- rootleaf(t(K,L,R),S,LS,LE):-
- S1 is S+K,
- rootleaf(L,S1,LS,LM),
- S2 is S+K,
- rootleaf(R,S2,LM,LE).
- %%%%%%%prime factors
- decompose(Nr,Nr,Count,[[Nr,Count1]]):-Count1 is Count + 1.
- decompose(Nr,Divider,Count,R):-0 is Nr mod Divider,!,Nr1 is Nr//Divider,Count1 is Count + 1,decompose(Nr1,Divider,Count1,R).
- decompose(Nr,Divider,Count,[[Divider,Count]|R]):- Divider1 is Divider + 1,decompose(Nr,Divider1,0,R).
- decomposee(Nr,R):-decompose(Nr,2,0,R).
- %%%%%%%%%%%%%
- maxim([],MIN,MIN).
- maxim([H|T],MIN,R):-H>MIN,!,MINP is H,maxim(T,MINP,R).
- maxim([H|T],MIN,R):-maxim(T,MIN,R).
- find_maxi(L,M,M):-var(L),!.
- find_maxi([H|T],MP,M):-maxim(H,0,MPP),MPP>MP,!,MP1 is MPP,find_maxi(T,MP1,M).
- find_maxi([H|T],MP,M):-find_maxi(T,MP,M).
- member_il(_, L):-var(L), !, fail.
- % these 2 clauses are the same as for the member1 predicate
- member_il(X, [X|_]):-!.
- member_il(X, [_|T]):-member_il(X, T).
- maxfinal(L,_,Partial,Partial):-var(L),!.
- maxfinal([H|T],M,Partial,R):-member_il(M,H),!,append(H,Partial,Partial1),maxfinal(T,M,Partial1,R).
- maxfinal([H|T],M,Partial,R):-maxfinal(T,M,Partial,R).
- maxfinalCall(L,R):-find_maxi(L,0,M),maxfinal(L,M,[],R).
- %%%%%%%%%%%%%%%%%%%%%%%%
- collect_tmiddle(nil,[],LS).
- collect_tmiddle(t(K,L,nil,R),LS,LE):-collect_tmiddle(L,LS,LM),collect_tmiddle(R,LM,LE).
- collect_tmiddle(t(K,L,M,R),[K|LS],LE):-collect_tmiddle(L,LS,LM),collect_tmiddle(M,LM,LX),collect_tmiddle(R,LX,LE).
- copacelul(t(2,t(8,nil,nil,nil),t(3,nil,nil,t(1,nil,1,nil)),t(5,t(7,nil,nil,nil),t(6,nil,nil,nil),t(1,nil,nil,t(9,nil,10,11))))).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement