Last letter-first letter: Difference between revisions

Content deleted Content added
Petelomax (talk | contribs)
m →‎{{header|Phix}}: added syntax colouring, marked p2js compatible
Hakank (talk | contribs)
Line 3,363:
with word[1] when I got bored and killed it, quickly calculating at the very least another 4.5 days and
quite probably not finishing within my lifetime...
 
=={{header|Picat}}==
It's a little faster with the words sorted on decreasing lengths (words2): 9.43s vs 11.742s.
<lang Picat>go =>
words(Words),
% Words that starts with same letter as <this words>'s last letter.
Starts = new_map(),
foreach(Word in Words)
S = [ Word2 : Word2 in Words, Word != Word2, Word.last() = Word2.first()],
Starts.put(Word,S)
end,
% Sort the words according to lengths in decreasing order.
Words2 = [W : W=_ in Starts.map_to_list().qsort(sort_len)],
 
% Start the search
MaxLen = _,
Continue := true,
foreach(Len in 2..Words.len, break(Continue == false))
if play1(Words2,Starts,Len,_List) then
MaxLen := Len
else
Continue := false
end
end,
println(maxLen=MaxLen),
% And present the result.
println("\nGet some (5) solutions:"),
get_some_solutions(Words2, Starts, MaxLen,5),
 
println("\nNnumber of optimal solutions:"),
NumSols = count_all(play1(Words2,Starts,MaxLen,_List)),
println(num_sols=NumSols),
nl.
 
 
% Check if it's possible to create a list of length Len.
play1(Words, Starts, Len, LLFL) :-
LLFL1 = new_list(Len),
select(LLFL1[1], Words, Rest),
C = 2,
while (C <= Len)
Among = Starts.get(LLFL1[C-1]),
Among != [],
select(Word,Among,Rest2),
not membchk(Word,LLFL1[1..C-1]),
LLFL1[C] := Word,
Rest := Rest2,
C := C + 1
end,
LLFL = LLFL1.
 
 
% Print NumSols solutions
get_some_solutions(Words,Starts,FoundLen,NumSols) =>
Map = get_global_map(),
Map.put(count,1),
play1(Words, Starts, FoundLen, LLFL),
println(LLFL),
C = Map.get(count),
if C < NumSols then Map.put(count,C+1), fail end.
 
 
% qsort(List, SortFunction)
% returns a sorted list according to the sort function SortFunction.
qsort([],_F) = [].
qsort([H|T],F) = qsort([E : E in T, call(F,E,H)], F)
++ [H] ++
qsort([E : E in T, not call(F,E,H)],F).
 
% Sort according to length
sort_len((_K1=V1),(_K2=V2)) :-
V1.len >= V2.len.
 
words(Words) =>
Words =
[
"audino", "bagon", "baltoy", "banette", "bidoof", "braviary", "bronzor", "carracosta", "charmeleon",
"cresselia", "croagunk", "darmanitan", "deino", "emboar", "emolga", "exeggcute", "gabite",
"girafarig", "gulpin", "haxorus", "heatmor", "heatran", "ivysaur", "jellicent", "jumpluff", "kangaskhan",
"kricketune", "landorus", "ledyba", "loudred", "lumineon", "lunatone", "machamp", "magnezone", "mamoswine",
"nosepass", "petilil", "pidgeotto", "pikachu", "pinsir", "poliwrath", "poochyena", "porygon2",
"porygonz", "registeel", "relicanth", "remoraid", "rufflet", "sableye", "scolipede", "scrafty", "seaking",
"sealeo", "silcoon", "simisear", "snivy", "snorlax", "spoink", "starly", "tirtouga", "trapinch", "treecko",
"tyrogue", "vigoroth", "vulpix", "wailord", "wartortle", "whismur", "wingull", "yamask"
].</lang>
 
 
Output:
<pre>maxLen = 23
 
Get some (5) solutions:
[machamp,petilil,landorus,scrafty,yamask,kricketune,emboar,registeel,loudred,darmanitan,nosepass,simisear,relicanth,heatmor,rufflet,trapinch,haxorus,seaking,girafarig,gabite,exeggcute,emolga,audino]
[machamp,petilil,landorus,scrafty,yamask,kricketune,emboar,registeel,loudred,darmanitan,nosepass,simisear,rufflet,trapinch,heatmor,relicanth,haxorus,seaking,girafarig,gabite,exeggcute,emolga,audino]
[machamp,petilil,landorus,scrafty,yamask,kricketune,emboar,relicanth,haxorus,simisear,rufflet,trapinch,heatmor,registeel,loudred,darmanitan,nosepass,seaking,girafarig,gabite,exeggcute,emolga,audino]
[machamp,petilil,landorus,scrafty,yamask,kricketune,emboar,relicanth,heatmor,registeel,loudred,darmanitan,nosepass,simisear,rufflet,trapinch,haxorus,seaking,girafarig,gabite,exeggcute,emolga,audino]
[machamp,petilil,landorus,scrafty,yamask,kricketune,emboar,relicanth,heatmor,rufflet,trapinch,haxorus,simisear,registeel,loudred,darmanitan,nosepass,seaking,girafarig,gabite,exeggcute,emolga,audino]
 
Nnumber of optimal solutions:
num_sols = 1248</pre>
 
=={{header|PicoLisp}}==