Last active
November 20, 2017 22:16
-
-
Save AndreFCruz/96b67924b93ec80fa7ab064cd88c722f to your computer and use it in GitHub Desktop.
Soluções para o teste modelo de PLOG, disponível no Moodle. 2017/2018
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| :- use_module(library(lists)). | |
| %% Teste Modelo 17/18 | |
| %participant(Id,Age,Performance) | |
| participant(1234, 17, 'Pé coxinho'). | |
| participant(3423, 21, 'Programar com os pés'). | |
| participant(3788, 20, 'Sing a Bit'). | |
| participant(4865, 22, 'Pontes de esparguete'). | |
| participant(8937, 19, 'Pontes de pen-drives'). | |
| participant(2564, 20, 'Moodle hack'). | |
| %performance(Id,Times) | |
| performance(1234,[120,120,120,120]). | |
| performance(3423,[32,120,45,120]). | |
| performance(3788,[110,2,6,43]). | |
| performance(4865,[120,120,110,120]). | |
| performance(8937,[97,101,105,110]). | |
| % P1 madeItThrough(+Id) | |
| madeItThrough(Id) :- | |
| performance(Id, Times), | |
| member(120, Times). | |
| % P2 juriTimes(+Participants, +JuriMember, -Times, -Total) | |
| juriTimes([], _, [], 0). | |
| juriTimes([First | OtherParticipants], JuriMember, [FirstTime | OtherTimes], Total) :- | |
| performance(First, Times), | |
| nth1(JuriMember, Times, FirstTime), | |
| juriTimes(OtherParticipants, JuriMember, OtherTimes, NTotal), | |
| Total is NTotal + FirstTime. | |
| % P3 patientJuri(+JuriMember) | |
| patientJuri(JuriMember) :- | |
| performance(Id1, Times1), | |
| nth1(JuriMember, Times1, 120), | |
| performance(Id2, Times2), | |
| Id1 \= Id2, | |
| nth1(JuriMember, Times2, 120). | |
| % P4 bestParticipant(+P1, +P2, -P) | |
| bestParticipant(P1, P2, P) :- | |
| performance(P1, Times1), | |
| performance(P2, Times2), | |
| sum_list(Times1, Sum1), | |
| sum_list(Times2, Sum2), | |
| Diff is Sum1 - Sum2, | |
| bestParticipantAux(P1, P2, Diff, P). | |
| bestParticipantAux(P1, _P2, Diff, P1) :- | |
| Diff > 0. | |
| bestParticipantAux(_P1, P2, Diff, P2) :- | |
| Diff < 0. | |
| sum_list([], 0). | |
| sum_list([First | Rest], Sum) :- | |
| sum_list(Rest, RestSum), | |
| Sum is RestSum + First. | |
| % P5 allPerfs | |
| allPerfs :- | |
| performance(Id, Times), | |
| participant(Id, _, Name), | |
| write(Id), write(':'), | |
| write(Name), write(':'), | |
| write(Times), nl, | |
| fail. | |
| allPerfs. | |
| % P6 nSuccessfulParticipants(-N) | |
| nSuccessfulParticipants(N) :- | |
| findall(Id, (performance(Id, Times), successfulTimes(Times)), Result), | |
| length(Result, N). | |
| successfulTimes(Times) :- | |
| member(Elem, Times), | |
| Elem < 120, !, fail. | |
| successfulTimes(_). | |
| % P7 juriFans(-juriFansList) | |
| juriFans(List) :- | |
| findall(Id-Fans, (performance(Id, Times), juriFansAux(Times, Fans, 1)), List). | |
| juriFansAux([], [], _). | |
| juriFansAux([120 | RestT], [Count | OtherFans], Count) :- | |
| !, NCount is Count + 1, | |
| juriFansAux(RestT, OtherFans, NCount). | |
| juriFansAux([_ | RestT], OtherFans, Count) :- | |
| NCount is Count + 1, | |
| juriFansAux(RestT, OtherFans, NCount). | |
| % P8 nextPhase(+N, -Participants) | |
| nextPhase(N, Participants) :- | |
| setof(Score-Id-Name, eligibleOutcome(Id, Name, Score), AllParticipants), | |
| selectBestN(N, AllParticipants, Participants). | |
| eligibleOutcome(Id,Perf,TT) :- | |
| performance(Id,Times), | |
| madeItThrough(Id), | |
| participant(Id,_,Perf), | |
| sumlist(Times,TT). | |
| selectBestN(0, _, []). | |
| selectBestN(N, L, [LastElem | Rest]) :- | |
| N1 is N - 1, | |
| append(L1, [LastElem], L), | |
| selectBestN(N1, L1, Rest). | |
| % P9 predX(+Idade, +L, -Ps) | |
| predX(Q,[R|Rs],[P|Ps]) :- | |
| participant(R,I,P), I=<Q, !, | |
| predX(Q,Rs,Ps). | |
| predX(Q,[R|Rs],Ps) :- | |
| participant(R,I,_), I>Q, | |
| predX(Q,Rs,Ps). | |
| predX(_,[],[]). | |
| % O predicado predX/3 recebe um inteiro, Idade, e uma lista, L, de Ids de | |
| % participantes, e unifica o terceiro argumento com as performances cujo | |
| % participante tem menos de Idade anos. | |
| % O cut utilizado é verde, pois não influencia as soluções obtidas. | |
| % P10 | |
| impoe(X,L) :- | |
| length(Mid,X), | |
| append(L1,[X|_],L), append(_,[X|Mid],L1). | |
| % O predicado impoe/2 avalia se é cumprida parte da condição enunciada, nomeadamente: | |
| % que L tem uma sub-lista de tamanho X cujo primeiro e último elemento é X. | |
| % P11 langford(+N, -L) | |
| langford(N, L) :- | |
| N > 0, | |
| N2 is N * 2, | |
| length(L, N2), | |
| langfordAux(N, L). | |
| langfordAux(0, _). | |
| langfordAux(N, L) :- | |
| impoe(N, L), | |
| N1 is N - 1, | |
| langfordAux(N1, L). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment