Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- :- use_module(library(lists)).
- :- dynamic film/4.
- %film(Title, Categories, Duration, AvgClassification).
- film( 'Doctor Strange',[action, adventure, fantasy], 115, 7.6).
- film( 'Hacksaw Ridge',[biography, drama, romance], 131, 8.7).
- film( 'Inferno',[action, adventure, crime], 121, 6.4).
- film( 'Arrival',[drama, mystery, scifi], 116, 8.5).
- film( 'The Accountant',[action, crime, drama], 127, 7.6).
- film( 'The Girl on the Train',[drama, mystery, thriller] , 112, 6.7).
- %user(Username, YearOfBirth, Country).
- user(john, 1992, 'USA').
- user(jack, 1989, 'UK').
- user(peter, 1983, 'Portugal').
- user(harry, 1993, 'USA').
- user(richard, 1982, 'USA').
- vote(john, ['Inferno'-7, 'Doctor Strange'-9, 'The Accountant'-6]).
- vote(jack, ['Inferno'-8, 'Doctor Strange'-8, 'The Accountant'-7]).
- vote(peter, ['The Accountant'-4, 'Hacksaw Ridge'-7, 'The Girl on the Train'-3]).
- vote(harry, ['Inferno'-7, 'The Accountant'-6]).
- vote(richard, ['Inferno'-10, 'Hacksaw Ridge'-10, 'Arrival'-9]).
- % EX 2
- diff(User1, User2, Difference, Film) :-
- vote(User1, L1),
- vote(User2, L2),
- list(L1, Film, Pontuation1),
- list(L2, Film, Pontuation2),
- Difference is abs(Pontuation1-Pontuation2).
- list([Film-Score|Tail], Title, Pontuation) :-
- (Title == Film -> Pontuation = Score ; list(Tail, Title, Pontuation))
- .
- % EX 3
- niceGuy(User) :-
- vote(User, L),
- listScore(L, 0, P),
- P >= 2.
- listScore([_-Score|Tail], P, PP) :-
- niceScore(Score, P, Prev),
- listScore(Tail, Prev, PP).
- listScore([], P, PP) :-
- PP is P.
- niceScore(Score, P, PP) :-
- Score > 8,
- PP is P + 1.
- niceScore(_,P,PP) :-
- PP is P.
- % EX: 4
- elemsComuns(L1,L,L2) :-
- comunsList(L1,L,[],L2).
- comunsList([], R, R, _).
- comunsList([H1|R1], R, Tmp, L2) :-
- member(H1, L2),
- append(Tmp, [H1], NewTmp),
- comunsList(R1, R, NewTmp, L2).
- comunsList([_|R1], R, Tmp, L2) :-
- comunsList(R1, R, Tmp, L2).
- % EX: 5
- printCategory(Category) :-
- film(Name, Categories, Duration, Score),
- checkCategory(Name, Categories, Duration, Score, Category),
- fail.
- printCategory(_).
- checkCategory(Name, Categories, Duration, Score, Category) :-
- memberchk(Category, Categories),
- printFilm(Name, Duration, Score).
- printFilm(Name, Duration, Score) :-
- write(Name),
- write(' ('),
- write(Duration),
- write('min, '),
- write(Score),
- write('10)'),
- nl.
- % EX: 6
- similarity(Film1, Film2, Similarity) :-
- percDurrScoreCat(Film1, Film2, Percent, Durr, Score),
- Similarity is Percent - 3 * Durr - 5 * Score.
- percDurrScoreCat(Film1, Film2, Percent, Durr, Score) :-
- film(Film1, Categories1, Duration1, Score1),
- film(Film2, Categories2, Duration2, Score2),
- percent(Categories1, Categories2, Percent),
- Durr is abs(Duration1 - Duration2),
- Score is abs(Score1 - Score2)
- .
- percent(Categories1, Categories2, Percent) :-
- elemsComuns(Categories1, CatCommon, Categories2),
- length(Categories1, X),
- length(Categories2, Y),
- length(CatCommon, Z),
- Percent is Z / (X - Z + Y) * 100
- .
- % EX: 7
- mostSimilar(Film, Similarity, Films) :-
- findall(Name, film(Name,_,_,_), Bag),
- delete(Bag, Film, FilmsBag),
- mostSimilarCycle(Film, FilmsBag, 0, Similarity, Films,[])
- .
- mostSimilarCycle(_,[],S,S,F,F).
- mostSimilarCycle(Film, [H|T], Sim_Tmp, Similarity, Films, FilmsTmp) :-
- similarity(Film, H, Sim),!,
- compareSimilarity(Sim, Sim_Tmp, SimNewTmp, H, FilmsTmp, FilmsNewTmp),
- mostSimilarCycle(Film, T, SimNewTmp, Similarity, Films, FilmsNewTmp)
- .
- compareSimilarity(Sim, First_Sim, Similarity, Film, _, [Film]) :-
- Sim > First_Sim,
- Similarity = Sim.
- compareSimilarity(Sim, First_Sim, First_Sim, Film, Films, FilmsTmp) :-
- Sim = First_Sim,
- append(Films, [Film], FilmsTmp).
- compareSimilarity(_,S,S,_,F,F).
- % EX: 8 (sem findall)
- distancia(User1, Distancia, User2) :-
- vote(User1, List1),
- vote(User2, List2),
- avgDiff(List1, List2, AvgDiff, 0, 0),
- ageDiffCountry(User1, User2, AgeDiff, CountryDiff),
- Distancia is AvgDiff + AgeDiff/3 + CountryDiff.
- avgDiff([],_,A,A,_).
- avgDiff([H|T], List2, AvgDiff, AvgDiffTmp, N) :-
- avgDiff2(H, List2, AvgDiffTmp, AvgDiffNewTmp, N, NTmp),
- avgDiff(T, List2, AvgDiff, AvgDiffNewTmp, NTmp).
- avgDiff2(_,[],A,A,N,N).
- avgDiff2(Pair1, [Pair2|T], AvgDiffTmp, AvgDiff, N, NRet) :-
- updateAvg(Pair1, Pair2, AvgDiffTmp, AvgDiffNewTmp, N, NTmp),
- avgDiff2(Pair1, T, AvgDiffNewTmp, AvgDiff, NTmp, NRet).
- updateAvg(Film1-Vote1, Film2-Vote2, AvgDiff, AvgDiffTmp, N, NTmp) :-
- Film1 = Film2,
- NTmp is N + 1,
- AvgDiffTmp is (AvgDiff * N + abs(Vote1-Vote2)) / NTmp.
- updateAvg(_,_,A,A,N,N).
- %END (sem findall)
- % EX: 8 (com findall)
- distanciaFindAll(User1, Distancia, User2) :-
- vote(User1, List1),
- vote(User2, List2),
- findall(VoteDiff, (member(Film-Vote1, List1), member(Film-Vote2, List2), VoteDiff is abs(Vote1 -Vote2)), Result),
- avgList(Result, AvgDiff),
- ageDiffCountry(User1, User2, AgeDiff, CountryDiff),
- Distancia is AvgDiff + AgeDiff/3 + CountryDiff.
- avgList(Result, AvgDiff) :-
- listSum(Result, Sum),
- length(Result, Size),
- AvgDiff is Sum / Size.
- listSum([], 0) :- !.
- listSum([X | List], NSum) :-
- listSum(List, Sum),
- NSum is Sum + X.
- %END (com findall)
- ageDiffCountry(User1, User2, AgeDiff, CountryDiff) :-
- user(User1, Age1, Country1),
- user(User2, Age2, Country2),
- AgeDiff is abs(Age2 - Age1),
- countryDiff(Country1, Country2, CountryDiff).
- countryDiff(Country1, Country2, CountryDiff) :-
- Country1 = Country2,
- CountryDiff is 0.
- countryDiff(_,_,CountryDiff) :-
- CountryDiff is 2.
- %EX9
- update(Film) :-
- findall(Vote, (vote(_,List),member(Film-Vote, List)), AllVotes),
- listSum(AllVotes, Sum),
- length(AllVotes, Size),
- Score is Sum/Size,
- retract(film(Film,Categories,Duration,_)),
- assert(film(Film,Categories,Duration,Score)).
- %EX11
- move(Row/Col, Celulas) :-
- findall(R/C, (horseMove(Row, Col, R, C), validRowCol(R,C)), Celulas).
- validRowCol(R,C) :-
- R =< 8, R > 0,
- C =< 8, C > 0.
- possibleMove(-1).
- possibleMove(1).
- possibleMove(-2).
- possibleMove(2).
- horseMove(Row, Col, R, C) :-
- possibleMove(X1),
- possibleMove(X2),
- X1 \= X2,
- R is Row + X1,
- C is Col + X2.
- %EX: 12
- podeMoverEmN(Row/Col, N, Celulas) :-
- N > 0,
- move(Row/Col, C),
- N1 is N - 1,
- findall(ResCells, (member(TmpCell, C), podeMoveEmN(TmpCell, N1, ResCells)), TotalCells),
- append(TotalCells, C2),
- append(C, C2, C3),
- sort(C3, Celulas).
- %EX: 13
- minJogadas(Initial, Final, N) :-
- minJogadas(Initial, Final, 1, N).
- minJogadas(Initial, Final, Counter, N) :-
- podeMoverEmN(Initial, Counter, Cells),
- (member(Final, Cells) ->
- N is Counter;
- minJogadas(Initial, Final, Counter + 1, N)).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement