Skip to content

Instantly share code, notes, and snippets.

@Alejandro131
Created November 20, 2015 16:39
Show Gist options
  • Save Alejandro131/5c36719d6a493286c66e to your computer and use it in GitHub Desktop.
Save Alejandro131/5c36719d6a493286c66e to your computer and use it in GitHub Desktop.
%append(list1, list2, result)
append([],B,B).
append([H|A],B,[H|T]):-append(A,B,T).
%member(List, Element)
member([X|T],X).
member([Y|T],X):-member(T,X).
%between(start, end, number)
between(A,B,A):- A <= B.
between(A,B,X):-A1 is A+1, A1 <= B, between(A1,B,X).
%perm(List, Permutations)
perm([],[]).
perm(L,[G|T]):-append(A,[G|Y],L),append(A,Y,W),perm(W,T).
%reverse(list, reversed)
%r(list, reversed, stack) very important order of [H|S]
r([], S, S).
r([H|T], R, S):-r(T,R,[H|S]).
reverse(L,R):-r(L,R,[]).
%length(List,Length)
length([],0).
length([H|T],N):-length(T,M),N is M+1.
%nthMember(List,N,Member)
nthMember([X|_],0,X).
nthMember([X|T],N,Y):-M is N-1, nthMember(T,M,Y).
nthMember2([X|_],0,X).
nthMember2([X|T],N,Y):-nthMember2(T,M,Y), N is M+1.
%remove(List,Member,Result)
remove([X|T],X,T).
remove([H|T],X,R):-remove(T,X,RT),append([H],RT,R).
%min(List,MinimumMember)
%min2(X,Y,ResultMin)
min2(X,Y,Y):-Y<X.
min2(X,Y,X):-not(Y<X).
min([X],X).
min([H|T],X):-min(T,R),min2(R,H,X).
%ssort(List,SortedList)
ssort([],[]).
ssort(L,[X|R]):-min(L,X),remove(L,X,L2),ssort(L2,R).
%prefix(prefixList, List)
prefix(P, L):-append(P, _, L).
%suffix(suffixList, List)
suffix(S, L):-append(_, S, L).
%substr2(subStr, List)
substr2(S,L):-prefix(P,L),suffix(S,P).
%sublist(subList, List)
%sublist([], []).
%sublist([H|T], L):-append(A, [H|B], L),sublist(T,B).
%allsublists(List,sublist).
allsublists([],[]).
allsublists([H|T],[H|S]):-allsublists(T,S).
allsublists([_|T],S):-allsublists(T,S).
e(0).
e(N):-e(M),N is M+1.
%tuples(0,0).
tuples(X,Y):-e(S),between(0,S,X),Y is S-X.
divisible(X,Y):-C is X mod Y, C =:= 0.
filter([],[]).
filter([H|T],[H|T1]):-divisible(H,2),filter(T,T1).
filter([H|T],T1):-not(divisible(H,2)),filter(T,T1).
%---------------------------------------------------
dekP([H],[G]):-append(A,[G|T],H).
dekP([H|T1],[G|T2]):-append(A,[G|T],H),dekP(T1,T2).
%dekP([[1,2,3,4],[5,6,7,8],[9,10],[11]],X).
divides(X,A):- A mod X =:= 0.
isPrime(X):-not((X1 is X-1, between(2,X1,G), divides(G,X))).
primeDivisorsXtoY(X,Y):-not((between(2,X,G), divides(G,X), isPrime(G), not(divides(G,Y)))).
primeDivisors(X,Y):-primeDivisorsXtoY(X,Y),primeDivisorsXtoY(Y,X).
%2012
%allsublists([1,2,3],X).
within([X0,Y0,A00], [X1,Y1,A11]):- A0 is A00 / 2, A1 is A11 / 2, X0 - A0 >= X1 - A1, X0 + A0 =< X1 + A1, Y0 - A0 >= Y1 - A1, Y0 + A0 =< Y1 + A1.
%concList([]).
concList([X]).
concList([A,B|T]):-within(A,B),concList([B|T]).
p1(X,Y):-allsublists(X,L),perm(L,Y),concList(Y).
%p1([[1,1,2],[1,1,1],[0,0,3],[2,2,1]],Y).
%--------------------------------------------------------------
p2find(X, L) :- findall(Y, p1(X, Y), L).
max3(A,B,A):-A>B.
max3(A,B,B):-not(A>B).
maxListLength([X],N):-length(X,N).
maxListLength([H|T],N):-length(H,M),maxListLength(T,F),max3(M,F,N).
p2(X,Y):-p2find(X,L),maxListLength(L,N),p1(X,Y),length(Y,B),B =:= N.
%maxListLength([[1,1,2],[1,1,1],[0,0,3],[2,2,1]],Y).
%---------------------------------------------------------------
%rt(0, []).
%rt(1, [[],[[],[]]]).
%rt(N, T):-e(S), between(0, S, X), Y is S-X, rt(X, T1), rt(Y, T2), append([T1], [T2], R), append([T1], [R], T).
%rt(0, []).
%rt(N, T):-between(0, N, X), Y is N-X, rt(X, T1), rt(Y, T2), append([T1], [T2], R), append([T1], [R], T).
%rtf(T):-e(S), rt(S, T).
%rt(_, _, []).
%rt(A, B, T):-append([A], [B], R), append([A], [R], T).
%sumTupleC(0, 1).
%sumTupleC(N, S):- N1 is N-1, N1 >= 0, sumTupleC(N1, S1), S2 is N+1, S is S1+S2.
%tuplesC(0,0,0).
%tuplesC(X,Y,N):-e(S), S >= 1, S1 is S-1, sumTupleC(S1, S2), S2 =< N, between(0,S,X), Y is S-X, S3 is S2 + Y,N =:= S3.
%tuplesC(X,Y,1).
%-----------------------------------------------------------------
tupl(X,Y,N,CN):-S1 is CN+1, N2 is N - S1, N2 > 0, CN2 is CN + 1, tupl(X,Y,N2,CN2).
tupl(X,Y,N,CN):-S1 is CN+1, N2 is N - S1, N2 =< 0, S is CN, Y is N-1, X is S-Y.
tup(X,Y,N):-N > 0, tupl(X,Y,N,1).
tup(0,0,0).
rrt(0,[]).
rrt(N,T):-N2 is N-1,tup(X,Y,N2),rrt(X,A),rrt(Y,B),append([A], [B], R), append([A], [R], T).
rt(T):-e(S), rrt(S,T).
%----------------------------------------------------------------
coh([L1,L2]):-member(L1,X),member(L2,X).
coh([L1,L2|T]):-member(L1,X),member(L2,X),append([L2],T,TN),coh(TN).
%coh([[1,2,3],[2,3,4],[5,4],[5]]).
ads(T):-append(A,[L1,L2|L],T),not((member(L1,X),not(member(L2,X)))).
union2([],U,U).
union2([H|T],L2,U):-member(L2,H),union2(T,L2,U2),append([],U2,U).
union2([H|T],L2,U):-not(member(L2,H)),union2(T,L2,U2),append([H],U2,U).
union([U|[]],U).
union([A,B|[]],U):-union2(A,B,U).
union([H|T],U):-union(T,U2),union2(U2,H,U).
zad11([],_,[]).
zad11([[A,B]|T],L2, M):- member(L2,[C,D]),C=:=B,zad11(T,L2,M1),append(M1, [[A,D]], M).
zad11([[A,B]|T],L2, M):- not((member(L2,[C,D]),C=:=B)),zad11(T,L2,M1),append(M1, [], M).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment