Advertisement
Guest User

MT 2016/2017

a guest
Nov 18th, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.82 KB | None | 0 0
  1. :- use_module(library(lists)).
  2. :- dynamic film/4.
  3.  
  4. %film(Title, Categories, Duration, AvgClassification).
  5. film( 'Doctor Strange',[action, adventure, fantasy], 115, 7.6).
  6. film( 'Hacksaw Ridge',[biography, drama, romance], 131, 8.7).
  7. film( 'Inferno',[action, adventure, crime], 121, 6.4).
  8. film( 'Arrival',[drama, mystery, scifi], 116, 8.5).
  9. film( 'The Accountant',[action, crime, drama], 127, 7.6).
  10. film( 'The Girl on the Train',[drama, mystery, thriller] , 112, 6.7).
  11.  
  12. %user(Username, YearOfBirth, Country).
  13. user(john, 1992, 'USA').
  14. user(jack, 1989, 'UK').
  15. user(peter, 1983, 'Portugal').
  16. user(harry, 1993, 'USA').
  17. user(richard, 1982, 'USA').
  18.  
  19. vote(john, ['Inferno'-7, 'Doctor Strange'-9, 'The Accountant'-6]).
  20. vote(jack, ['Inferno'-8, 'Doctor Strange'-8, 'The Accountant'-7]).
  21. vote(peter, ['The Accountant'-4, 'Hacksaw Ridge'-7, 'The Girl on the Train'-3]).
  22. vote(harry, ['Inferno'-7, 'The Accountant'-6]).
  23. vote(richard, ['Inferno'-10, 'Hacksaw Ridge'-10, 'Arrival'-9]).
  24.  
  25. % EX 2
  26.  
  27. diff(User1, User2, Difference, Film) :-
  28. vote(User1, L1),
  29. vote(User2, L2),
  30. list(L1, Film, Pontuation1),
  31. list(L2, Film, Pontuation2),
  32. Difference is abs(Pontuation1-Pontuation2).
  33.  
  34. list([Film-Score|Tail], Title, Pontuation) :-
  35. (Title == Film -> Pontuation = Score ; list(Tail, Title, Pontuation))
  36. .
  37.  
  38. % EX 3
  39.  
  40. niceGuy(User) :-
  41. vote(User, L),
  42. listScore(L, 0, P),
  43. P >= 2.
  44.  
  45. listScore([_-Score|Tail], P, PP) :-
  46. niceScore(Score, P, Prev),
  47. listScore(Tail, Prev, PP).
  48.  
  49. listScore([], P, PP) :-
  50. PP is P.
  51.  
  52. niceScore(Score, P, PP) :-
  53. Score > 8,
  54. PP is P + 1.
  55.  
  56. niceScore(_,P,PP) :-
  57. PP is P.
  58.  
  59. % EX: 4
  60.  
  61. elemsComuns(L1,L,L2) :-
  62. comunsList(L1,L,[],L2).
  63.  
  64. comunsList([], R, R, _).
  65. comunsList([H1|R1], R, Tmp, L2) :-
  66. member(H1, L2),
  67. append(Tmp, [H1], NewTmp),
  68. comunsList(R1, R, NewTmp, L2).
  69. comunsList([_|R1], R, Tmp, L2) :-
  70. comunsList(R1, R, Tmp, L2).
  71.  
  72. % EX: 5
  73.  
  74. printCategory(Category) :-
  75. film(Name, Categories, Duration, Score),
  76. checkCategory(Name, Categories, Duration, Score, Category),
  77. fail.
  78. printCategory(_).
  79.  
  80. checkCategory(Name, Categories, Duration, Score, Category) :-
  81. memberchk(Category, Categories),
  82. printFilm(Name, Duration, Score).
  83.  
  84. printFilm(Name, Duration, Score) :-
  85. write(Name),
  86. write(' ('),
  87. write(Duration),
  88. write('min, '),
  89. write(Score),
  90. write('10)'),
  91. nl.
  92.  
  93. % EX: 6
  94.  
  95. similarity(Film1, Film2, Similarity) :-
  96. percDurrScoreCat(Film1, Film2, Percent, Durr, Score),
  97. Similarity is Percent - 3 * Durr - 5 * Score.
  98.  
  99. percDurrScoreCat(Film1, Film2, Percent, Durr, Score) :-
  100. film(Film1, Categories1, Duration1, Score1),
  101. film(Film2, Categories2, Duration2, Score2),
  102. percent(Categories1, Categories2, Percent),
  103. Durr is abs(Duration1 - Duration2),
  104. Score is abs(Score1 - Score2)
  105. .
  106.  
  107. percent(Categories1, Categories2, Percent) :-
  108. elemsComuns(Categories1, CatCommon, Categories2),
  109. length(Categories1, X),
  110. length(Categories2, Y),
  111. length(CatCommon, Z),
  112. Percent is Z / (X - Z + Y) * 100
  113. .
  114.  
  115. % EX: 7
  116. mostSimilar(Film, Similarity, Films) :-
  117. findall(Name, film(Name,_,_,_), Bag),
  118. delete(Bag, Film, FilmsBag),
  119. mostSimilarCycle(Film, FilmsBag, 0, Similarity, Films,[])
  120. .
  121.  
  122. mostSimilarCycle(_,[],S,S,F,F).
  123. mostSimilarCycle(Film, [H|T], Sim_Tmp, Similarity, Films, FilmsTmp) :-
  124. similarity(Film, H, Sim),!,
  125. compareSimilarity(Sim, Sim_Tmp, SimNewTmp, H, FilmsTmp, FilmsNewTmp),
  126. mostSimilarCycle(Film, T, SimNewTmp, Similarity, Films, FilmsNewTmp)
  127. .
  128.  
  129. compareSimilarity(Sim, First_Sim, Similarity, Film, _, [Film]) :-
  130. Sim > First_Sim,
  131. Similarity = Sim.
  132.  
  133. compareSimilarity(Sim, First_Sim, First_Sim, Film, Films, FilmsTmp) :-
  134. Sim = First_Sim,
  135. append(Films, [Film], FilmsTmp).
  136.  
  137. compareSimilarity(_,S,S,_,F,F).
  138.  
  139. % EX: 8 (sem findall)
  140. distancia(User1, Distancia, User2) :-
  141. vote(User1, List1),
  142. vote(User2, List2),
  143. avgDiff(List1, List2, AvgDiff, 0, 0),
  144. ageDiffCountry(User1, User2, AgeDiff, CountryDiff),
  145. Distancia is AvgDiff + AgeDiff/3 + CountryDiff.
  146.  
  147. avgDiff([],_,A,A,_).
  148. avgDiff([H|T], List2, AvgDiff, AvgDiffTmp, N) :-
  149. avgDiff2(H, List2, AvgDiffTmp, AvgDiffNewTmp, N, NTmp),
  150. avgDiff(T, List2, AvgDiff, AvgDiffNewTmp, NTmp).
  151.  
  152.  
  153. avgDiff2(_,[],A,A,N,N).
  154. avgDiff2(Pair1, [Pair2|T], AvgDiffTmp, AvgDiff, N, NRet) :-
  155. updateAvg(Pair1, Pair2, AvgDiffTmp, AvgDiffNewTmp, N, NTmp),
  156. avgDiff2(Pair1, T, AvgDiffNewTmp, AvgDiff, NTmp, NRet).
  157.  
  158. updateAvg(Film1-Vote1, Film2-Vote2, AvgDiff, AvgDiffTmp, N, NTmp) :-
  159. Film1 = Film2,
  160. NTmp is N + 1,
  161. AvgDiffTmp is (AvgDiff * N + abs(Vote1-Vote2)) / NTmp.
  162.  
  163. updateAvg(_,_,A,A,N,N).
  164. %END (sem findall)
  165.  
  166. % EX: 8 (com findall)
  167. distanciaFindAll(User1, Distancia, User2) :-
  168. vote(User1, List1),
  169. vote(User2, List2),
  170. findall(VoteDiff, (member(Film-Vote1, List1), member(Film-Vote2, List2), VoteDiff is abs(Vote1 -Vote2)), Result),
  171. avgList(Result, AvgDiff),
  172. ageDiffCountry(User1, User2, AgeDiff, CountryDiff),
  173. Distancia is AvgDiff + AgeDiff/3 + CountryDiff.
  174.  
  175. avgList(Result, AvgDiff) :-
  176. listSum(Result, Sum),
  177. length(Result, Size),
  178. AvgDiff is Sum / Size.
  179.  
  180. listSum([], 0) :- !.
  181. listSum([X | List], NSum) :-
  182. listSum(List, Sum),
  183. NSum is Sum + X.
  184. %END (com findall)
  185.  
  186. ageDiffCountry(User1, User2, AgeDiff, CountryDiff) :-
  187. user(User1, Age1, Country1),
  188. user(User2, Age2, Country2),
  189. AgeDiff is abs(Age2 - Age1),
  190. countryDiff(Country1, Country2, CountryDiff).
  191.  
  192. countryDiff(Country1, Country2, CountryDiff) :-
  193. Country1 = Country2,
  194. CountryDiff is 0.
  195.  
  196. countryDiff(_,_,CountryDiff) :-
  197. CountryDiff is 2.
  198.  
  199.  
  200. %EX9
  201. update(Film) :-
  202. findall(Vote, (vote(_,List),member(Film-Vote, List)), AllVotes),
  203. listSum(AllVotes, Sum),
  204. length(AllVotes, Size),
  205. Score is Sum/Size,
  206. retract(film(Film,Categories,Duration,_)),
  207. assert(film(Film,Categories,Duration,Score)).
  208.  
  209. %EX11
  210. move(Row/Col, Celulas) :-
  211. findall(R/C, (horseMove(Row, Col, R, C), validRowCol(R,C)), Celulas).
  212.  
  213. validRowCol(R,C) :-
  214. R =< 8, R > 0,
  215. C =< 8, C > 0.
  216.  
  217. possibleMove(-1).
  218. possibleMove(1).
  219. possibleMove(-2).
  220. possibleMove(2).
  221.  
  222. horseMove(Row, Col, R, C) :-
  223. possibleMove(X1),
  224. possibleMove(X2),
  225. X1 \= X2,
  226. R is Row + X1,
  227. C is Col + X2.
  228.  
  229. %EX: 12
  230. podeMoverEmN(Row/Col, N, Celulas) :-
  231. N > 0,
  232. move(Row/Col, C),
  233. N1 is N - 1,
  234. findall(ResCells, (member(TmpCell, C), podeMoveEmN(TmpCell, N1, ResCells)), TotalCells),
  235. append(TotalCells, C2),
  236. append(C, C2, C3),
  237. sort(C3, Celulas).
  238.  
  239. %EX: 13
  240. minJogadas(Initial, Final, N) :-
  241. minJogadas(Initial, Final, 1, N).
  242.  
  243. minJogadas(Initial, Final, Counter, N) :-
  244. podeMoverEmN(Initial, Counter, Cells),
  245. (member(Final, Cells) ->
  246. N is Counter;
  247. minJogadas(Initial, Final, Counter + 1, N)).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement