Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
74
Task/Best-shuffle/Prolog/best-shuffle-1.pro
Normal file
74
Task/Best-shuffle/Prolog/best-shuffle-1.pro
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
:- dynamic score/2.
|
||||
|
||||
best_shuffle :-
|
||||
maplist(best_shuffle, ["abracadabra", "eesaw", "elk", "grrrrrr",
|
||||
"up", "a"]).
|
||||
|
||||
best_shuffle(Str) :-
|
||||
retractall(score(_,_)),
|
||||
length(Str, Len),
|
||||
assert(score(Str, Len)),
|
||||
calcule_min(Str, Len, Min),
|
||||
repeat,
|
||||
shuffle(Str, Shuffled),
|
||||
maplist(comp, Str, Shuffled, Result),
|
||||
sumlist(Result, V),
|
||||
retract(score(Cur, VCur)),
|
||||
( V < VCur -> assert(score(Shuffled, V)); assert(score(Cur, VCur))),
|
||||
V = Min,
|
||||
retract(score(Cur, VCur)),
|
||||
writef('%s : %s (%d)\n', [Str, Cur, VCur]).
|
||||
|
||||
comp(C, C1, S):-
|
||||
( C = C1 -> S = 1; S = 0).
|
||||
|
||||
% this code was written by P.Caboche and can be found here :
|
||||
% http://pcaboche.developpez.com/article/prolog/listes/?page=page_3#Lshuffle
|
||||
shuffle(List, Shuffled) :-
|
||||
length(List, Len),
|
||||
shuffle(Len, List, Shuffled).
|
||||
|
||||
shuffle(0, [], []) :- !.
|
||||
|
||||
shuffle(Len, List, [Elem|Tail]) :-
|
||||
RandInd is random(Len),
|
||||
nth0(RandInd, List, Elem),
|
||||
select(Elem, List, Rest),
|
||||
NewLen is Len - 1,
|
||||
shuffle(NewLen, Rest, Tail).
|
||||
|
||||
|
||||
% letters are sorted out then packed
|
||||
% If a letter is more numerous than the rest
|
||||
% the min is the difference between the quantity of this letter and
|
||||
% the sum of the quantity of the other letters
|
||||
calcule_min(Str, Len, Min) :-
|
||||
msort(Str, SS),
|
||||
packList(SS, Lst),
|
||||
sort(Lst, Lst1),
|
||||
last(Lst1, [N, _]),
|
||||
( N * 2 > Len -> Min is 2 * N - Len; Min = 0).
|
||||
|
||||
|
||||
|
||||
% almost the same code as in "run_length" page
|
||||
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):-
|
||||
run(Var,LRest,[N, Var],RRest),
|
||||
N > 0,
|
||||
N1 is N + 1.
|
||||
|
||||
|
||||
run(Var,[Other|RRest], [1,Var],[Other|RRest]):-
|
||||
dif(Var,Other).
|
||||
84
Task/Best-shuffle/Prolog/best-shuffle-2.pro
Normal file
84
Task/Best-shuffle/Prolog/best-shuffle-2.pro
Normal file
|
|
@ -0,0 +1,84 @@
|
|||
:- system:set_prolog_flag(double_quotes,codes) .
|
||||
|
||||
play(STRINGs)
|
||||
:-
|
||||
shuffle(STRINGs,SHUFFLEDs) ,
|
||||
score(STRINGs,SHUFFLEDs,SCORE) ,
|
||||
system:format('~s , ~s , (~10r)~n',[STRINGs,SHUFFLEDs,SCORE])
|
||||
.
|
||||
|
||||
test
|
||||
:-
|
||||
play("abracadabra") ,
|
||||
play("seesaw") ,
|
||||
play("elk") ,
|
||||
play("grrrrrr") ,
|
||||
play("up") ,
|
||||
play("a")
|
||||
.
|
||||
|
||||
%! shuffle(Xs0,Ys) .
|
||||
%
|
||||
% The list `Ys` is an random permutation of the list `Xs0` .
|
||||
% No assumption is made about the nature of each item in the list .
|
||||
%
|
||||
% The default seed for randomness provided by the system is truly random .
|
||||
% Set the seed explicitly with `system:set_random(seed(SEED))` .
|
||||
|
||||
:- op(1,'xfy','shuffle_') .
|
||||
|
||||
shuffle(Xs0,Ys)
|
||||
:-
|
||||
(assign_randomness) shuffle_ (Xs0,Ys0) ,
|
||||
(sort) shuffle_ (Ys0,Ys1) ,
|
||||
(remove_randomness) shuffle_ (Ys1,Ys)
|
||||
.
|
||||
|
||||
/*
|
||||
1. assign an random number to each of the items in the list .
|
||||
2. sort the list of items according to the random number assigned to each item .
|
||||
3. remove the random number from each of the items in the list .
|
||||
*/
|
||||
|
||||
(assign_randomness) shuffle_ ([],[]) :- ! .
|
||||
|
||||
(assign_randomness) shuffle_ ([X0|Xs0],[sortable(R,X0)|Rs])
|
||||
:-
|
||||
system:random(R) ,
|
||||
(assign_randomness) shuffle_ (Xs0,Rs)
|
||||
.
|
||||
|
||||
(sort) shuffle_ (Rs0,Ss)
|
||||
:-
|
||||
prolog:sort(Rs0,Ss)
|
||||
.
|
||||
|
||||
(remove_randomness) shuffle_ ([],[]) :- ! .
|
||||
|
||||
(remove_randomness) shuffle_ ([sortable(_R0,X0)|Ss0],[X0|Xs])
|
||||
:-
|
||||
(remove_randomness) shuffle_ (Ss0,Xs)
|
||||
.
|
||||
|
||||
|
||||
%! score(Xs0,Ys0,SCORE) .
|
||||
%
|
||||
% `SCORE` is the count of positions in Ys0 that
|
||||
% have the identical content as
|
||||
% the content in the same position in Xs0 .
|
||||
|
||||
score([],[],0) :- ! .
|
||||
|
||||
score([X0|Xs0],[Y0|Ys0],SCORE)
|
||||
:-
|
||||
X0 = Y0 ,
|
||||
! ,
|
||||
score(Xs0,Ys0,SCORE0) ,
|
||||
SCORE is SCORE0 + 1
|
||||
.
|
||||
|
||||
score([_|Xs0],[_|Ys0],SCORE)
|
||||
:-
|
||||
! ,
|
||||
score(Xs0,Ys0,SCORE)
|
||||
.
|
||||
Loading…
Add table
Add a link
Reference in a new issue