Advertisement
atrem21215

lab5

Nov 3rd, 2021
1,220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Prolog 3.12 KB | None | 0 0
  1. print_odd:-writeln('print A and B(segment)'),read(A),read(B),odd(A,B).
  2. odd(A,B):-A=<B,H2 is B mod 2,H2==1,format('~w ',[B]),B1 is B-2,odd(A,B1);
  3.           A=<B,H2 is B mod 2,H2==0,B1 is B-1,odd(A,B1).
  4.          
  5. fib:-repeat,read(Num),my_check(Num),Num<0,!.
  6. my_check(N):-ConstMinus1 is -1,N\==ConstMinus1,f(N,Res),Res\==0,format('fibonaci ~w is ~w',[N,Res]),nl;
  7.              N<0,!.
  8. f(N,Res):- N==0,Res is 1;
  9.            N==1,Res is 1;
  10.            N>1,Num_pred is N-1,Num_pred2 is N-2,f(Num_pred,Res_pred),f(Num_pred2,Res_pred2), Res is Res_pred+Res_pred2;
  11.            N<0,Res is 0.
  12.            
  13. split_list:-write('input list: '),read(L),write('input left num: '),read(Left),write('input right num'),read(Right),L1=[],L2=[],L3=[],cur_elem(L,Left,Right,L1,L2,L3).
  14. cur_elem([Head|Tail],Left,Right,L1,L2,L3):-Tail\==[],X=Head,X<Left,append(L1,[X],LL1),cur_elem(Tail,Left,Right,LL1,L2,L3);
  15.                                            Tail\==[],X=Head,X>=Left,X=<Right,append(L2,[X],LL2),cur_elem(Tail,Left,Right,L1,LL2,L3);
  16.                                            Tail\==[],X=Head,X>Right,append(L3,[X],LL3),cur_elem(Tail,Left,Right,L1,L2,LL3);
  17.                                            Tail==[],format('~w ~w ~w',[L1,L2,L3]),!.
  18.                                            
  19.  
  20.  
  21.  
  22. most_frequent:-write('input list: '),read(L),in_sort(L,Sorted_L),calc_first(Sorted_L,Pred),Len is 0,
  23.                Cur_Maxx is 0,max_seq(Sorted_L,Cur_Maxx,Pred,Len,Maxx),build_rez(Sorted_L,Maxx,Cur_Maxx,Pred,[],Rez_list),format('~w',[Rez_list]).
  24.  
  25. calc_first([X|Tail],Pred):-Pred is X.
  26.  
  27. in_sort([],[]).
  28. in_sort([X|Tail],Sort_list):-in_sort(Tail,Sort_Tail),add(X,Sort_Tail,Sort_list).
  29. add(X,[Y|Sort_list],[Y|Sort_list1]):-X@>Y,!,add(X,Sort_list,Sort_list1).
  30. add(X,Sort_list,[X|Sort_list]).
  31.  
  32. max_seq([X|Tail_Sorted_L],Cur_Maxx,Pred,Len,Maxx):-Tail_Sorted_L==[],X==Pred,New_Len is Len+1,New_Len>Cur_Maxx,Maxx is Len+1;
  33.                                           Tail_Sorted_L==[],Maxx is Cur_Maxx;
  34.                                           X==Pred,New_len is Len+1,max_seq(Tail_Sorted_L,Cur_Maxx,X,New_len,Maxx);
  35.                                           X\==Pred,Len>Cur_Maxx,New_Maxx is Len,New_len is 1,max_seq(Tail_Sorted_L,New_Maxx,X,New_len,Maxx);
  36.                                           X\==Pred,Len=<Cur_Maxx,New_len is 1,max_seq(Tail_Sorted_L,Cur_Maxx,X,New_len,Maxx).
  37.  
  38. build_rez([X|Tail_Sorted_L],Maxx,Cur_Len,Pred,Cur_list,Rez_list):-Tail_Sorted_L==[],X==Pred,New_Len is Cur_Len+1,New_Len==Maxx,append(Cur_list,[Pred],New_list),
  39.                                                        Rez_list=New_list;
  40.                                           Tail_Sorted_L==[],Rez_list=Cur_list;
  41.                                           X==Pred,New_len is Cur_Len+1,build_rez(Tail_Sorted_L,Maxx,New_len,X,Cur_list,Rez_list);
  42.                                           X\==Pred,Cur_Len==Maxx,append(Cur_list,[Pred],New_list),New_len is 1,
  43.                                                        build_rez(Tail_Sorted_L,Maxx,New_len,X,New_list,Rez_list);
  44.                                           X\==Pred,Cur_Len\==Maxx,New_len is 1,build_rez(Tail_Sorted_L,Maxx,New_len,X,Cur_list,Rez_list).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement