Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
64
Task/Best-shuffle/Picat/best-shuffle-1.picat
Normal file
64
Task/Best-shuffle/Picat/best-shuffle-1.picat
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
import cp.
|
||||
|
||||
go =>
|
||||
Words = ["abracadabra",
|
||||
"seesaw",
|
||||
"elk",
|
||||
"grrrrrr",
|
||||
"up",
|
||||
"a",
|
||||
"shuffle",
|
||||
"aaaaaaa"
|
||||
],
|
||||
foreach(Word in Words)
|
||||
best_shuffle(Word,Best,_Score),
|
||||
printf("%s, %s, (%d)\n", Word,Best,diff_word(Word, Best))
|
||||
end,
|
||||
nl.
|
||||
|
||||
best_shuffle(Word,Best,Score) =>
|
||||
WordAlpha = Word.map(ord), % convert to integers
|
||||
WordAlphaNoDups = WordAlpha.remove_dups(),
|
||||
% occurrences of each character in the word
|
||||
Occurrences = occurrences(WordAlpha),
|
||||
|
||||
Len = Word.length,
|
||||
|
||||
% Decision variables
|
||||
WordC = new_list(Len),
|
||||
WordC :: WordAlphaNoDups,
|
||||
|
||||
%
|
||||
% The constraints
|
||||
%
|
||||
|
||||
% Ensure that the shuffled word has the same
|
||||
% occurrences for each character
|
||||
foreach(V in WordAlphaNoDups)
|
||||
count(V, WordC,#=, Occurrences.get(V))
|
||||
end,
|
||||
|
||||
% The score is the number of characters
|
||||
% in the same position as the origin word
|
||||
% (to be minimized).
|
||||
Score #= sum([WordC[I] #= WordAlpha[I] : I in 1..Len]),
|
||||
|
||||
if var(Score) then
|
||||
% We don't have a score yet: minimize Score
|
||||
solve([$min(Score),split], WordC)
|
||||
else
|
||||
% Get a solution for the given Score
|
||||
solve([split], WordC)
|
||||
end,
|
||||
% convert back to alpha
|
||||
Best = WordC.map(chr).
|
||||
|
||||
|
||||
diff_word(W1,W2) = Diff =>
|
||||
Diff = sum([1 : I in 1..W1.length, W1[I]==W2[I]]).
|
||||
|
||||
occurrences(L) = Occ =>
|
||||
Occ = new_map(),
|
||||
foreach(E in L)
|
||||
Occ.put(E, Occ.get(E,0) + 1)
|
||||
end.
|
||||
28
Task/Best-shuffle/Picat/best-shuffle-2.picat
Normal file
28
Task/Best-shuffle/Picat/best-shuffle-2.picat
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
go2 ?=>
|
||||
Words = ["abracadabra",
|
||||
"seesaw",
|
||||
"elk",
|
||||
"grrrrrr",
|
||||
"up",
|
||||
"a",
|
||||
"shuffle",
|
||||
"aaaaaaa"
|
||||
],
|
||||
member(Word,Words),
|
||||
println(word=Word),
|
||||
best_shuffle(Word,_Best,Score),
|
||||
println(best_score=Score),
|
||||
% Find all optimal solutions
|
||||
All = findall(Best2,best_shuffle(Word,Best2,Score)),
|
||||
Len = All.len,
|
||||
println(num_solutions=All.len),
|
||||
if Len <= 10 then
|
||||
println(solutions=All)
|
||||
else
|
||||
println("Only showing the first 10 solutions:"),
|
||||
println(solutions=All[1..10])
|
||||
end,
|
||||
nl,
|
||||
fail,
|
||||
nl.
|
||||
go2 => true.
|
||||
Loading…
Add table
Add a link
Reference in a new issue