Advertisement
UTCN

coloq pl

Dec 19th, 2018
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.58 KB | None | 0 0
  1. %1. Count the number of lists in a deep list.
  2. count_lists([],0).
  3. count_lists([H|T],R):-atomic(H),count_lists(T,R).
  4. count_lists([H|T], R):-count_lists(H, RH),count_lists(T, RT),R is RH + RT + 1.
  5.  
  6. append1([], L, L).
  7. append1([H|T], L, [H|R]):-append(T, L, R).
  8. %2. Double the odd numbers and square the even.
  9. numbers([],[]).
  10. numbers([H|T],[H*H|R]):- 0 is H mod 2,numbers(T,R).
  11. numbers([H|T],[2*H|R]):- 1 is H mod 2,numbers(T,R).
  12. %3. Convert a number to binary
  13. dec_bin(0,'0').
  14. dec_bin(1,'1').
  15. dec_bin(N,B):-N>1,X is N mod 2,Y is N//2,dec_bin(Y,B1),atom_concat(B1, X, B).
  16.  
  17. %5. Delete the occurrences of x on even positions
  18. del_pos_even([],_,_,[]).
  19. del_pos_even([H|T],C,H,R):-0 is C mod 2,!,C1 is C+1,del_pos_even(T,C1,H,R).
  20. del_pos_even([H|T],C,Nr,[H|R]):-C1 is C+1,del_pos_even(T,C1,Nr,R).
  21.  
  22.  
  23. %6 Compute the divisors of a natural number.
  24. divisor(NR,NR,DIVS,[NR|DIVS]):-!.
  25. divisor(NR,ACC,DIVS,R):-0 is NR mod ACC,!,ACC1 is ACC + 1,append([ACC],DIVS,DIVS1),divisor(NR,ACC1,DIVS1,R).
  26. divisor(NR,ACC,DIVS,R):-ACC1 is ACC+1,divisor(NR,ACC1,DIVS,R).
  27.  
  28.  
  29. %7. Reverse a natural number.
  30. reverse_n(0,0).
  31. reverse_n(Nr,R1):-M is Nr mod 10, D is Nr div 10,reverse_n(D,R),atom_concat(M,R,R1).
  32.  
  33. %8. Delete each kth element from the end of the list.
  34. delete_k([],_,_,[]).
  35. delete_k([H|T],K,Count,R):-Count is K-1,!,delete_k(T,K,0,R).
  36. delete_k([H|T],K,Count,[H|R]):-Count1 is Count + 1, delete_k(T,K,Count1,R).
  37. reverse_list([],[]).
  38. reverse_list([H|T],R):-reverse(T,R1),append(R1,[H],R).
  39. delete_kend(L,K,Count,R):-reverse_list(L,NewL),delete_k(NewL,K,Count,R1),reverse_list(R1,R).
  40.  
  41. % 9. Separate the even elements on odd positions from the rest
  42. separate1([],_,[],[]).
  43. 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).
  44. separate1([H|T],Count,Even,[H|Rest]):-Count1 is Count + 1,separate1(T,Count1,Even,Rest).
  45.  
  46. separate11(L,Even,Rest):-separate1(L,1,Even,Rest).
  47.  
  48.  
  49. %10. Binary incomplete tree. Collect odd nodes with 1 child in an incomplete list.
  50. collect_odd_from_1child(t(_, L, R), _):-
  51. var(L),
  52. var(R), !.
  53. collect_odd_from_1child(t(K, N, R), [K|List]):-
  54. var(N),
  55. 1 is K mod 2, !,
  56. collect_odd_from_1child(R, List).
  57. collect_odd_from_1child(t(K, L, N), List):-
  58. var(N),
  59. 1 is K mod 2, !,
  60. collect_odd_from_1child(L, List),
  61. insert_il(K, List).
  62. collect_odd_from_1child(t(_, L, R), List):-
  63. collect_odd_from_1child(L, LL),
  64. collect_odd_from_1child(R, RL),
  65. append_il(LL, RL),
  66. List = LL.
  67.  
  68. insert_il(X, L):-
  69. var(L), !,
  70. L = [X|_].
  71. insert_il(X, [_|T]):-
  72. insert_il(X, T).
  73.  
  74. append_il(L1, L2):-
  75. var(L1), !,
  76. L1 = L2.
  77. append_il([_|T1], L2):-
  78. append_il(T1, L2).
  79.  
  80. %12. Binary Tree. Collect even keys from leaves in a difference list.
  81.  
  82. collect_even_from_leaf(nil,L,L).
  83. collect_even_from_leaf(t(K,nil,nil),[K|LS],LS):-0 is K mod 2,!.
  84. collect_even_from_leaf(t(K,L,R),LS,LE):-collect_even_from_leaf(L,LS,LM),collect_even_from_leaf(R,LM,LE).
  85.  
  86. %14. Collect all the nodes at odd depth from a binary incomplete tree (the root has depth 0).
  87. collect_all_odd_depth(N, _, []):-
  88. var(N),!.
  89. collect_all_odd_depth(t(K, L, R), N, [K|List]):-
  90. 1 is N mod 2, !,
  91. N1 is N + 1,
  92. collect_all_odd_depth(L, N1, LL),
  93. collect_all_odd_depth(R, N1, LR),
  94. append(LL, LR, List).
  95. collect_all_odd_depth(t(_, L, R), N, List):-
  96. N1 is N + 1,
  97. collect_all_odd_depth(L, N1, LL),
  98. collect_all_odd_depth(R, N1, LR),
  99. append(LL, LR, List).
  100.  
  101. %15. Flatten only the elements at depth X from a deep list.
  102. max(A,B,A):-A>B,!.
  103. max(A,B,B).
  104.  
  105. depth([],1).
  106. depth([H|T],R):-atomic(H),!,depth(T,R).
  107. depth([H|T],R):- depth(H,R1), depth(T,R2), R3 is R1+1, max(R3,R2,R).
  108.  
  109. flatten([],[]).
  110. flatten([H|T], [H|R]) :- atomic(H),!, flatten(T,R).
  111. flatten([H|T], R) :- flatten(H,R1), flatten(T,R2), append(R1,R2,R).
  112.  
  113. flatten_depth([],_,_,[]).
  114. flatten_depth([H|T],Depth,Count,[H|R]):-atomic(H),!,Count is Depth,flatten_depth(T,Depth,Count,R).
  115. flatten_depth([H|T],Depth,Count,R):-atomic(H),!,flatten_depth(T,Depth,Count,R).
  116. 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).
  117.  
  118. %16. Delete duplicate elements that are on an odd position in a list
  119. %count_dups(List,Elemen,Count,R)
  120. count_dups([],El,Count,[Count]).
  121. count_dups([H|T],H,Count,R):-Count1 is Count+1,count_dups(T,H,Count1,R).
  122. count_dups([H|T],El,Count,R):-count_dups(T,El,Count,R).
  123.  
  124. %remove_odd(List,Elem,Count,R)
  125. remove_odd([],_,_,[]).
  126. remove_odd([H|T],H,Count,R):-1 is Count mod 2,!,Count1 is Count + 1, remove_odd(T,H,Count1,R).
  127. remove_odd([H|T],Elem,Count,[H|R]):-Count1 is Count + 1, remove_odd(T,Elem,Count1,R).
  128.  
  129. %remove_odd_dups(List,R)
  130. remove_odd_dups([],[]).
  131. 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).
  132. remove_odd_dups([H|T],[H|R]):-remove_odd_dups(T,R).
  133.  
  134. %11. Ternary incomplete tree. Collect the keys between X and Y (closed interval) in a difference list.
  135.  
  136. %collect_ternary(T,X,Y,R).
  137. collect_ternary(L,_,_,LS,LS):-var(L),!.
  138. 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).
  139. 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).
  140.  
  141. tree(t(2,t(8,_,_,_),t(3,_,_,t(4,_,_,_)),t(5,t(7,_,_,_),t(6,_,_,_),t(1,_,_,t(9,_,_,_))))).
  142.  
  143.  
  144. %13. Replace the min element from a ternary incomplete tree with the root.
  145. inorder_ternary(T,_):-var(T),!.
  146. inorder_ternary(t(K,L,M,R), List):-inorder_ternary(L,LL), inorder_ternary(M,LM), inorder_ternary(R,RL),
  147. append(LL, [K|LM],List1), append(List1,RL,List).
  148.  
  149.  
  150. minim([],MIN,MIN).
  151. minim([H|T],MIN,R):-H<MIN,!,MINP is H,minim(T,MINP,R).
  152. minim([H|T],MIN,R):-minim(T,MIN,R).
  153.  
  154. min_ternary(T,List):-inorder_ternary(T,L),minim(L,100,List).
  155.  
  156. %copac(t(2,t(8,_,_,_),t(3,_,_,t(1,_,_,_)),t(5,t(7,_,_,_),t(6,_,_,_),t(1,_,_,t(9,_,_,_))))).
  157.  
  158. 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)).
  159.  
  160. replace_min_tree(L,_,_,_):-var(L),!.
  161. replace_min_tree(t(K,L,M,R),Min,Root,t(Root,NL,NM,NR)):-Min=K,!,
  162. replace_min_tree(L,Min,Root,NL),
  163. replace_min_tree(M,Min,Root,NM),
  164. replace_min_tree(R,Min,Root,NR).
  165. replace_min_tree(t(K,L,M,R),Min,Root,t(K,NL,NM,NR)):-
  166. replace_min_tree(L,Min,Root,NL),
  167. replace_min_tree(M,Min,Root,NM),
  168. replace_min_tree(R,Min,Root,NR).
  169. %18. Replace each node with its height in a binary incomplete tree
  170. % predicate which computes the maximum between 2 numbers
  171. max(A, B, A):-A>B, !.
  172. max(_, B, B).
  173. % predicate which computes the height of a binary tree
  174. height(L, 0):-var(L),!.
  175. height(t(_, L, R), H):-height(L, H1), height(R, H2), max(H1, H2, H3), H is H3+1.
  176.  
  177. replaceheight(T,R):-height(T,H),replace_height(T,H,R).
  178.  
  179. replace_height(L,_,L):-var(L).
  180. 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).
  181. altcopac(t(2,t(4,t(5,_,_),t(7,_,_)),t(3,t(0,t(4,_,_),_),t(8,_,t(5,_,_))))).
  182.  
  183. %21. Encode a list with RLE.
  184. encode([],X,Count,[[X,Count]]).
  185. encode([H|T],H,Count,R):-Count1 is Count + 1,encode(T,H,Count1,R).
  186. encode([H|T],X,Count,[[X,Count]|R]):-encode(T,H,1,R).
  187.  
  188. rle_encode([H|T],R):-encode(T,H,1,R).
  189.  
  190. %20. Decode a list encoded with RLE.
  191. printn([Nr,Count],Count,List,[List]).
  192. printn([Nr,Count],Acc,List,R):-Acc<Count,Acc1 is Acc + 1, append([Nr],List,List1),printn([Nr,Count],Acc1,List1,R).
  193. decode([],List,[List]).
  194. decode([[Nr,Count]|T],List,R):-printn([Nr,Count],0,[],R1),append(List,R1,R2),decode(T,R2,R).
  195.  
  196.  
  197.  
  198.  
  199.  
  200. %[1,2,3,4]
  201. %[5,6,4,6]
  202. %[4,5,6,3]
  203. %[1,2,3,3]
  204.  
  205. %[1,5,4,1]
  206. %[2,6,5,2]
  207. %[3,4,6,3]
  208. %[4,6,3,3]
  209.  
  210.  
  211. extractk([],_,_,[]).
  212. extractk([H|T],Count,K,[H|R]):-Count is K,!,Count1 is Count + 1,extractk(T,Count1,K,R).
  213. extractk([H|T],Count,K,R):-Count1 is Count +1,extractk(T,Count1,K,R).
  214.  
  215.  
  216. %%%%%%%%%%%
  217. count_dups1(L,El,Count,[Count]):-var(L),!.
  218. count_dups1([H|T],H,Count,R):-Count1 is Count+1,count_dups1(T,H,Count1,R).
  219. count_dups1([H|T],El,Count,R):-count_dups1(T,El,Count,R).
  220. %%%%%%%%%%%%%%
  221. postorder(t(K,L,R),List):-postorder(L,LL),postorder(R,LR),append(LL,LR,Rez1),append(Rez1,[K],List).
  222. postorder(nil,[]).
  223.  
  224. sum([],0).
  225. sum([H|T],R):-sum(T,T1),R is T1 + H.
  226.  
  227. suma(L,Partial,Partial):-var(L),!.
  228. suma([H|T],Partial,R):-sum(H,Suma),append(Partial,[Suma],Partial1),suma(T,Partial1,R).
  229.  
  230. sumaminim(L,M):-suma(L,[],R),minim(R,100,M).
  231.  
  232. %In lista incompleta, nodurile de la o anumita inaltime
  233.  
  234.  
  235. heightC(nil, 0).
  236. heightC(t(_, L, R), H):-heightC(L, H1), heightC(R, H2), max(H1, H2, H3), H is H3+1.
  237.  
  238. collect(nil,_,_,_).
  239. collect(t(K,L,R),WantedHeight,LocalHeight,[K|List]):-LocalHeight is WantedHeight,!,LocalHeight1 is LocalHeight - 1,
  240. collect(L,WantedHeight,LocalHeight1,List1),
  241. collect(R,WantedHeight,LocalHeight1,List2),
  242. append(List1,List2,List).
  243. collect(t(K,L,R),WantedHeight,LocalHeight,List):-LocalHeight1 is LocalHeight - 1,
  244. collect(L,WantedHeight,LocalHeight1,List1),
  245. collect(R,WantedHeight,LocalHeight1,Lis2),
  246. append(List1,List2,List).
  247.  
  248. collectFinal(T,WantedHeight,List):-heightC(T,Height),collect(T,WantedHeight,Height,List).
  249.  
  250. copacel(t(6, t(4, t(2, nil, nil), t(5, nil, nil)), t(9, t(7, nil, nil), nil))).
  251.  
  252. %%%%%%%% Sum from all elems from root to leaf
  253. rootleaf(nil,_,[]).
  254. rootleaf(t(K,nil,nil),S,[Res]):-
  255. Res is K+S.
  256. rootleaf(t(K,L,R),S,Res):-
  257. S1 is S+K,
  258. rootleaf(L,S1,R1),
  259. S2 is S+K,
  260. rootleaf(R,S2,R2),
  261. append(R1,R2,Res).
  262. %%%%%%%%%%%%%%
  263.  
  264. rootleaf(nil,_,LS,LS).
  265. rootleaf(t(K,nil,nil),S,[Res|LS],LS):-
  266. Res is K+S.
  267. rootleaf(t(K,L,R),S,LS,LE):-
  268. S1 is S+K,
  269. rootleaf(L,S1,LS,LM),
  270. S2 is S+K,
  271. rootleaf(R,S2,LM,LE).
  272.  
  273. %%%%%%%prime factors
  274.  
  275. decompose(Nr,Nr,Count,[[Nr,Count1]]):-Count1 is Count + 1.
  276. decompose(Nr,Divider,Count,R):-0 is Nr mod Divider,!,Nr1 is Nr//Divider,Count1 is Count + 1,decompose(Nr1,Divider,Count1,R).
  277. decompose(Nr,Divider,Count,[[Divider,Count]|R]):- Divider1 is Divider + 1,decompose(Nr,Divider1,0,R).
  278.  
  279. decomposee(Nr,R):-decompose(Nr,2,0,R).
  280. %%%%%%%%%%%%%
  281. maxim([],MIN,MIN).
  282. maxim([H|T],MIN,R):-H>MIN,!,MINP is H,maxim(T,MINP,R).
  283. maxim([H|T],MIN,R):-maxim(T,MIN,R).
  284.  
  285. find_maxi(L,M,M):-var(L),!.
  286. find_maxi([H|T],MP,M):-maxim(H,0,MPP),MPP>MP,!,MP1 is MPP,find_maxi(T,MP1,M).
  287. find_maxi([H|T],MP,M):-find_maxi(T,MP,M).
  288.  
  289. member_il(_, L):-var(L), !, fail.
  290. % these 2 clauses are the same as for the member1 predicate
  291. member_il(X, [X|_]):-!.
  292. member_il(X, [_|T]):-member_il(X, T).
  293.  
  294. maxfinal(L,_,Partial,Partial):-var(L),!.
  295. maxfinal([H|T],M,Partial,R):-member_il(M,H),!,append(H,Partial,Partial1),maxfinal(T,M,Partial1,R).
  296. maxfinal([H|T],M,Partial,R):-maxfinal(T,M,Partial,R).
  297.  
  298. maxfinalCall(L,R):-find_maxi(L,0,M),maxfinal(L,M,[],R).
  299. %%%%%%%%%%%%%%%%%%%%%%%%
  300.  
  301. collect_tmiddle(nil,[],LS).
  302. collect_tmiddle(t(K,L,nil,R),LS,LE):-collect_tmiddle(L,LS,LM),collect_tmiddle(R,LM,LE).
  303. 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).
  304.  
  305. 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))))).
  306.  
  307. reverseL(L,R):-var(L),!.
  308. reverseL([H|T],R):-reverseL(T,T1),append(T1,[H],R).
  309.  
  310.  
  311. edge(1,2).
  312. edge(2,1).
  313. edge(1,4).
  314. edge(1,3).
  315. edge(3,2).
  316.  
  317. :- dynamic info/3.
  318.  
  319. increseIn(X):-retract(info(X,I,O)),!,I1 is I+1,
  320. assert(info(X,I1,O)).
  321. increseIn(X):-assert(info(X,1,0)).
  322.  
  323. increseOut(X):-retract(info(X,I,O)),!,O1 is O+1,
  324. assert(info(X,I,O1)).
  325. increseOut(X):-assert(info(X,0,1)).
  326.  
  327. build_info:-edge(X,Y),
  328. increseIn(Y),
  329. increseOut(X),
  330. fail.
  331. build_info.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement