Guest User

Untitled

a guest
Nov 17th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.65 KB | None | 0 0
  1. :- use_module(library(lists)).
  2. :- dynamic film/4.
  3.  
  4. % Factos
  5. film('A', [action, adventure, fantasy], 115, 7.6).
  6. film('B', [biography, drama, romance], 131, 8.7).
  7. film('C', [action, adventure, crime], 121, 6.4).
  8. film('D', [drama, mystery, scifi], 116, 8.5).
  9. film('E', [action, crime, drama], 127, 7.6).
  10. film('F', [drama, mystery, thriller], 112, 6.7).
  11.  
  12. user(john, 1992, 'USA').
  13. user(jack, 1989, 'UK').
  14. user(peter, 1983, 'portugal').
  15. user(harry, 1993, 'USA').
  16. user(richard, 1982, 'USA').
  17.  
  18. vote(john, ['C'-7, 'A'-9, 'E'-6]).
  19. vote(jack, ['C'-8, 'A'-8, 'E'-7]).
  20. vote(peter, ['E'-4, 'B'-7, 'F'-3]).
  21. vote(harry, ['C'-7, 'E'-6]).
  22. vote(richard, ['C'-10, 'B'-10, 'D'-9]).
  23.  
  24.  
  25. %% PERGUNTAS %%
  26.  
  27. % P1 curto(+)
  28. curto(Nome) :-
  29. film(Nome, _, Duracao, _),
  30. Duracao < 125.
  31.  
  32. % P2 diff(+, +, -, +)
  33. diff(User1, User2, Difference, Film) :-
  34. vote(User1, Votes1),
  35. vote(User2, Votes2),
  36. member(Film-Vote1, Votes1),
  37. member(Film-Vote2, Votes2),
  38. Difference is abs(Vote1 - Vote2).
  39.  
  40. % P3 niceGuy(+)
  41. niceGuy(User) :-
  42. vote(User, Votes),
  43. exclude(lessThan8, Votes, Result),
  44. length(Result, Len),
  45. Len >= 2.
  46.  
  47. lessThan8(_Key-Value) :-
  48. Value < 8.
  49.  
  50. % P4 elemsComuns(+, -, +)
  51. elemsComuns([], [], _) :- !.
  52. elemsComuns(_, [], []) :- !.
  53. elemsComuns([X1 | L1], [X1 | Common], L2) :-
  54. select(X1, L2, NewL2), !,
  55. elemsComuns(L1, Common, NewL2).
  56. elemsComuns([_ | L1], Common, L2) :-
  57. elemsComuns(L1, Common, L2), !.
  58.  
  59. % P5 printCategory(+)
  60. printCategory(Category) :-
  61. film(Name, List, Duration, Average),
  62. member(Category, List),
  63. write(Name), write(' ('), write(Duration),
  64. write(', '), write(Average), write('/10)'), nl,
  65. fail.
  66. printCategory(_).
  67.  
  68. union(L1, L2, Union) :-
  69. append(L1, L2, Tmp),
  70. sort(Tmp, Union).
  71.  
  72. % P6 similarity(+, +, -)
  73. similarity(Film1, Film2, Similarity) :-
  74. film(Film1, Cat1, Dur1, Score1),
  75. film(Film2, Cat2, Dur2, Score2),
  76.  
  77. elemsComuns(Cat1, CommonCat, Cat2),
  78. length(CommonCat, NumCommCat),
  79. union(Cat1, Cat2, UnionCat),
  80. length(UnionCat, NumUnionCat),
  81. PercentCat is NumCommCat / NumUnionCat,
  82.  
  83. DurDiff is abs(Dur1 - Dur2),
  84.  
  85. ScoreDiff is abs(Score1 - Score2),
  86.  
  87. Similarity is (PercentCat * 100) - 3 * DurDiff - 5 * ScoreDiff.
  88.  
  89. % P7 mostSimilar(+, -, -)
  90. mostSimilar(Film, Similarity, FilmsList) :-
  91. findall(TmpSim-TmpFilm, (similarity(Film, TmpFilm, TmpSim), Film \= TmpFilm), Result),
  92. max_member(Similarity-_, Result),
  93. Similarity > 10, !,
  94. findall(TmpFilm, member(Similarity-TmpFilm, Result), FilmsList).
  95. mostSimilar(_, 0, []).
  96.  
  97. % P8 distancia(+, -, +)
  98. distancia(User1, Distancia, User2) :-
  99. vote(User1, Votes1),
  100. vote(User2, Votes2),
  101. findall(VoteDiff, (member(TmpFilm-V1, Votes1), member(TmpFilm-V2, Votes2), (VoteDiff is abs(V1 - V2))), Result),
  102. listAvg(Result, AvgDiff),
  103.  
  104. ageDiff(User1, User2, AgeDiff),
  105.  
  106. countryDiff(User1, User2, CountryDiff),
  107.  
  108. Distancia is (AvgDiff + AgeDiff / 3 + CountryDiff).
  109.  
  110. ageDiff(User1, User2, AgeDiff) :-
  111. user(User1, Age1, _),
  112. user(User2, Age2, _),
  113. AgeDiff is abs(Age1 - Age2).
  114.  
  115. countryDiff(User1, User2, Diff) :-
  116. user(User1, _, Country),
  117. user(User2, _, Country),
  118. !, Diff = 0.
  119. countryDiff(User1, User2, Diff) :-
  120. user(User1, _, C1),
  121. user(User2, _, C2),
  122. C1 \= C2, !, Diff = 2.
  123.  
  124. listSum([], 0) :- !.
  125. listSum([X | List], NSum) :-
  126. listSum(List, Sum),
  127. NSum is Sum + X.
  128.  
  129. listAvg(List, Avg) :-
  130. listSum(List, Sum),
  131. length(List, Length),
  132. Avg is (Sum / Length).
  133.  
  134.  
  135. % P9 update(+)
  136. update(Film) :-
  137. findall(Vote, (vote(_, Votes), member(Film-Vote, Votes)), Result),
  138. listAvg(Result, NewScore),
  139.  
  140. retract(film(Film, Cats, Duration, _)),
  141. assert(film(Film, Cats, Duration, NewScore)).
  142.  
  143. % P11 move(+, -)
  144. move(Row/Col, Celulas) :-
  145. findall(R/C, (horseMove(Row, Col, R, C), validRowCol(R, C)), Celulas).
  146. validRowCol(Row, Col) :-
  147. Row >= 1, Col >= 1,
  148. Row =< 8, Col =< 8.
  149.  
  150. possibleHorseCoordChange(1).
  151. possibleHorseCoordChange(-1).
  152. possibleHorseCoordChange(2).
  153. possibleHorseCoordChange(-2).
  154.  
  155. horseMove(Row, Col, R, C) :-
  156. possibleHorseCoordChange(Change1),
  157. possibleHorseCoordChange(Change2),
  158. AbsChange1 is abs(Change1),
  159. AbsChange2 is abs(Change2),
  160. AbsChange1 \= AbsChange2,
  161. R is Row + Change1,
  162. C is Col + Change2.
  163.  
  164. % P12 podeMoverEmN(+, +, -)
  165. podeMoverEmN(R/C, 0, [R/C]).
  166. podeMoverEmN(R/C, N, Cells) :-
  167. N > 0,
  168. N1 is N - 1,
  169. move(R/C, TmpCells),
  170. findall(ResCells, (member(TmpCell, TmpCells), podeMoverEmN(TmpCell, N1, ResCells)), OtherCells),
  171. append(OtherCells, TmpCells2),
  172. append(TmpCells, TmpCells2, TmpCells3),
  173. sort(TmpCells3, Cells).
  174.  
  175. % P13 minJogadas(+, +, -)
  176. minJogadas(Row, Col, Row, Col, 0).
  177. minJogadas(InitR, InitC, EndR, EndC, N) :-
  178. minAux(InitR/InitC, EndR/EndC, 0, N).
  179.  
  180. minAux(IR/IC, ER/EC, Counter, N):-
  181. podeMoverEmN(IR/IC, Counter, Cells),
  182. member(ER/EC, Cells), !,
  183. N is Counter.
  184. % P13 by Damas TM - blame him on the performance
Add Comment
Please, Sign In to add comment