Find the missing permutation: Difference between revisions
→{{header|Picat}}: Moved into subsections.
Not a robot (talk | contribs) (Add 8086 assembly) |
(→{{header|Picat}}: Moved into subsections.) |
||
Line 2,557:
=={{header|Picat}}==
Here are several approaches, including constraint modelling, sets (ordset), and permutations.
All assume that the variables P1 and/or Perms has been defined:
<lang picat> P1 = ["ABCD","CABD","ACDB","DACB","BCDA","ACBD",
"ADCB","CDAB","DABC","BCAD","CADB","CDBA",
"CBAD","ABDC","ADBC","BDCA","DCBA","BACD",
"BADC","BDAC","CBDA","DBCA","DCAB"],
Perms = permutations("ABCD"),
% ...
</lang>
<lang Picat> % ...
foreach(P in Perms,
Found =
foreach(T in P1)
if P == T
Found :=
end
end,
if not Found then
Missing := P
end
end,
println(missing1=Missing).</lang>
<lang Picat> % ...
foreach(P in Perms, Missing2 = _)
if not member(P,P1) then
end
end,
println(missing2=Missing2).</lang>
===Using findall===
<lang Picat> % ...
println(missing3=difference(Perms,P1)).
difference(Xs,Ys) = findall(X,(member(X,Xs),not(member(X,Ys)))).</lang>
<lang Picat> % ...
println(missing4=findall(X,(member(X,Perms),not(member(X,P1))))).</lang>
The module <code>ordsets</code> must be imported,
<lang Picat>import ordsets.
% ...
println(missing5=subtract(new_ordset(Perms),new_ordset(P1))).</lang>
List comprehension with <code>membchk/1</code> for the check)
<lang Picat> % ...
println(missing6=[P:P in Perms,not membchk(P,P1)])</lang>
<lang Picat> % ...
Map = new_map(),
println(missing7=[P: P in Perms, not Map.has_key(P)]).</lang>
==="Merge sort" variants===
"Merge sort" variants, using sorted lists. <code>zip/2</code> requires that the length of the two lists are the same, hence the "dummy".
<lang Picat> % ...
PermsSorted = Perms.sort(),
P1Sorted = P1.sort(),
Found2 = false,
foreach({P,PP} in zip(PermsSorted,P1Sorted ++ ["DUMMY"]), Found2 = false)
if P != PP then
println(missing8=P),
Found2 := true
end
end,
A = [cond(P == PP,1,0) : {P,PP} in zip(PermsSorted,P1Sorted ++ ["DUMMY"])],
println(missing9=[PermsSorted[I] : I in 1..PermsSorted.length, A[I] = 0].first()),
% shorter
===Constraint modelling===
The <code>cp</code> module must be imported.
<lang Picat>import cp.
% ...
ABCD = new_map(['A'=1,'B'=2,'C'=3,'D'=4]),
% convert to integers (for the table constraint)
P1Table = [ [ABCD.get(C,0) : C in P].to_array() : P in P1],
Line 2,643 ⟶ 2,654:
solve(Missing3),
ABCD2 = "ABCD",
println(missing11=[ABCD2[I] : I in Missing3])
<lang Picat> % ...
PermsLen = Perms.length,
P1Len = P1.length,
Line 2,653 ⟶ 2,664:
A2[I,J] := 1
end,
println(missing12=[Perms[I] : I in 1..PermsLen, sum([A2[I,J] : J in 1..P1Len])=0])
===Xor variant===
{{trans|Raku}}
<lang Picat> % ...
println(missing13=to_fstring("%X",reduce(^,[parse_term("0x"++P):P in P1]))).</lang>
===Count occurrences===
Count the character with the least occurrence (=5) for each positions (1..4). Some variants.
{{trans|K}}
<lang Picat> % ...
println(missing14=[[O:O=5 in Occ]:Occ in [occurrences([P[I]:P in P1]):I in 1..4]]),
% variant using sorting the occurrences
println(missing15a=[C:C=_ in [sort2(Occ).first():Occ in [occurrences([P[I]:P in P1]):I in 1..4]]]),
% transpose instead of array index
println(missing15b=[C:C=_ in [sort2(O).first():T in transpose(P1),O=occurrences(T)]]),
% extract the values with first
println(missing15c=[sort2(O).first():T in transpose(P1),O=occurrences(T)].map(first)),
println(missing15d=[sort2(O).first().first():T in transpose(P1),O=occurrences(T)]),
println(missing15e=[S[1,1]:T in transpose(P1),S=sort2(occurrences(T))]).
% return a map with the elements and the number of occurrences
occurrences(List) = Map =>
Map = new_map(),
foreach(E in List)
Map.put(E, cond(Map.has_key(E),Map.get(E)+1,1))
end,
Perms2 = Perms,
foreach(P in P1) Perms2 := delete(Perms2,P) end,
Line 2,678 ⟶ 2,701:
nl.
% sort a map according to values
Line 2,693 ⟶ 2,706:
</lang>
Running all these snippets:
{{out}}
<pre>
missing1 = DBAC
Line 2,715 ⟶ 2,728:
missing15d = DBAC
missing15e = DBAC
missing16 = [DBAC]</pre>
=={{header|PicoLisp}}==
|