Guest User

Untitled

a guest
Nov 20th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.67 KB | None | 0 0
  1. :- use_module(library(lists)).
  2.  
  3. %% Teste Modelo 17/18
  4.  
  5. %participant(Id,Age,Performance)
  6. participant(1234, 17, 'Pé coxinho').
  7. participant(3423, 21, 'Programar com os pés').
  8. participant(3788, 20, 'Sing a Bit').
  9. participant(4865, 22, 'Pontes de esparguete').
  10. participant(8937, 19, 'Pontes de pen-drives').
  11. participant(2564, 20, 'Moodle hack').
  12.  
  13. %performance(Id,Times)
  14. performance(1234,[120,120,120,120]).
  15. performance(3423,[32,120,45,120]).
  16. performance(3788,[110,2,6,43]).
  17. performance(4865,[120,120,110,120]).
  18. performance(8937,[97,101,105,110]).
  19.  
  20.  
  21. % P1 madeItThrough(+Id)
  22. madeItThrough(Id) :-
  23. performance(Id, Times),
  24. member(120, Times).
  25.  
  26. % P2 juriTimes(+Participants, +JuriMember, -Times, -Total)
  27. juriTimes([], _, [], 0).
  28. juriTimes([First | OtherParticipants], JuriMember, [FirstTime | OtherTimes], Total) :-
  29. performance(First, Times),
  30. nth1(JuriMember, Times, FirstTime),
  31. juriTimes(OtherParticipants, JuriMember, OtherTimes, NTotal),
  32. Total is NTotal + FirstTime.
  33.  
  34. % P3 patientJuri(+JuriMember)
  35. patientJuri(JuriMember) :-
  36. performance(Id1, Times1),
  37. nth1(JuriMember, Times1, 120),
  38. performance(Id2, Times2),
  39. Id1 \= Id2,
  40. nth1(JuriMember, Times2, 120).
  41.  
  42. % P4 bestParticipant(+P1, +P2, -P)
  43. bestParticipant(P1, P2, P) :-
  44. performance(P1, Times1),
  45. performance(P2, Times2),
  46. sum_list(Times1, Sum1),
  47. sum_list(Times2, Sum2),
  48. Diff is Sum1 - Sum2,
  49. bestParticipantAux(P1, P2, Diff, P).
  50. bestParticipantAux(P1, _P2, Diff, P1) :-
  51. Diff > 0.
  52. bestParticipantAux(_P1, P2, Diff, P2) :-
  53. Diff < 0.
  54.  
  55. sum_list([], 0).
  56. sum_list([First | Rest], Sum) :-
  57. sum_list(Rest, RestSum),
  58. Sum is RestSum + First.
  59.  
  60. % P5 allPerfs
  61. allPerfs :-
  62. performance(Id, Times),
  63. participant(Id, _, Name),
  64. write(Id), write(':'),
  65. write(Name), write(':'),
  66. write(Times), nl,
  67. fail.
  68. allPerfs.
  69.  
  70. % P6 nSuccessfulParticipants(-N)
  71. nSuccessfulParticipants(N) :-
  72. findall(Id, (performance(Id, Times), successfulTimes(Times)), Result),
  73. length(Result, N).
  74.  
  75. successfulTimes(Times) :-
  76. member(Elem, Times),
  77. Elem < 120, !, fail.
  78. successfulTimes(_).
  79.  
  80. % P7 juriFans(-juriFansList)
  81. juriFans(List) :-
  82. findall(Id-Fans, (performance(Id, Times), juriFansAux(Times, Fans, 1)), List).
  83.  
  84. juriFansAux([], [], _).
  85. juriFansAux([120 | RestT], [Count | OtherFans], Count) :-
  86. !, NCount is Count + 1,
  87. juriFansAux(RestT, OtherFans, NCount).
  88. juriFansAux([_ | RestT], OtherFans, Count) :-
  89. NCount is Count + 1,
  90. juriFansAux(RestT, OtherFans, NCount).
  91.  
  92. % P8 nextPhase(+N, -Participants)
  93. nextPhase(N, Participants) :-
  94. setof(Score-Id-Name, eligibleOutcome(Id, Name, Score), AllParticipants),
  95. selectBestN(N, AllParticipants, Participants).
  96.  
  97. eligibleOutcome(Id,Perf,TT) :-
  98. performance(Id,Times),
  99. madeItThrough(Id),
  100. participant(Id,_,Perf),
  101. sumlist(Times,TT).
  102.  
  103. selectBestN(0, _, []).
  104. selectBestN(N, L, [LastElem | Rest]) :-
  105. N1 is N - 1,
  106. append(L1, [LastElem], L),
  107. selectBestN(N1, L1, Rest).
  108.  
  109. % P9 predX(+Idade, +L, -Ps)
  110. predX(Q,[R|Rs],[P|Ps]) :-
  111. participant(R,I,P), I=<Q, !,
  112. predX(Q,Rs,Ps).
  113. predX(Q,[R|Rs],Ps) :-
  114. participant(R,I,_), I>Q,
  115. predX(Q,Rs,Ps).
  116. predX(_,[],[]).
  117. % O predicado predX/3 recebe um inteiro, Idade, e uma lista, L, de Ids de
  118. % participantes, e unifica o terceiro argumento com as performances cujo
  119. % participante tem menos de Idade anos.
  120.  
  121.  
  122. % P10
  123. impoe(X,L) :-
  124. length(Mid,X),
  125. append(L1,[X|_],L), append(_,[X|Mid],L1).
  126. % O predicado impoe/2 avalia se é cumprida parte da condição enunciada, nomeadamente:
  127. % que L tem uma sub-lista de tamanho X cujo primeiro e último elemento é X.
  128.  
  129.  
  130. % P11 langford(+N, -L)
  131. langford(N, L) :-
  132. N > 0, N2 is N * 2,
  133. length(UninitializedList, N2),
  134. langfordAux(N, UninitializedList, L).
  135.  
  136.  
  137. langfordAux(0, L, L).
  138. langfordAux(N, UL, L) :-
  139. impoe(N, UL), N1 is N - 1,
  140. langfordAux(N1, UL, L).
Add Comment
Please, Sign In to add comment