Advertisement
Guest User

Teste_Modelo.pl

a guest
Nov 20th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Prolog 3.84 KB | None | 0 0
  1. :- use_module(library(lists)).
  2.  
  3. %participant(Id,Age,Performance)
  4. participant(1234, 17, 'Pé coxinho').
  5. participant(3423, 21, 'Programar com os pés').
  6. participant(3788, 20, 'Sing a Bit').
  7. participant(4865, 22, 'Pontes de esparguete').
  8. participant(8937, 19, 'Pontes de pen-drives').
  9. participant(2564, 20, 'Moodle hack').
  10.  
  11. %performance(Id,Times)
  12. performance(1234,[120,120,120,120]).
  13. performance(3423,[32,120,45,120]).
  14. performance(3788,[110,2,6,43]).
  15. performance(4865,[120,120,110,120]).
  16. performance(8937,[97,101,105,110]).
  17.  
  18. %1 5min
  19. madeItThrough(Participant) :-
  20.     participant(Participant,_,_),
  21.     performance(Participant, Performance),
  22.     member(120, Performance).
  23.  
  24. %2 15min
  25. juriTimes([],_,[],0).
  26. juriTimes([H|Tail], Juri, Times, Total) :-
  27.     juriTimes(Tail, Juri, TimesN, TotalN),
  28.     getTimePerfomance(H,Juri, T),
  29.     append([T], TimesN, Times),
  30.     Total is TotalN + T.
  31.  
  32. getTimePerfomance(Participant, Juri, T) :-
  33.     performance(Participant, List),
  34.     nth1(Juri, List, T).
  35.  
  36. %3 25min,
  37. patientJuri(Juri) :-
  38.     getJuri(Juri, [], [], V),
  39.     validateGoogJuri(V, Counter),
  40.     Counter >= 2.
  41.  
  42. getJuri(Juri, ListParticipant, ListVote, V) :-
  43.     performance(ID,JurisTimes),
  44.     \+member(ID, ListParticipant),
  45.     append(ListParticipant, [ID], ListPN),
  46.     nth1(Juri, JurisTimes, Time),
  47.     append(ListVote, [Time], ListNV),
  48.     getJuri(Juri, ListPN, ListNV, V).
  49. getJuri(_,_, V, V).
  50.  
  51. validateGoogJuri([], 0).
  52. validateGoogJuri([H|T], Counter) :-
  53.     validateGoogJuri(T, CounterN),
  54.     checkPatient(H, CounterN, Counter).
  55.  
  56. checkPatient(H, CounterN, Counter) :-
  57.     H == 120,
  58.     Counter is CounterN + 1.
  59. checkPatient(_, C, C).
  60.  
  61. %4 7min
  62. listSum([],0).
  63. listSum([H|T], Sum) :-
  64.     listSum(T, SumN),
  65.     Sum is H + SumN.
  66.  
  67. bestParticipant(P1, P2, Z) :-
  68.     performance(P1, Votes1),
  69.     performance(P2, Votes2),
  70.     listSum(Votes1, Sum1),
  71.     listSum(Votes2, Sum2),
  72.     bestP(P1, Sum1, P2, Sum2, Z).
  73.  
  74. bestP(P1, Sum1, _, Sum2, Z) :-
  75.     Sum1 > Sum2,
  76.     Z is P1.
  77. bestP(_,Sum1,P2,Sum2,Z) :-
  78.     Sum2 > Sum1,
  79.     Z is P2.
  80. bestP(_,_,_,_,_) :- fail.
  81.  
  82. %5 7min
  83. allPerfs :-
  84.     performance(ID, Times),
  85.     participant(ID, _, Title),
  86.     writeInfo(ID, Title, Times),
  87.     fail.
  88. allPerfs.
  89. writeInfo(ID, Title, Times) :-
  90.     write(ID),
  91.     write(':'),
  92.     write(Title),
  93.     write(':'),
  94.     write(Times),
  95.     nl.
  96.  
  97. %6 13min
  98. nSuccessfulParticipants(T) :-
  99.     getAllVotes([], [], Votes),
  100.     countSuccessfulP(Votes, T).
  101.  
  102. getAllVotes(P, V,Votes) :-
  103.     performance(ID, Vt),
  104.     \+member(ID, P),
  105.     getAllVotes([ID|P], [Vt|V], Votes).
  106. getAllVotes(_, V, V).
  107.  
  108. countSuccessfulP([], 0).
  109. countSuccessfulP([H|Tail], T) :-
  110.     countSuccessfulP(Tail, NT),
  111.     listSum(H, Sum),
  112.     length(H, Size),
  113.     checkSuccessfulP(Sum, Size, Check),
  114.     T is Check + NT.
  115.  
  116. checkSuccessfulP(Sum, Size, Check) :-
  117.     Total is Size * 120,
  118.     Sum == Total,
  119.     Check is 1.
  120. checkSuccessfulP(_,_,0).
  121.    
  122. %7 10min
  123. juriFans(L) :-
  124.     findall(ID-Vote,
  125.         (performance(ID, Votes),
  126.         findall(N, nth1(N, Votes, 120), Vote)),
  127.     L).
  128. %8
  129. eligibleOutcome(Id,Perf,TT) :-
  130.     performance(Id,Times),
  131.     madeItThrough(Id),
  132.     participant(Id,_,Perf),
  133.     sumlist(Times,TT).
  134.    
  135. nextPhase(N, P) :-
  136.     findall(Bag,setof(TT-Id-Perf,
  137.     (
  138.         eligibleOutcome(Id, Perf, TT),
  139.         performance(Id, Votes),
  140.         member(120, Votes)
  141.     ), [Bag]), R),
  142.     reverse(R, NR),
  143.     getNFirst(N, NR, P).
  144.  
  145. getNFirst(0, _, []).
  146. getNFirst(N, [H|T], [H|P]) :-
  147.     NewN is N - 1,
  148.     getNFirst(NewN,T,P).
  149.  
  150. %11
  151. impoe(X,L) :-
  152.     length(Mid,X),
  153.     append(L1,[X|_],L), append(_,[X|Mid],L1).
  154.  
  155. langford(0,[]).
  156. langford(N,L) :-
  157.     TotalLength is N * 2,
  158.     length(LL, TotalLength),
  159.     langfordCycle(N,L,LL).
  160.  
  161. langfordCycle(0,L, L).
  162. langfordCycle(N,L, LL) :-
  163.     impoe(N, LL),
  164.     NewN is N - 1,
  165.     langfordCycle(NewN, L, LL).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement