Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- :- use_module(library(lists)).
- :- dynamic film/4.
- % Factos
- film('A', [action, adventure, fantasy], 115, 7.6).
- film('B', [biography, drama, romance], 131, 8.7).
- film('C', [action, adventure, crime], 121, 6.4).
- film('D', [drama, mystery, scifi], 116, 8.5).
- film('E', [action, crime, drama], 127, 7.6).
- film('F', [drama, mystery, thriller], 112, 6.7).
- user(john, 1992, 'USA').
- user(jack, 1989, 'UK').
- user(peter, 1983, 'portugal').
- user(harry, 1993, 'USA').
- user(richard, 1982, 'USA').
- vote(john, ['C'-7, 'A'-9, 'E'-6]).
- vote(jack, ['C'-8, 'A'-8, 'E'-7]).
- vote(peter, ['E'-4, 'B'-7, 'F'-3]).
- vote(harry, ['C'-7, 'E'-6]).
- vote(richard, ['C'-10, 'B'-10, 'D'-9]).
- %% PERGUNTAS %%
- % P1 curto(+)
- curto(Nome) :-
- film(Nome, _, Duracao, _),
- Duracao < 125.
- % P2 diff(+, +, -, +)
- diff(User1, User2, Difference, Film) :-
- vote(User1, Votes1),
- vote(User2, Votes2),
- member(Film-Vote1, Votes1),
- member(Film-Vote2, Votes2),
- Difference is abs(Vote1 - Vote2).
- % P3 niceGuy(+)
- niceGuy(User) :-
- vote(User, Votes),
- exclude(lessThan8, Votes, Result),
- length(Result, Len),
- Len >= 2.
- lessThan8(_Key-Value) :-
- Value < 8.
- % P4 elemsComuns(+, -, +)
- elemsComuns([], [], _) :- !.
- elemsComuns(_, [], []) :- !.
- elemsComuns([X1 | L1], [X1 | Common], L2) :-
- select(X1, L2, NewL2), !,
- elemsComuns(L1, Common, NewL2).
- elemsComuns([_ | L1], Common, L2) :-
- elemsComuns(L1, Common, L2), !.
- % P5 printCategory(+)
- printCategory(Category) :-
- film(Name, List, Duration, Average),
- member(Category, List),
- write(Name), write(' ('), write(Duration),
- write(', '), write(Average), write('/10)'), nl,
- fail.
- printCategory(_).
- union(L1, L2, Union) :-
- append(L1, L2, Tmp),
- sort(Tmp, Union).
- % P6 similarity(+, +, -)
- similarity(Film1, Film2, Similarity) :-
- film(Film1, Cat1, Dur1, Score1),
- film(Film2, Cat2, Dur2, Score2),
- elemsComuns(Cat1, CommonCat, Cat2),
- length(CommonCat, NumCommCat),
- union(Cat1, Cat2, UnionCat),
- length(UnionCat, NumUnionCat),
- PercentCat is NumCommCat / NumUnionCat,
- DurDiff is abs(Dur1 - Dur2),
- ScoreDiff is abs(Score1 - Score2),
- Similarity is (PercentCat * 100) - 3 * DurDiff - 5 * ScoreDiff.
- % P7 mostSimilar(+, -, -)
- mostSimilar(Film, Similarity, FilmsList) :-
- findall(TmpSim-TmpFilm, (similarity(Film, TmpFilm, TmpSim), Film \= TmpFilm), Result),
- max_member(Similarity-_, Result),
- Similarity > 10, !,
- findall(TmpFilm, member(Similarity-TmpFilm, Result), FilmsList).
- mostSimilar(_, 0, []).
- % P8 distancia(+, -, +)
- distancia(User1, Distancia, User2) :-
- vote(User1, Votes1),
- vote(User2, Votes2),
- findall(VoteDiff, (member(TmpFilm-V1, Votes1), member(TmpFilm-V2, Votes2), (VoteDiff is abs(V1 - V2))), Result),
- listAvg(Result, AvgDiff),
- ageDiff(User1, User2, AgeDiff),
- countryDiff(User1, User2, CountryDiff),
- Distancia is (AvgDiff + AgeDiff / 3 + CountryDiff).
- ageDiff(User1, User2, AgeDiff) :-
- user(User1, Age1, _),
- user(User2, Age2, _),
- AgeDiff is abs(Age1 - Age2).
- countryDiff(User1, User2, Diff) :-
- user(User1, _, Country),
- user(User2, _, Country),
- !, Diff = 0.
- countryDiff(User1, User2, Diff) :-
- user(User1, _, C1),
- user(User2, _, C2),
- C1 \= C2, !, Diff = 2.
- listSum([], 0) :- !.
- listSum([X | List], NSum) :-
- listSum(List, Sum),
- NSum is Sum + X.
- listAvg(List, Avg) :-
- listSum(List, Sum),
- length(List, Length),
- Avg is (Sum / Length).
- % P9 update(+)
- update(Film) :-
- findall(Vote, (vote(_, Votes), member(Film-Vote, Votes)), Result),
- listAvg(Result, NewScore),
- retract(film(Film, Cats, Duration, _)),
- assert(film(Film, Cats, Duration, NewScore)).
- % P11 move(+, -)
- move(Row/Col, Celulas) :-
- findall(R/C, (horseMove(Row, Col, R, C), validRowCol(R, C)), Celulas).
- validRowCol(Row, Col) :-
- Row >= 1, Col >= 1,
- Row =< 8, Col =< 8.
- possibleHorseCoordChange(1).
- possibleHorseCoordChange(-1).
- possibleHorseCoordChange(2).
- possibleHorseCoordChange(-2).
- horseMove(Row, Col, R, C) :-
- possibleHorseCoordChange(Change1),
- possibleHorseCoordChange(Change2),
- AbsChange1 is abs(Change1),
- AbsChange2 is abs(Change2),
- AbsChange1 \= AbsChange2,
- R is Row + Change1,
- C is Col + Change2.
- % P12 podeMoverEmN(+, +, -)
- podeMoverEmN(R/C, 0, [R/C]).
- podeMoverEmN(R/C, N, Cells) :-
- N > 0,
- N1 is N - 1,
- move(R/C, TmpCells),
- findall(ResCells, (member(TmpCell, TmpCells), podeMoverEmN(TmpCell, N1, ResCells)), OtherCells),
- append(OtherCells, TmpCells2),
- append(TmpCells, TmpCells2, TmpCells3),
- sort(TmpCells3, Cells).
- % P13 minJogadas(+, +, -)
- minJogadas(Row, Col, Row, Col, 0).
- minJogadas(InitR, InitC, EndR, EndC, N) :-
- minAux(InitR/InitC, EndR/EndC, 0, N).
- minAux(IR/IC, ER/EC, Counter, N):-
- podeMoverEmN(IR/IC, Counter, Cells),
- member(ER/EC, Cells), !,
- N is Counter.
- % P13 by Damas TM - blame him on the performance
Add Comment
Please, Sign In to add comment