Advertisement
steffffffan

P2_15

Nov 20th, 2022
836
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Prolog 0.73 KB | None | 0 0
  1. %15.
  2. % a) Sa se determine cea mai lunga secventa de numere pare consecutive
  3. % dintr-o lista (daca sunt mai multe secvente de lungime maxima, una
  4. % dintre ele).
  5.  
  6. % secvh(A, R, L=lungimea, C, CL, M, ML).
  7. secvh([],M,ML,_,CL,M,ML):- ML>CL,!.
  8. secvh([],C,CL,C,CL,_,ML) :- CL>=ML,!.
  9.  
  10. % gasim un numar impar, updatam M si ML daca CL>ML si resetam C
  11. secvh([I|T],R,L,C,CL,_,ML):- 1 is I mod 2,
  12.     CL>ML, !,
  13.     secvh(T,R,L,[],0,C,CL).
  14.  
  15. secvh([I|T],R,L,_,CL,M,ML):- 1 is I mod 2,
  16.     CL=<ML, !,
  17.     secvh(T,R,L,[],0,M,ML).
  18.  
  19. % gasim un numar impar, il adaugam in C
  20. secvh([P|T],R,L,C,CL,M,ML):-
  21.     0 is P mod 2,
  22.     C1 = [P|C], % se face aici adaugare la sfarsit daca se vrea ordinea din sir
  23.     CL1 is CL+1,
  24.     secvh(T,R,L,C1,CL1,M,ML).
  25.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement