Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
44
Task/Look-and-say-sequence/Prolog/look-and-say-sequence.pro
Normal file
44
Task/Look-and-say-sequence/Prolog/look-and-say-sequence.pro
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
look_and_say(L) :-
|
||||
maplist(write, L), nl,
|
||||
encode(L, L1),
|
||||
look_and_say(L1).
|
||||
|
||||
% This code is almost identical to the code of "run-length-encoding"
|
||||
encode(In, Out) :-
|
||||
packList(In, R1),
|
||||
append(R1,Out).
|
||||
|
||||
|
||||
% use of library clpfd allows packList(?In, ?Out) to works
|
||||
% in both ways In --> Out and In <-- Out.
|
||||
|
||||
:- use_module(library(clpfd)).
|
||||
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
%
|
||||
% ?- packList([a,a,a,b,c,c,c,d,d,e], L).
|
||||
% L = [[3,a],[1,b],[3,c],[2,d],[1,e]] .
|
||||
% ?- packList(R, [[3,a],[1,b],[3,c],[2,d],[1,e]]).
|
||||
% R = [a,a,a,b,c,c,c,d,d,e] .
|
||||
%
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
packList([],[]).
|
||||
|
||||
packList([X],[[1,X]]) :- !.
|
||||
|
||||
|
||||
packList([X|Rest],[XRun|Packed]):-
|
||||
run(X,Rest, XRun,RRest),
|
||||
packList(RRest,Packed).
|
||||
|
||||
|
||||
run(Var,[],[1,Var],[]).
|
||||
|
||||
run(Var,[Var|LRest],[N1, Var],RRest):-
|
||||
N #> 0,
|
||||
N1 #= N + 1,
|
||||
run(Var,LRest,[N, Var],RRest).
|
||||
|
||||
|
||||
run(Var,[Other|RRest], [1,Var],[Other|RRest]):-
|
||||
dif(Var,Other).
|
||||
Loading…
Add table
Add a link
Reference in a new issue