Find the missing permutation: Difference between revisions

Content added Content deleted
(→‎{{header|Picat}}: Moved into subsections.)
m (syntax highlighting fixup automation)
Line 57: Line 57:
{{trans|C}}
{{trans|C}}


<lang 11l>V perms = [‘ABCD’, ‘CABD’, ‘ACDB’, ‘DACB’, ‘BCDA’, ‘ACBD’, ‘ADCB’, ‘CDAB’,
<syntaxhighlight lang="11l">V perms = [‘ABCD’, ‘CABD’, ‘ACDB’, ‘DACB’, ‘BCDA’, ‘ACBD’, ‘ADCB’, ‘CDAB’,
‘DABC’, ‘BCAD’, ‘CADB’, ‘CDBA’, ‘CBAD’, ‘ABDC’, ‘ADBC’, ‘BDCA’,
‘DABC’, ‘BCAD’, ‘CADB’, ‘CDBA’, ‘CBAD’, ‘ABDC’, ‘ADBC’, ‘BDCA’,
‘DCBA’, ‘BACD’, ‘BADC’, ‘BDAC’, ‘CBDA’, ‘DBCA’, ‘DCAB’]
‘DCBA’, ‘BACD’, ‘BADC’, ‘BDAC’, ‘CBDA’, ‘DBCA’, ‘DCAB’]
Line 71: Line 71:
L.break
L.break


print(missing)</lang>
print(missing)</syntaxhighlight>


{{out}}
{{out}}
Line 81: Line 81:
{{trans|BBC BASIC}}
{{trans|BBC BASIC}}
Very compact version, thanks to the clever [[#Raku|Raku]] "xor" algorithm.
Very compact version, thanks to the clever [[#Raku|Raku]] "xor" algorithm.
<lang 360asm>* Find the missing permutation - 19/10/2015
<syntaxhighlight lang="360asm">* Find the missing permutation - 19/10/2015
PERMMISX CSECT
PERMMISX CSECT
USING PERMMISX,R15 set base register
USING PERMMISX,R15 set base register
Line 109: Line 109:
MISS DC 4XL1'00',C' is missing' buffer
MISS DC 4XL1'00',C' is missing' buffer
YREGS
YREGS
END PERMMISX</lang>
END PERMMISX</syntaxhighlight>
{{out}}
{{out}}
<pre>DBAC is missing</pre>
<pre>DBAC is missing</pre>


=={{header|8080 Assembly}}==
=={{header|8080 Assembly}}==
<lang asm>PRMLEN: equ 4 ; length of permutation string
<syntaxhighlight lang="asm">PRMLEN: equ 4 ; length of permutation string
puts: equ 9 ; CP/M print string
puts: equ 9 ; CP/M print string
org 100h
org 100h
Line 138: Line 138:
db 'DABC','BCAD','CADB','CDBA','CBAD','ABDC','ADBC','BDCA'
db 'DABC','BCAD','CADB','CDBA','CBAD','ABDC','ADBC','BDCA'
db 'DCBA','BACD','BADC','BDAC','CBDA','DBCA','DCAB'
db 'DCBA','BACD','BADC','BDAC','CBDA','DBCA','DCAB'
db 0 ; end marker </lang>
db 0 ; end marker </syntaxhighlight>
{{out}}
{{out}}
<pre>Missing permutation: DBAC</pre>
<pre>Missing permutation: DBAC</pre>


=={{header|8086 Assembly}}==
=={{header|8086 Assembly}}==
<lang asm> cpu 8086
<syntaxhighlight lang="asm"> cpu 8086
org 100h
org 100h
mov si,perms ; Start of permutations
mov si,perms ; Start of permutations
Line 164: Line 164:
perms: db 'ABCD','CABD','ACDB','DACB','BCDA','ACBD','ADCB','CDAB'
perms: db 'ABCD','CABD','ACDB','DACB','BCDA','ACBD','ADCB','CDAB'
db 'DABC','BCAD','CADB','CDBA','CBAD','ABDC','ADBC','BDCA'
db 'DABC','BCAD','CADB','CDBA','CBAD','ABDC','ADBC','BDCA'
db 'DCBA','BACD','BADC','BDAC','CBDA','DBCA','DCAB'</lang>
db 'DCBA','BACD','BADC','BDAC','CBDA','DBCA','DCAB'</syntaxhighlight>
{{out}}
{{out}}
<pre>Missing permutation: DBAC</pre>
<pre>Missing permutation: DBAC</pre>


=={{header|Action!}}==
=={{header|Action!}}==
<lang Action!>PROC Main()
<syntaxhighlight lang="action!">PROC Main()
DEFINE PTR="CARD"
DEFINE PTR="CARD"
DEFINE COUNT="23"
DEFINE COUNT="23"
Line 199: Line 199:


Print(missing)
Print(missing)
RETURN</lang>
RETURN</syntaxhighlight>
{{out}}
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Find_the_missing_permutation.png Screenshot from Atari 8-bit computer]
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Find_the_missing_permutation.png Screenshot from Atari 8-bit computer]
Line 208: Line 208:
=={{header|Ada}}==
=={{header|Ada}}==


<lang Ada>with Ada.Text_IO;
<syntaxhighlight lang="ada">with Ada.Text_IO;
procedure Missing_Permutations is
procedure Missing_Permutations is
subtype Permutation_Character is Character range 'A' .. 'D';
subtype Permutation_Character is Character range 'A' .. 'D';
Line 258: Line 258:
Ada.Text_IO.Put_Line ("Missing Permutation:");
Ada.Text_IO.Put_Line ("Missing Permutation:");
Put (Missing_Permutation);
Put (Missing_Permutation);
end Missing_Permutations;</lang>
end Missing_Permutations;</syntaxhighlight>


=={{header|Aime}}==
=={{header|Aime}}==
<lang aime>void
<syntaxhighlight lang="aime">void
paste(record r, index x, text p, integer a)
paste(record r, index x, text p, integer a)
{
{
Line 294: Line 294:


return 0;
return 0;
}</lang>
}</syntaxhighlight>
{{Out}}
{{Out}}
<pre>DBAC</pre>
<pre>DBAC</pre>
Line 300: Line 300:
=={{header|ALGOL 68}}==
=={{header|ALGOL 68}}==
Uses the XOR algorithm of the Raku sample.
Uses the XOR algorithm of the Raku sample.
<lang algol68>BEGIN # find the missing permutation in a list using the XOR method of the Raku sample #
<syntaxhighlight lang="algol68">BEGIN # find the missing permutation in a list using the XOR method of the Raku sample #
# the list to find the missing permutation of #
# the list to find the missing permutation of #
[]STRING list = ( "ABCD", "CABD", "ACDB", "DACB", "BCDA"
[]STRING list = ( "ABCD", "CABD", "ACDB", "DACB", "BCDA"
Line 321: Line 321:
print( ( REPR ABS m ) )
print( ( REPR ABS m ) )
OD
OD
END</lang>
END</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 333: Line 333:
the letter that occurs least.
the letter that occurs least.


<lang APL>missing ← ((⊂↓⍳¨⌊/) +⌿∘(⊢∘.=∪∘∊)) ⌷ ∪∘∊</lang>
<syntaxhighlight lang="apl">missing ← ((⊂↓⍳¨⌊/) +⌿∘(⊢∘.=∪∘∊)) ⌷ ∪∘∊</syntaxhighlight>


{{out}}
{{out}}


<lang APL> perms←↑'ABCD' 'CABD' 'ACDB' 'DACB' 'BCDA' 'ACBD' 'ADCB' 'CDAB'
<syntaxhighlight lang="apl"> perms←↑'ABCD' 'CABD' 'ACDB' 'DACB' 'BCDA' 'ACBD' 'ADCB' 'CDAB'
perms⍪←↑'DABC' 'BCAD' 'CADB' 'CDBA' 'CBAD' 'ABDC' 'ADBC' 'BDCA'
perms⍪←↑'DABC' 'BCAD' 'CADB' 'CDBA' 'CBAD' 'ABDC' 'ADBC' 'BDCA'
perms⍪←↑'DCBA' 'BACD' 'BADC' 'BDAC' 'CBDA' 'DBCA' 'DCAB'
perms⍪←↑'DCBA' 'BACD' 'BADC' 'BDAC' 'CBDA' 'DBCA' 'DCAB'
missing perms
missing perms
DBAC</lang>
DBAC</syntaxhighlight>


=={{header|AppleScript}}==
=={{header|AppleScript}}==
Line 349: Line 349:


Yosemite OS X onwards (uses NSString for sorting):
Yosemite OS X onwards (uses NSString for sorting):
<lang AppleScript>use framework "Foundation" -- ( sort )
<syntaxhighlight lang="applescript">use framework "Foundation" -- ( sort )


--------------- RAREST LETTER IN EACH COLUMN -------------
--------------- RAREST LETTER IN EACH COLUMN -------------
Line 607: Line 607:
on |words|(s)
on |words|(s)
words of s
words of s
end |words|</lang>
end |words|</syntaxhighlight>
{{Out}}
{{Out}}
<pre>"DBAC"</pre>
<pre>"DBAC"</pre>
Line 613: Line 613:
=={{header|Arturo}}==
=={{header|Arturo}}==


<lang rebol>perms: [
<syntaxhighlight lang="rebol">perms: [
"ABCD" "CABD" "ACDB" "DACB" "BCDA" "ACBD" "ADCB" "CDAB" "DABC"
"ABCD" "CABD" "ACDB" "DACB" "BCDA" "ACBD" "ADCB" "CDAB" "DABC"
"BCAD" "CADB" "CDBA" "CBAD" "ABDC" "ADBC" "BDCA" "DCBA" "BACD"
"BCAD" "CADB" "CDBA" "CBAD" "ABDC" "ADBC" "BDCA" "DCBA" "BACD"
Line 621: Line 621:
allPerms: map permutate split "ABCD" => join
allPerms: map permutate split "ABCD" => join


print first difference allPerms perms</lang>
print first difference allPerms perms</syntaxhighlight>


{{out}}
{{out}}
Line 628: Line 628:


=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==
<lang AutoHotkey>IncompleteList := "ABCD CABD ACDB DACB BCDA ACBD ADCB CDAB DABC BCAD CADB CDBA CBAD ABDC ADBC BDCA DCBA BACD BADC BDAC CBDA DBCA DCAB"
<syntaxhighlight lang="autohotkey">IncompleteList := "ABCD CABD ACDB DACB BCDA ACBD ADCB CDAB DABC BCAD CADB CDBA CBAD ABDC ADBC BDCA DCBA BACD BADC BDAC CBDA DBCA DCAB"


CompleteList := Perm( "ABCD" )
CompleteList := Perm( "ABCD" )
Line 657: Line 657:
}
}
return substr(L, 1, -1)
return substr(L, 1, -1)
}</lang>
}</syntaxhighlight>


=={{header|AWK}}==
=={{header|AWK}}==
Line 663: Line 663:
This reads the list of permutations as standard input and outputs the missing one.
This reads the list of permutations as standard input and outputs the missing one.


<lang awk>{
<syntaxhighlight lang="awk">{
split($1,a,"");
split($1,a,"");
for (i=1;i<=4;++i) {
for (i=1;i<=4;++i) {
Line 681: Line 681:
}
}
print s[1]s[2]s[3]s[4]
print s[1]s[2]s[3]s[4]
}</lang>
}</syntaxhighlight>


{{Out}}
{{Out}}
Line 688: Line 688:
=={{header|BBC BASIC}}==
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
{{works with|BBC BASIC for Windows}}
<lang bbcbasic> DIM perms$(22), miss&(4)
<syntaxhighlight lang="bbcbasic"> DIM perms$(22), miss&(4)
perms$() = "ABCD", "CABD", "ACDB", "DACB", "BCDA", "ACBD", "ADCB", \
perms$() = "ABCD", "CABD", "ACDB", "DACB", "BCDA", "ACBD", "ADCB", \
\ "CDAB", "DABC", "BCAD", "CADB", "CDBA", "CBAD", "ABDC", "ADBC", \
\ "CDAB", "DABC", "BCAD", "CADB", "CDBA", "CBAD", "ABDC", "ADBC", \
Line 699: Line 699:
NEXT
NEXT
PRINT $$^miss&(0) " is missing"
PRINT $$^miss&(0) " is missing"
END</lang>
END</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 707: Line 707:
=={{header|Burlesque}}==
=={{header|Burlesque}}==


<lang burlesque>
<syntaxhighlight lang="burlesque">
ln"ABCD"r@\/\\
ln"ABCD"r@\/\\
</syntaxhighlight>
</lang>


(Feed permutations via STDIN. Uses the naive method).
(Feed permutations via STDIN. Uses the naive method).
Line 716: Line 716:
the letters with the lowest frequency:
the letters with the lowest frequency:


<lang burlesque>
<syntaxhighlight lang="burlesque">
ln)XXtp)><)F:)<]u[/v\[
ln)XXtp)><)F:)<]u[/v\[
</syntaxhighlight>
</lang>


=={{header|C}}==
=={{header|C}}==
<lang C>#include <stdio.h>
<syntaxhighlight lang="c">#include <stdio.h>


#define N 4
#define N 4
Line 753: Line 753:
return 0;
return 0;
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>Missing: DBAC</pre>
<pre>Missing: DBAC</pre>
Line 760: Line 760:
===By permutating===
===By permutating===
{{works with|C sharp|C#|2+}}
{{works with|C sharp|C#|2+}}
<lang csharp>using System;
<syntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.Collections.Generic;


Line 800: Line 800:
}
}
}
}
}</lang>
}</syntaxhighlight>
===By xor-ing the values===
===By xor-ing the values===
{{works with|C sharp|C#|3+}}
{{works with|C sharp|C#|3+}}
<lang csharp>using System;
<syntaxhighlight lang="csharp">using System;
using System.Linq;
using System.Linq;


Line 821: Line 821:
Console.WriteLine(string.Join("", values.Select(i => (char)i)));
Console.WriteLine(string.Join("", values.Select(i => (char)i)));
}
}
}</lang>
}</syntaxhighlight>


=={{header|C++}}==
=={{header|C++}}==
<lang Cpp>#include <algorithm>
<syntaxhighlight lang="cpp">#include <algorithm>
#include <vector>
#include <vector>
#include <set>
#include <set>
Line 862: Line 862:
std::copy(missing.begin(), missing.end(), std::ostream_iterator<std::string>(std::cout, "\n"));
std::copy(missing.begin(), missing.end(), std::ostream_iterator<std::string>(std::cout, "\n"));
return 0;
return 0;
}</lang>
}</syntaxhighlight>


=={{header|Clojure}}==
=={{header|Clojure}}==
<lang clojure>
<syntaxhighlight lang="clojure">
(use 'clojure.math.combinatorics)
(use 'clojure.math.combinatorics)
(use 'clojure.set)
(use 'clojure.set)
Line 872: Line 872:
(def s1 (apply hash-set (permutations "ABCD")))
(def s1 (apply hash-set (permutations "ABCD")))
(def missing (difference s1 given))
(def missing (difference s1 given))
</syntaxhighlight>
</lang>
Here's a version based on the hint in the description. ''freqs'' is a sequence of letter frequency maps, one for each column. There should be 6 of each letter in each column, so we look for the one with 5.
Here's a version based on the hint in the description. ''freqs'' is a sequence of letter frequency maps, one for each column. There should be 6 of each letter in each column, so we look for the one with 5.
<lang clojure>(def abcds ["ABCD" "CABD" "ACDB" "DACB" "BCDA" "ACBD" "ADCB" "CDAB"
<syntaxhighlight lang="clojure">(def abcds ["ABCD" "CABD" "ACDB" "DACB" "BCDA" "ACBD" "ADCB" "CDAB"
"DABC" "BCAD" "CADB" "CDBA" "CBAD" "ABDC" "ADBC" "BDCA"
"DABC" "BCAD" "CADB" "CDBA" "CBAD" "ABDC" "ADBC" "BDCA"
"DCBA" "BACD" "BADC" "BDAC" "CBDA" "DBCA" "DCAB"])
"DCBA" "BACD" "BADC" "BDAC" "CBDA" "DBCA" "DCAB"])
Line 882: Line 882:
(defn v->k [fqmap v] (->> fqmap (filter #(-> % second (= v))) ffirst))
(defn v->k [fqmap v] (->> fqmap (filter #(-> % second (= v))) ffirst))


(->> freqs (map #(v->k % 5)) (apply str) println)</lang>
(->> freqs (map #(v->k % 5)) (apply str) println)</syntaxhighlight>


=={{header|CoffeeScript}}==
=={{header|CoffeeScript}}==


<lang coffeescript>
<syntaxhighlight lang="coffeescript">
missing_permutation = (arr) ->
missing_permutation = (arr) ->
# Find the missing permutation in an array of N! - 1 permutations.
# Find the missing permutation in an array of N! - 1 permutations.
Line 922: Line 922:
console.log missing_permutation(arr)
console.log missing_permutation(arr)
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 931: Line 931:


=={{header|Common Lisp}}==
=={{header|Common Lisp}}==
<lang lisp>(defparameter *permutations*
<syntaxhighlight lang="lisp">(defparameter *permutations*
'("ABCD" "CABD" "ACDB" "DACB" "BCDA" "ACBD" "ADCB" "CDAB" "DABC" "BCAD" "CADB" "CDBA"
'("ABCD" "CABD" "ACDB" "DACB" "BCDA" "ACBD" "ADCB" "CDAB" "DABC" "BCAD" "CADB" "CDBA"
"CBAD" "ABDC" "ADBC" "BDCA" "DCBA" "BACD" "BADC" "BDAC" "CBDA" "DBCA" "DCAB"))
"CBAD" "ABDC" "ADBC" "BDCA" "DCBA" "BACD" "BADC" "BDAC" "CBDA" "DBCA" "DCAB"))
Line 944: Line 944:
(cons (count letter occurs) letter))
(cons (count letter occurs) letter))
letters))))))
letters))))))
(concatenate 'string (mapcar #'least-occurs (enum (length letters)))))))</lang>
(concatenate 'string (mapcar #'least-occurs (enum (length letters)))))))</syntaxhighlight>
{{out}}
{{out}}
<pre>ROSETTA> (missing-perm *permutations*)
<pre>ROSETTA> (missing-perm *permutations*)
Line 950: Line 950:


=={{header|D}}==
=={{header|D}}==
<lang d>void main() {
<syntaxhighlight lang="d">void main() {
import std.stdio, std.string, std.algorithm, std.range, std.conv;
import std.stdio, std.string, std.algorithm, std.range, std.conv;


Line 992: Line 992:
perms[0][maxCode - code].write;
perms[0][maxCode - code].write;
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>DBAC
<pre>DBAC
Line 1,002: Line 1,002:


=={{header|EchoLisp}}==
=={{header|EchoLisp}}==
<lang lisp>
<syntaxhighlight lang="lisp">
;; use the obvious methos
;; use the obvious methos
(lib 'list) ; for (permutations) function
(lib 'list) ; for (permutations) function
Line 1,017: Line 1,017:
(set-substract (make-set all-perms) (make-set perms))
(set-substract (make-set all-perms) (make-set perms))
→ { DBAC }
→ { DBAC }
</syntaxhighlight>
</lang>


=={{header|Elixir}}==
=={{header|Elixir}}==
<lang elixir>defmodule RC do
<syntaxhighlight lang="elixir">defmodule RC do
def find_miss_perm(head, perms) do
def find_miss_perm(head, perms) do
all_permutations(head) -- perms
all_permutations(head) -- perms
Line 1,037: Line 1,037:
"CBAD", "ABDC", "ADBC", "BDCA", "DCBA", "BACD", "BADC", "BDAC", "CBDA", "DBCA", "DCAB"]
"CBAD", "ABDC", "ADBC", "BDCA", "DCBA", "BACD", "BADC", "BDAC", "CBDA", "DBCA", "DCAB"]


IO.inspect RC.find_miss_perm( hd(perms), perms )</lang>
IO.inspect RC.find_miss_perm( hd(perms), perms )</syntaxhighlight>


{{out}}
{{out}}
Line 1,046: Line 1,046:
=={{header|Erlang}}==
=={{header|Erlang}}==
The obvious method. It seems fast enough (no waiting time).
The obvious method. It seems fast enough (no waiting time).
<syntaxhighlight lang="erlang">
<lang Erlang>
-module( find_missing_permutation ).
-module( find_missing_permutation ).


Line 1,063: Line 1,063:
is_different( [_H] ) -> true;
is_different( [_H] ) -> true;
is_different( [H | T] ) -> not lists:member(H, T) andalso is_different( T ).
is_different( [H | T] ) -> not lists:member(H, T) andalso is_different( T ).
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 1,071: Line 1,071:


=={{header|ERRE}}==
=={{header|ERRE}}==
<syntaxhighlight lang="erre">
<lang ERRE>
PROGRAM MISSING
PROGRAM MISSING


Line 1,105: Line 1,105:
PRINT("Solution is: ";SOL$)
PRINT("Solution is: ";SOL$)
END PROGRAM
END PROGRAM
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 1,113: Line 1,113:
=={{header|Factor}}==
=={{header|Factor}}==
Permutations are read in via STDIN.
Permutations are read in via STDIN.
<lang factor>USING: io math.combinatorics sequences sets ;
<syntaxhighlight lang="factor">USING: io math.combinatorics sequences sets ;


"ABCD" all-permutations lines diff first print</lang>
"ABCD" all-permutations lines diff first print</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,126: Line 1,126:
'''Method:''' Read the permutations in as hexadecimal numbers, exclusive ORing them together gives the answer.
'''Method:''' Read the permutations in as hexadecimal numbers, exclusive ORing them together gives the answer.
(This solution assumes that none of the permutations is defined as a Forth word.)
(This solution assumes that none of the permutations is defined as a Forth word.)
<lang forth> hex
<syntaxhighlight lang="forth"> hex
ABCD CABD xor ACDB xor DACB xor BCDA xor ACBD xor
ABCD CABD xor ACDB xor DACB xor BCDA xor ACBD xor
ADCB xor CDAB xor DABC xor BCAD xor CADB xor CDBA xor
ADCB xor CDAB xor DABC xor BCAD xor CADB xor CDBA xor
Line 1,132: Line 1,132:
BADC xor BDAC xor CBDA xor DBCA xor DCAB xor
BADC xor BDAC xor CBDA xor DBCA xor DCAB xor
cr .( Missing permutation: ) u.
cr .( Missing permutation: ) u.
decimal</lang>
decimal</syntaxhighlight>
{{out}}
{{out}}
<pre>Missing permutation: DBAC ok</pre>
<pre>Missing permutation: DBAC ok</pre>
Line 1,138: Line 1,138:
=={{header|Fortran}}==
=={{header|Fortran}}==
'''Work-around''' to let it run properly with some bugged versions (e.g. 4.3.2) of gfortran: remove the ''parameter'' attribute to the array list.
'''Work-around''' to let it run properly with some bugged versions (e.g. 4.3.2) of gfortran: remove the ''parameter'' attribute to the array list.
<lang fortran>program missing_permutation
<syntaxhighlight lang="fortran">program missing_permutation


implicit none
implicit none
Line 1,153: Line 1,153:
write (*, *)
write (*, *)


end program missing_permutation</lang>
end program missing_permutation</syntaxhighlight>
{{out}}
{{out}}
<pre>DBAC</pre>
<pre>DBAC</pre>
Line 1,159: Line 1,159:
=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
===Simple count===
===Simple count===
<lang freebasic>' version 30-03-2017
<syntaxhighlight lang="freebasic">' version 30-03-2017
' compile with: fbc -s console
' compile with: fbc -s console


Line 1,195: Line 1,195:
Print : Print "hit any key to end program"
Print : Print "hit any key to end program"
Sleep
Sleep
End</lang>
End</syntaxhighlight>
{{out}}
{{out}}
<pre>The missing permutation is : DBAC</pre>
<pre>The missing permutation is : DBAC</pre>
===Add the value's===
===Add the value's===
<lang freebasic>' version 30-03-2017
<syntaxhighlight lang="freebasic">' version 30-03-2017
' compile with: fbc -s console
' compile with: fbc -s console


Line 1,232: Line 1,232:
Print : Print "hit any key to end program"
Print : Print "hit any key to end program"
Sleep
Sleep
End</lang>
End</syntaxhighlight>
<pre>output is same as the first version</pre>
<pre>output is same as the first version</pre>
===Using Xor===
===Using Xor===
<lang freebasic>' version 30-03-2017
<syntaxhighlight lang="freebasic">' version 30-03-2017
' compile with: fbc -s console
' compile with: fbc -s console


Line 1,261: Line 1,261:
Print : Print "hit any key to end program"
Print : Print "hit any key to end program"
Sleep
Sleep
End</lang>
End</syntaxhighlight>
<pre>Output is the same as the first version</pre>
<pre>Output is the same as the first version</pre>


=={{header|GAP}}==
=={{header|GAP}}==
<lang gap># our deficient list
<syntaxhighlight lang="gap"># our deficient list
L :=
L :=
[ "ABCD", "CABD", "ACDB", "DACB", "BCDA",
[ "ABCD", "CABD", "ACDB", "DACB", "BCDA",
Line 1,281: Line 1,281:
# convert back to letters
# convert back to letters
s := "ABCD";
s := "ABCD";
List(v, p -> List(p, i -> s[i]));</lang>
List(v, p -> List(p, i -> s[i]));</syntaxhighlight>


=={{header|Go}}==
=={{header|Go}}==
Alternate method suggested by task description:
Alternate method suggested by task description:
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 1,331: Line 1,331:
}
}
fmt.Println(string(b))
fmt.Println(string(b))
}</lang>
}</syntaxhighlight>
Xor method suggested by Raku contributor:
Xor method suggested by Raku contributor:
<lang go>func main() {
<syntaxhighlight lang="go">func main() {
b := make([]byte, len(given[0]))
b := make([]byte, len(given[0]))
for _, p := range given {
for _, p := range given {
Line 1,341: Line 1,341:
}
}
fmt.Println(string(b))
fmt.Println(string(b))
}</lang>
}</syntaxhighlight>
{{out}} in either case:
{{out}} in either case:
<pre>
<pre>
Line 1,349: Line 1,349:
=={{header|Groovy}}==
=={{header|Groovy}}==
Solution:
Solution:
<lang groovy>def fact = { n -> [1,(1..<(n+1)).inject(1) { prod, i -> prod * i }].max() }
<syntaxhighlight lang="groovy">def fact = { n -> [1,(1..<(n+1)).inject(1) { prod, i -> prod * i }].max() }
def missingPerms
def missingPerms
missingPerms = {List elts, List perms ->
missingPerms = {List elts, List perms ->
Line 1,357: Line 1,357:
: missingPerms(elts - e, ePerms).collect { [e] + it }
: missingPerms(elts - e, ePerms).collect { [e] + it }
}.sum()
}.sum()
}</lang>
}</syntaxhighlight>


Test:
Test:
<lang groovy>def e = 'ABCD' as List
<syntaxhighlight lang="groovy">def e = 'ABCD' as List
def p = ['ABCD', 'CABD', 'ACDB', 'DACB', 'BCDA', 'ACBD', 'ADCB', 'CDAB', 'DABC', 'BCAD', 'CADB', 'CDBA',
def p = ['ABCD', 'CABD', 'ACDB', 'DACB', 'BCDA', 'ACBD', 'ADCB', 'CDAB', 'DABC', 'BCAD', 'CADB', 'CDBA',
'CBAD', 'ABDC', 'ADBC', 'BDCA', 'DCBA', 'BACD', 'BADC', 'BDAC', 'CBDA', 'DBCA', 'DCAB'].collect { it as List }
'CBAD', 'ABDC', 'ADBC', 'BDCA', 'DCBA', 'BACD', 'BADC', 'BDAC', 'CBDA', 'DBCA', 'DCAB'].collect { it as List }


def mp = missingPerms(e, p)
def mp = missingPerms(e, p)
mp.each { println it }</lang>
mp.each { println it }</syntaxhighlight>


{{out}}
{{out}}
Line 1,373: Line 1,373:
====Difference between two lists====
====Difference between two lists====
{{works with|GHC|7.10.3}}
{{works with|GHC|7.10.3}}
<lang haskell>import Data.List ((\\), permutations, nub)
<syntaxhighlight lang="haskell">import Data.List ((\\), permutations, nub)
import Control.Monad (join)
import Control.Monad (join)


Line 1,409: Line 1,409:


main :: IO ()
main :: IO ()
main = print $ missingPerm deficientPermsList</lang>
main = print $ missingPerm deficientPermsList</syntaxhighlight>
{{Out}}
{{Out}}
<pre>["DBAC"]</pre>
<pre>["DBAC"]</pre>
Line 1,416: Line 1,416:
Another, more statistical, approach is to return the least common letter in each of the four columns. (If all permutations were present, letter frequencies would not vary).
Another, more statistical, approach is to return the least common letter in each of the four columns. (If all permutations were present, letter frequencies would not vary).


<lang haskell>import Data.List (minimumBy, group, sort, transpose)
<syntaxhighlight lang="haskell">import Data.List (minimumBy, group, sort, transpose)
import Data.Ord (comparing)
import Data.Ord (comparing)


Line 1,452: Line 1,452:


main :: IO ()
main :: IO ()
main = print $ missingPerm deficientPermsList</lang>
main = print $ missingPerm deficientPermsList</syntaxhighlight>
{{Out}}
{{Out}}
<pre>"DBAC"</pre>
<pre>"DBAC"</pre>
Line 1,460: Line 1,460:
{{Trans|JavaScript}}
{{Trans|JavaScript}}
{{Trans|Python}}
{{Trans|Python}}
<lang haskell>import Data.Char (chr, ord)
<syntaxhighlight lang="haskell">import Data.Char (chr, ord)
import Data.Bits (xor)
import Data.Bits (xor)


Line 1,494: Line 1,494:


main :: IO ()
main :: IO ()
main = putStrLn $ missingPerm deficientPermsList</lang>
main = putStrLn $ missingPerm deficientPermsList</syntaxhighlight>
{{Out}}
{{Out}}
<pre>DBAC</pre>
<pre>DBAC</pre>


=={{header|Icon}} and {{header|Unicon}}==
=={{header|Icon}} and {{header|Unicon}}==
<lang Icon>link strings # for permutes
<syntaxhighlight lang="icon">link strings # for permutes


procedure main()
procedure main()
Line 1,510: Line 1,510:
write("The difference is : ")
write("The difference is : ")
every write(!givens, " ")
every write(!givens, " ")
end</lang>
end</syntaxhighlight>


The approach above generates a full set of permutations and calculates the difference. Changing the two commented lines to the three below will calculate on the fly and would be more efficient for larger data sets.
The approach above generates a full set of permutations and calculates the difference. Changing the two commented lines to the three below will calculate on the fly and would be more efficient for larger data sets.


<lang Icon>every x := permutes("ABCD") do # generate all permutations
<syntaxhighlight lang="icon">every x := permutes("ABCD") do # generate all permutations
if member(givens,x) then delete(givens,x) # remove givens as they are generated
if member(givens,x) then delete(givens,x) # remove givens as they are generated
else insert(givens,x) # add back any not given</lang>
else insert(givens,x) # add back any not given</syntaxhighlight>


A still more efficient version is:
A still more efficient version is:
<lang Icon>link strings
<syntaxhighlight lang="icon">link strings
procedure main()
procedure main()
Line 1,530: Line 1,530:
if not member(givens, p) then write(p)
if not member(givens, p) then write(p)
end</lang>
end</syntaxhighlight>


{{libheader|Icon Programming Library}}
{{libheader|Icon Programming Library}}
Line 1,537: Line 1,537:
=={{header|J}}==
=={{header|J}}==
'''Solution:'''
'''Solution:'''
<lang J>permutations=: A.~ i.@!@#
<syntaxhighlight lang="j">permutations=: A.~ i.@!@#
missingPerms=: -.~ permutations @ {.</lang>
missingPerms=: -.~ permutations @ {.</syntaxhighlight>
'''Use:'''
'''Use:'''
<pre>data=: >;: 'ABCD CABD ACDB DACB BCDA ACBD ADCB CDAB DABC BCAD CADB CDBA'
<pre>data=: >;: 'ABCD CABD ACDB DACB BCDA ACBD ADCB CDAB DABC BCAD CADB CDBA'
Line 1,550: Line 1,550:
Or the above could be a single definition that works the same way:
Or the above could be a single definition that works the same way:


<lang J>missingPerms=: -.~ (A.~ i.@!@#) @ {. </lang>
<syntaxhighlight lang="j">missingPerms=: -.~ (A.~ i.@!@#) @ {. </syntaxhighlight>


Or the equivalent explicit (cf. tacit above) definition:
Or the equivalent explicit (cf. tacit above) definition:
<lang J>missingPerms=: monad define
<syntaxhighlight lang="j">missingPerms=: monad define
item=. {. y
item=. {. y
y -.~ item A.~ i.! #item
y -.~ item A.~ i.! #item
)</lang>
)</syntaxhighlight>


Or, the solution could be obtained without defining an independent program:
Or, the solution could be obtained without defining an independent program:


<lang J> data -.~ 'ABCD' A.~ i.!4
<syntaxhighlight lang="j"> data -.~ 'ABCD' A.~ i.!4
DBAC</lang>
DBAC</syntaxhighlight>


Here, <code>'ABCD'</code> represents the values being permuted (their order does not matter), and <code>4</code> is how many of them we have.
Here, <code>'ABCD'</code> represents the values being permuted (their order does not matter), and <code>4</code> is how many of them we have.
Line 1,567: Line 1,567:
Yet another alternative expression, which uses parentheses instead of the [http://www.jsoftware.com/help/dictionary/d220v.htm passive operator] (<code>~</code>), would be:
Yet another alternative expression, which uses parentheses instead of the [http://www.jsoftware.com/help/dictionary/d220v.htm passive operator] (<code>~</code>), would be:


<lang J> ((i.!4) A. 'ABCD') -. data
<syntaxhighlight lang="j"> ((i.!4) A. 'ABCD') -. data
DBAC</lang>
DBAC</syntaxhighlight>


Of course the task suggests that the missing permutation can be found without generating all permutations. And of course that is doable:
Of course the task suggests that the missing permutation can be found without generating all permutations. And of course that is doable:


<lang J> 'ABCD'{~,I.@(= <./)@(#/.~)@('ABCD' , ])"1 |:perms
<syntaxhighlight lang="j"> 'ABCD'{~,I.@(= <./)@(#/.~)@('ABCD' , ])"1 |:perms
DBAC</lang>
DBAC</syntaxhighlight>


However, that's actually a false economy - not only does this approach take more code to implement (at least, in J) but we are already dealing with a data structure of approximately the size of all permutations. So what is being saved by this supposedly "more efficient" approach? Not much... (Still, perhaps this exercise is useful as an illustration of some kind of advertising concept?)
However, that's actually a false economy - not only does this approach take more code to implement (at least, in J) but we are already dealing with a data structure of approximately the size of all permutations. So what is being saved by this supposedly "more efficient" approach? Not much... (Still, perhaps this exercise is useful as an illustration of some kind of advertising concept?)


We could use parity, as suggested in the task hints:
We could use parity, as suggested in the task hints:
<lang J> ,(~.#~2|(#/.~))"1|:data
<syntaxhighlight lang="j"> ,(~.#~2|(#/.~))"1|:data
DBAC</lang>
DBAC</syntaxhighlight>


We could use arithmetic, as suggested in the task hints:
We could use arithmetic, as suggested in the task hints:
<lang J> ({.data){~|(->./)+/({.i.])data
<syntaxhighlight lang="j"> ({.data){~|(->./)+/({.i.])data
DBAC</lang>
DBAC</syntaxhighlight>


=={{header|Java}}==
=={{header|Java}}==
Line 1,589: Line 1,589:
Following needs: [[User:Margusmartsepp/Contributions/Java/Utils.java|Utils.java]]
Following needs: [[User:Margusmartsepp/Contributions/Java/Utils.java|Utils.java]]


<lang java>import java.util.ArrayList;
<syntaxhighlight lang="java">import java.util.ArrayList;


import com.google.common.base.Joiner;
import com.google.common.base.Joiner;
Line 1,608: Line 1,608:
System.out.println(joiner.join(cs));
System.out.println(joiner.join(cs));
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 1,615: Line 1,615:
Alternate version, based on checksumming each position:
Alternate version, based on checksumming each position:


<lang java>public class FindMissingPermutation
<syntaxhighlight lang="java">public class FindMissingPermutation
{
{
public static void main(String[] args)
public static void main(String[] args)
Line 1,638: Line 1,638:
System.out.println("Missing permutation: " + missingPermutation.toString());
System.out.println("Missing permutation: " + missingPermutation.toString());
}
}
}</lang>
}</syntaxhighlight>


=={{header|JavaScript}}==
=={{header|JavaScript}}==
Line 1,646: Line 1,646:


The permute() function taken from http://snippets.dzone.com/posts/show/1032
The permute() function taken from http://snippets.dzone.com/posts/show/1032
<lang javascript>permute = function(v, m){ //v1.0
<syntaxhighlight lang="javascript">permute = function(v, m){ //v1.0
for(var p = -1, j, k, f, r, l = v.length, q = 1, i = l + 1; --i; q *= i);
for(var p = -1, j, k, f, r, l = v.length, q = 1, i = l + 1; --i; q *= i);
for(x = [new Array(l), new Array(l), new Array(l), new Array(l)], j = q, k = l + 1, i = -1;
for(x = [new Array(l), new Array(l), new Array(l), new Array(l)], j = q, k = l + 1, i = -1;
Line 1,665: Line 1,665:


missing = all.filter(function(elem) {return list.indexOf(elem) == -1});
missing = all.filter(function(elem) {return list.indexOf(elem) == -1});
print(missing); // ==> DBAC</lang>
print(missing); // ==> DBAC</syntaxhighlight>


====Functional====
====Functional====


<lang JavaScript>(function (strList) {
<syntaxhighlight lang="javascript">(function (strList) {


// [a] -> [[a]]
// [a] -> [[a]]
Line 1,708: Line 1,708:
'ABCD\nCABD\nACDB\nDACB\nBCDA\nACBD\nADCB\nCDAB\nDABC\nBCAD\nCADB\n\
'ABCD\nCABD\nACDB\nDACB\nBCDA\nACBD\nADCB\nCDAB\nDABC\nBCAD\nCADB\n\
CDBA\nCBAD\nABDC\nADBC\nBDCA\nDCBA\nBACD\nBADC\nBDAC\nCBDA\nDBCA\nDCAB'
CDBA\nCBAD\nABDC\nADBC\nBDCA\nDCBA\nBACD\nBADC\nBDAC\nCBDA\nDBCA\nDCAB'
);</lang>
);</syntaxhighlight>


{{Out}}
{{Out}}


<lang JavaScript>["DBAC"]</lang>
<syntaxhighlight lang="javascript">["DBAC"]</syntaxhighlight>


===ES6===
===ES6===
====Statistical====
====Statistical====
=====Using a dictionary=====
=====Using a dictionary=====
<lang JavaScript>(() => {
<syntaxhighlight lang="javascript">(() => {
'use strict';
'use strict';


Line 1,749: Line 1,749:


// --> 'DBAC'
// --> 'DBAC'
})();</lang>
})();</syntaxhighlight>


{{Out}}
{{Out}}
Line 1,757: Line 1,757:
=====Composing functional primitives=====
=====Composing functional primitives=====
{{Trans|Haskell}}
{{Trans|Haskell}}
<lang JavaScript>(() => {
<syntaxhighlight lang="javascript">(() => {
'use strict';
'use strict';


Line 1,852: Line 1,852:


// -> "DBAC"
// -> "DBAC"
})();</lang>
})();</syntaxhighlight>
{{Out}}
{{Out}}
<pre>DBAC</pre>
<pre>DBAC</pre>
Line 1,858: Line 1,858:
====XOR====
====XOR====
Folding an xor operator over the list of character codes:
Folding an xor operator over the list of character codes:
<lang javaScript>(() => {
<syntaxhighlight lang="javascript">(() => {
'use strict';
'use strict';


Line 1,896: Line 1,896:


return main()
return main()
})();</lang>
})();</syntaxhighlight>
{{Out}}
{{Out}}
<pre>DBAC</pre>
<pre>DBAC</pre>
Line 1,920: Line 1,920:
If your version of jq has transpose/0, the definition given here
If your version of jq has transpose/0, the definition given here
(which is the same as in [[Matrix_Transpose#jq]]) may be omitted.
(which is the same as in [[Matrix_Transpose#jq]]) may be omitted.
<lang jq>def transpose:
<syntaxhighlight lang="jq">def transpose:
if (.[0] | length) == 0 then []
if (.[0] | length) == 0 then []
else [map(.[0])] + (map(.[1:]) | transpose)
else [map(.[0])] + (map(.[1:]) | transpose)
Line 1,941: Line 1,941:
# encode a string (e.g. "ABCD") as an array (e.g. [0,1,2,3]):
# encode a string (e.g. "ABCD") as an array (e.g. [0,1,2,3]):
def encode_string: [explode[] - 65];</lang>
def encode_string: [explode[] - 65];</syntaxhighlight>


'''The task''':
'''The task''':
<lang jq>map(encode_string) | transpose | map(parities | decode) | join("")</lang>
<syntaxhighlight lang="jq">map(encode_string) | transpose | map(parities | decode) | join("")</syntaxhighlight>


{{Out}}
{{Out}}
<lang sh>$ jq -R . Find_the_missing_permutation.txt | jq -s -f Find_the_missing_permutation.jq
<syntaxhighlight lang="sh">$ jq -R . Find_the_missing_permutation.txt | jq -s -f Find_the_missing_permutation.jq
"DBAC"</lang>
"DBAC"</syntaxhighlight>


=={{header|Julia}}==
=={{header|Julia}}==
Line 1,955: Line 1,955:
=== Obvious method ===
=== Obvious method ===
Calculate all possible permutations and return the first not included in the array.
Calculate all possible permutations and return the first not included in the array.
<lang julia>using BenchmarkTools, Combinatorics
<syntaxhighlight lang="julia">using BenchmarkTools, Combinatorics


function missingperm(arr::Vector)
function missingperm(arr::Vector)
Line 1,967: Line 1,967:
"CADB", "CDBA", "CBAD", "ABDC", "ADBC", "BDCA", "DCBA", "BACD", "BADC", "BDAC",
"CADB", "CDBA", "CBAD", "ABDC", "ADBC", "BDCA", "DCBA", "BACD", "BADC", "BDAC",
"CBDA", "DBCA", "DCAB"]
"CBDA", "DBCA", "DCAB"]
@show missingperm(arr)</lang>
@show missingperm(arr)</syntaxhighlight>


{{out}}
{{out}}
Line 1,974: Line 1,974:
=== Alternative method 1 ===
=== Alternative method 1 ===
{{trans|Python}}
{{trans|Python}}
<lang julia>function missingperm1(arr::Vector{<:AbstractString})
<syntaxhighlight lang="julia">function missingperm1(arr::Vector{<:AbstractString})
missperm = string()
missperm = string()
for pos in 1:length(arr[1])
for pos in 1:length(arr[1])
Line 1,986: Line 1,986:
return missperm
return missperm
end
end
</syntaxhighlight>
</lang>


=== Alternative method 2 ===
=== Alternative method 2 ===
{{trans|Raku}}
{{trans|Raku}}
<lang julia>function missingperm2(arr::Vector)
<syntaxhighlight lang="julia">function missingperm2(arr::Vector)
len = length(arr[1])
len = length(arr[1])
xorval = zeros(UInt8, len)
xorval = zeros(UInt8, len)
Line 2,006: Line 2,006:
@btime missingperm1(arr)
@btime missingperm1(arr)
@btime missingperm2(arr)
@btime missingperm2(arr)
</lang>{{out}}
</syntaxhighlight>{{out}}
<pre>
<pre>
missingperm(arr) = "DBAC"
missingperm(arr) = "DBAC"
Line 2,017: Line 2,017:


=={{header|K}}==
=={{header|K}}==
<lang K> split:{1_'(&x=y)_ x:y,x}
<syntaxhighlight lang="k"> split:{1_'(&x=y)_ x:y,x}


g: ("ABCD CABD ACDB DACB BCDA ACBD ADCB CDAB DABC BCAD CADB")
g: ("ABCD CABD ACDB DACB BCDA ACBD ADCB CDAB DABC BCAD CADB")
Line 2,040: Line 2,040:
p2@&~p2 _lin p
p2@&~p2 _lin p
"DBAC"</lang>
"DBAC"</syntaxhighlight>


Alternative approach:
Alternative approach:
<lang K>
<syntaxhighlight lang="k">
table:{b@<b:(x@*:'a),'#:'a:=x}
table:{b@<b:(x@*:'a),'#:'a:=x}
,/"ABCD"@&:'{5=(table p[;x])[;1]}'!4
,/"ABCD"@&:'{5=(table p[;x])[;1]}'!4
"DBAC"</lang>
"DBAC"</syntaxhighlight>


Third approach (where p is the given set of permutations):
Third approach (where p is the given set of permutations):
<syntaxhighlight lang="k">
<lang K>
,/p2@&~(p2:{x@m@&n=(#?:)'m:!n#n:#x}[*p]) _lin p
,/p2@&~(p2:{x@m@&n=(#?:)'m:!n#n:#x}[*p]) _lin p
</syntaxhighlight>
</lang>


=={{header|Kotlin}}==
=={{header|Kotlin}}==
<lang scala>// version 1.1.2
<syntaxhighlight lang="scala">// version 1.1.2


fun <T> permute(input: List<T>): List<List<T>> {
fun <T> permute(input: List<T>): List<List<T>> {
Line 2,087: Line 2,087:
for (perm in missing) println(perm.joinToString(""))
for (perm in missing) println(perm.joinToString(""))
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 2,096: Line 2,096:
=={{header|Lua}}==
=={{header|Lua}}==
Using the popular Penlight extension module - https://luarocks.org/modules/steved/penlight
Using the popular Penlight extension module - https://luarocks.org/modules/steved/penlight
<lang Lua>local permute, tablex = require("pl.permute"), require("pl.tablex")
<syntaxhighlight lang="lua">local permute, tablex = require("pl.permute"), require("pl.tablex")
local permList, pStr = {
local permList, pStr = {
"ABCD", "CABD", "ACDB", "DACB", "BCDA", "ACBD", "ADCB", "CDAB",
"ABCD", "CABD", "ACDB", "DACB", "BCDA", "ACBD", "ADCB", "CDAB",
Line 2,105: Line 2,105:
pStr = table.concat(perm)
pStr = table.concat(perm)
if not tablex.find(permList, pStr) then print(pStr) end
if not tablex.find(permList, pStr) then print(pStr) end
end</lang>
end</syntaxhighlight>
{{out}}
{{out}}
<pre>DBAC</pre>
<pre>DBAC</pre>


=={{header|Maple}}==
=={{header|Maple}}==
<lang Maple>lst := ["ABCD","CABD","ACDB","DACB","BCDA","ACBD","ADCB","CDAB","DABC","BCAD","CADB","CDBA","CBAD","ABDC","ADBC","BDCA","DCBA","BACD","BADC","BDAC","CBDA","DBCA","DCAB"]:
<syntaxhighlight lang="maple">lst := ["ABCD","CABD","ACDB","DACB","BCDA","ACBD","ADCB","CDAB","DABC","BCAD","CADB","CDBA","CBAD","ABDC","ADBC","BDCA","DCBA","BACD","BADC","BDAC","CBDA","DBCA","DCAB"]:
perm := table():
perm := table():
for letter in "ABCD" do
for letter in "ABCD" do
Line 2,120: Line 2,120:
end do:
end do:
end do:
end do:
print(StringTools:-Join(ListTools:-Flatten([indices(perm)], 4)[sort(map(x->60-x, ListTools:-Flatten([entries(perm)],4)),'output=permutation')], "")):</lang>
print(StringTools:-Join(ListTools:-Flatten([indices(perm)], 4)[sort(map(x->60-x, ListTools:-Flatten([entries(perm)],4)),'output=permutation')], "")):</syntaxhighlight>
{{Out|Output}}
{{Out|Output}}
<pre>"DBAC"</pre>
<pre>"DBAC"</pre>


=={{header|Mathematica}} / {{header|Wolfram Language}}==
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<lang Mathematica>ProvidedSet = {"ABCD" , "CABD" , "ACDB" , "DACB" , "BCDA" , "ACBD",
<syntaxhighlight lang="mathematica">ProvidedSet = {"ABCD" , "CABD" , "ACDB" , "DACB" , "BCDA" , "ACBD",
"ADCB" , "CDAB", "DABC", "BCAD" , "CADB", "CDBA" , "CBAD" , "ABDC",
"ADCB" , "CDAB", "DABC", "BCAD" , "CADB", "CDBA" , "CBAD" , "ABDC",
"ADBC" , "BDCA", "DCBA" , "BACD", "BADC", "BDAC" , "CBDA", "DBCA", "DCAB"};
"ADBC" , "BDCA", "DCBA" , "BACD", "BADC", "BDAC" , "CBDA", "DBCA", "DCAB"};
Line 2,132: Line 2,132:




->{"DBAC"}</lang>
->{"DBAC"}</syntaxhighlight>


=={{header|MATLAB}}==
=={{header|MATLAB}}==
This solution is designed to work on a column vector of strings. This will not work with a cell array or row vector of strings.
This solution is designed to work on a column vector of strings. This will not work with a cell array or row vector of strings.


<lang MATLAB>function perm = findMissingPerms(list)
<syntaxhighlight lang="matlab">function perm = findMissingPerms(list)


permsList = perms(list(1,:)); %Generate all permutations of the 4 letters
permsList = perms(list(1,:)); %Generate all permutations of the 4 letters
Line 2,163: Line 2,163:
end %for
end %for
end %fingMissingPerms</lang>
end %fingMissingPerms</syntaxhighlight>


{{out}}
{{out}}
<lang MATLAB>>> list = ['ABCD';
<syntaxhighlight lang="matlab">>> list = ['ABCD';
'CABD';
'CABD';
'ACDB';
'ACDB';
Line 2,220: Line 2,220:
ans =
ans =


DBAC</lang>
DBAC</syntaxhighlight>


=={{header|Nim}}==
=={{header|Nim}}==
{{trans|Python}}
{{trans|Python}}
<lang nim>import strutils
<syntaxhighlight lang="nim">import strutils


proc missingPermutation(arr: openArray[string]): string =
proc missingPermutation(arr: openArray[string]): string =
Line 2,242: Line 2,242:
CBAD ABDC ADBC BDCA DCBA BACD BADC BDAC CBDA DBCA DCAB""".splitWhiteSpace()
CBAD ABDC ADBC BDCA DCBA BACD BADC BDAC CBDA DBCA DCAB""".splitWhiteSpace()


echo missingPermutation(given)</lang>
echo missingPermutation(given)</syntaxhighlight>
{{out}}
{{out}}
<pre>DBAC</pre>
<pre>DBAC</pre>
Line 2,249: Line 2,249:


some utility functions:
some utility functions:
<lang ocaml>(* insert x at all positions into li and return the list of results *)
<syntaxhighlight lang="ocaml">(* insert x at all positions into li and return the list of results *)
let rec insert x = function
let rec insert x = function
| [] -> [[x]]
| [] -> [[x]]
Line 2,266: Line 2,266:
(* convert a char list to a string *)
(* convert a char list to a string *)
let string_of_chars cl =
let string_of_chars cl =
String.concat "" (List.map (String.make 1) cl)</lang>
String.concat "" (List.map (String.make 1) cl)</syntaxhighlight>


resolve the task:
resolve the task:


<lang ocaml>let deficient_perms = [
<syntaxhighlight lang="ocaml">let deficient_perms = [
"ABCD";"CABD";"ACDB";"DACB";
"ABCD";"CABD";"ACDB";"DACB";
"BCDA";"ACBD";"ADCB";"CDAB";
"BCDA";"ACBD";"ADCB";"CDAB";
Line 2,285: Line 2,285:
let results = List.filter (fun v -> not(List.mem v deficient_perms)) perms
let results = List.filter (fun v -> not(List.mem v deficient_perms)) perms


let () = List.iter print_endline results</lang>
let () = List.iter print_endline results</syntaxhighlight>


Alternate method : if we had all permutations,
Alternate method : if we had all permutations,
Line 2,293: Line 2,293:
of the number of occurences of each letter.
of the number of occurences of each letter.
The following program works with permutations of at least 3 letters:
The following program works with permutations of at least 3 letters:
<lang ocaml>let array_of_perm s =
<syntaxhighlight lang="ocaml">let array_of_perm s =
let n = String.length s in
let n = String.length s in
Array.init n (fun i -> int_of_char s.[i] - 65);;
Array.init n (fun i -> int_of_char s.[i] - 65);;
Line 2,322: Line 2,322:


find_missing deficient_perms;;
find_missing deficient_perms;;
(* - : string = "DBAC" *)</lang>
(* - : string = "DBAC" *)</syntaxhighlight>


=={{header|Octave}}==
=={{header|Octave}}==
<lang octave>given = [ 'ABCD';'CABD';'ACDB';'DACB'; ...
<syntaxhighlight lang="octave">given = [ 'ABCD';'CABD';'ACDB';'DACB'; ...
'BCDA';'ACBD';'ADCB';'CDAB'; ...
'BCDA';'ACBD';'ADCB';'CDAB'; ...
'DABC';'BCAD';'CADB';'CDBA'; ...
'DABC';'BCAD';'CADB';'CDBA'; ...
Line 2,339: Line 2,339:
bits(there) = 0;
bits(there) = 0;
missing = dec2base(find(bits)-1,'ABCD')
missing = dec2base(find(bits)-1,'ABCD')
</syntaxhighlight>
</lang>


=={{header|Oz}}==
=={{header|Oz}}==
Using constraint programming for this problem may be a bit overkill...
Using constraint programming for this problem may be a bit overkill...
<lang oz>declare
<syntaxhighlight lang="oz">declare
GivenPermutations =
GivenPermutations =
["ABCD" "CABD" "ACDB" "DACB" "BCDA" "ACBD" "ADCB" "CDAB" "DABC" "BCAD" "CADB" "CDBA"
["ABCD" "CABD" "ACDB" "DACB" "BCDA" "ACBD" "ADCB" "CDAB" "DABC" "BCAD" "CADB" "CDBA"
Line 2,362: Line 2,362:
{System.showInfo "Missing: "#P}
{System.showInfo "Missing: "#P}
end
end
end</lang>
end</syntaxhighlight>


=={{header|PARI/GP}}==
=={{header|PARI/GP}}==
<lang parigp>v=["ABCD","CABD","ACDB","DACB","BCDA","ACBD","ADCB","CDAB","DABC","BCAD","CADB","CDBA","CBAD","ABDC","ADBC","BDCA","DCBA","BACD","BADC","BDAC","CBDA","DBCA","DCAB"];
<syntaxhighlight lang="parigp">v=["ABCD","CABD","ACDB","DACB","BCDA","ACBD","ADCB","CDAB","DABC","BCAD","CADB","CDBA","CBAD","ABDC","ADBC","BDCA","DCBA","BACD","BADC","BDAC","CBDA","DBCA","DCAB"];
v=apply(u->permtonum(apply(n->n-64,Vec(Vecsmall(u)))),v);
v=apply(u->permtonum(apply(n->n-64,Vec(Vecsmall(u)))),v);
t=numtoperm(4, binomial(4!,2)-sum(i=1,#v,v[i]));
t=numtoperm(4, binomial(4!,2)-sum(i=1,#v,v[i]));
Strchr(apply(n->n+64,t))</lang>
Strchr(apply(n->n+64,t))</syntaxhighlight>
{{out}}
{{out}}
<pre>%1 = "DBAC"</pre>
<pre>%1 = "DBAC"</pre>
Line 2,374: Line 2,374:
=={{header|Pascal}}==
=={{header|Pascal}}==
like [[c]], summation, and [[Raku]] XORing
like [[c]], summation, and [[Raku]] XORing
<lang pascal>program MissPerm;
<syntaxhighlight lang="pascal">program MissPerm;
{$MODE DELPHI} //for result
{$MODE DELPHI} //for result


Line 2,436: Line 2,436:
writeln(CountOccurences,' is missing');
writeln(CountOccurences,' is missing');
writeln(CheckXOR,' is missing');
writeln(CheckXOR,' is missing');
end.</lang>{{out}}<pre>DBAC is missing
end.</syntaxhighlight>{{out}}<pre>DBAC is missing
DBAC is missing</pre>
DBAC is missing</pre>


Line 2,444: Line 2,444:
the first missing rotation is the target.
the first missing rotation is the target.


<lang Perl>sub check_perm {
<syntaxhighlight lang="perl">sub check_perm {
my %hash; @hash{@_} = ();
my %hash; @hash{@_} = ();
for my $s (@_) { exists $hash{$_} or return $_
for my $s (@_) { exists $hash{$_} or return $_
Line 2,453: Line 2,453:
@perms = qw(ABCD CABD ACDB DACB BCDA ACBD ADCB CDAB DABC BCAD CADB CDBA
@perms = qw(ABCD CABD ACDB DACB BCDA ACBD ADCB CDAB DABC BCAD CADB CDBA
CBAD ABDC ADBC BDCA DCBA BACD BADC BDAC CBDA DBCA DCAB);
CBAD ABDC ADBC BDCA DCBA BACD BADC BDAC CBDA DBCA DCAB);
print check_perm(@perms), "\n";</lang>
print check_perm(@perms), "\n";</syntaxhighlight>


{{out}}
{{out}}
Line 2,463: Line 2,463:
If the string XOR was of all the permutations, the result would be a string of nulls "\0",
If the string XOR was of all the permutations, the result would be a string of nulls "\0",
since one is missing, it is the result of XOR of all the rest :)
since one is missing, it is the result of XOR of all the rest :)
<lang perl>print eval join '^', map "'$_'", <>;</lang>
<syntaxhighlight lang="perl">print eval join '^', map "'$_'", <>;</syntaxhighlight>
or if you don't like eval...
or if you don't like eval...
<lang perl>$\ ^= $_ while <>;
<syntaxhighlight lang="perl">$\ ^= $_ while <>;
print '';</lang>
print '';</syntaxhighlight>
Every permutation has a "reverse", just take all reverses and remove the "normals".
Every permutation has a "reverse", just take all reverses and remove the "normals".
<lang perl>local $_ = join '', <>;
<syntaxhighlight lang="perl">local $_ = join '', <>;
my %h = map { $_, '' } reverse =~ /\w+/g;
my %h = map { $_, '' } reverse =~ /\w+/g;
delete @h{ /\w+/g };
delete @h{ /\w+/g };
print %h, "\n";</lang>
print %h, "\n";</syntaxhighlight>


=={{header|Phix}}==
=={{header|Phix}}==
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">perms</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"ABCD"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"CABD"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"ACDB"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"DACB"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"BCDA"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"ACBD"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"ADCB"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"CDAB"</span><span style="color: #0000FF;">,</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">perms</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"ABCD"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"CABD"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"ACDB"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"DACB"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"BCDA"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"ACBD"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"ADCB"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"CDAB"</span><span style="color: #0000FF;">,</span>
Line 2,523: Line 2,523:
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">r</span><span style="color: #0000FF;">})</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">r</span><span style="color: #0000FF;">})</span>
<span style="color: #000080;font-style:italic;">-- (relies on brute force(!) - but this is the only method that could be made to cope with &gt;1 omission)</span>
<span style="color: #000080;font-style:italic;">-- (relies on brute force(!) - but this is the only method that could be made to cope with &gt;1 omission)</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 2,533: Line 2,533:


=={{header|PHP}}==
=={{header|PHP}}==
<lang php><?php
<syntaxhighlight lang="php"><?php
$finalres = Array();
$finalres = Array();
function permut($arr,$result=array()){
function permut($arr,$result=array()){
Line 2,553: Line 2,553:
permut($given);
permut($given);
print_r(array_diff($finalres,$givenPerms)); // Array ( [20] => DBAC )
print_r(array_diff($finalres,$givenPerms)); // Array ( [20] => DBAC )
</syntaxhighlight>
</lang>


=={{header|Picat}}==
=={{header|Picat}}==
Line 2,559: Line 2,559:


All assume that the variables P1 and/or Perms has been defined:
All assume that the variables P1 and/or Perms has been defined:
<lang picat> P1 = ["ABCD","CABD","ACDB","DACB","BCDA","ACBD",
<syntaxhighlight lang="picat"> P1 = ["ABCD","CABD","ACDB","DACB","BCDA","ACBD",
"ADCB","CDAB","DABC","BCAD","CADB","CDBA",
"ADCB","CDAB","DABC","BCAD","CADB","CDBA",
"CBAD","ABDC","ADBC","BDCA","DCBA","BACD",
"CBAD","ABDC","ADBC","BDCA","DCBA","BACD",
Line 2,565: Line 2,565:
Perms = permutations("ABCD"),
Perms = permutations("ABCD"),
% ...
% ...
</syntaxhighlight>
</lang>


===Very imperative===
===Very imperative===
<lang Picat> % ...
<syntaxhighlight lang="picat"> % ...
Missing = _,
Missing = _,
foreach(P in Perms, Missing = _)
foreach(P in Perms, Missing = _)
Line 2,581: Line 2,581:
end
end
end,
end,
println(missing1=Missing).</lang>
println(missing1=Missing).</syntaxhighlight>


===Somewhat less imperative===
===Somewhat less imperative===
<lang Picat> % ...
<syntaxhighlight lang="picat"> % ...
Missing2 = _,
Missing2 = _,
foreach(P in Perms, Missing2 = _)
foreach(P in Perms, Missing2 = _)
Line 2,591: Line 2,591:
end
end
end,
end,
println(missing2=Missing2).</lang>
println(missing2=Missing2).</syntaxhighlight>


===Using findall===
===Using findall===
<lang Picat> % ...
<syntaxhighlight lang="picat"> % ...
println(missing3=difference(Perms,P1)).
println(missing3=difference(Perms,P1)).


difference(Xs,Ys) = findall(X,(member(X,Xs),not(member(X,Ys)))).</lang>
difference(Xs,Ys) = findall(X,(member(X,Xs),not(member(X,Ys)))).</syntaxhighlight>


===findall approach as a one-liner===
===findall approach as a one-liner===
<lang Picat> % ...
<syntaxhighlight lang="picat"> % ...
println(missing4=findall(X,(member(X,Perms),not(member(X,P1))))).</lang>
println(missing4=findall(X,(member(X,Perms),not(member(X,P1))))).</syntaxhighlight>


===Using ordsets===
===Using ordsets===
The module <code>ordsets</code> must be imported,
The module <code>ordsets</code> must be imported,
<lang Picat>import ordsets.
<syntaxhighlight lang="picat">import ordsets.
% ...
% ...
println(missing5=subtract(new_ordset(Perms),new_ordset(P1))).</lang>
println(missing5=subtract(new_ordset(Perms),new_ordset(P1))).</syntaxhighlight>


===List comprehension===
===List comprehension===
List comprehension with <code>membchk/1</code> for the check)
List comprehension with <code>membchk/1</code> for the check)
<lang Picat> % ...
<syntaxhighlight lang="picat"> % ...
println(missing6=[P:P in Perms,not membchk(P,P1)])</lang>
println(missing6=[P:P in Perms,not membchk(P,P1)])</syntaxhighlight>


===Using maps===
===Using maps===
<lang Picat> % ...
<syntaxhighlight lang="picat"> % ...
Map = new_map(),
Map = new_map(),
foreach(P in P1) Map.put(P,1) end,
foreach(P in P1) Map.put(P,1) end,
println(missing7=[P: P in Perms, not Map.has_key(P)]).</lang>
println(missing7=[P: P in Perms, not Map.has_key(P)]).</syntaxhighlight>


==="Merge sort" variants===
==="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".
"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> % ...
<syntaxhighlight lang="picat"> % ...
PermsSorted = Perms.sort(),
PermsSorted = Perms.sort(),
P1Sorted = P1.sort(),
P1Sorted = P1.sort(),
Line 2,637: Line 2,637:


% shorter
% shorter
println(missing10=[P:{P,PP} in zip(PermsSorted,P1Sorted ++ ["DUMMY"]), P != PP].first()),</lang>
println(missing10=[P:{P,PP} in zip(PermsSorted,P1Sorted ++ ["DUMMY"]), P != PP].first()),</syntaxhighlight>


===Constraint modelling===
===Constraint modelling===
The <code>cp</code> module must be imported.
The <code>cp</code> module must be imported.
<lang Picat>import cp.
<syntaxhighlight lang="picat">import cp.


% ...
% ...
Line 2,654: Line 2,654:
solve(Missing3),
solve(Missing3),
ABCD2 = "ABCD",
ABCD2 = "ABCD",
println(missing11=[ABCD2[I] : I in Missing3]).</lang>
println(missing11=[ABCD2[I] : I in Missing3]).</syntaxhighlight>


===Matrix approach===
===Matrix approach===
<lang Picat> % ...
<syntaxhighlight lang="picat"> % ...
PermsLen = Perms.length,
PermsLen = Perms.length,
P1Len = P1.length,
P1Len = P1.length,
Line 2,664: Line 2,664:
A2[I,J] := 1
A2[I,J] := 1
end,
end,
println(missing12=[Perms[I] : I in 1..PermsLen, sum([A2[I,J] : J in 1..P1Len])=0]).</lang>
println(missing12=[Perms[I] : I in 1..PermsLen, sum([A2[I,J] : J in 1..P1Len])=0]).</syntaxhighlight>


===Xor variant===
===Xor variant===
{{trans|Raku}}
{{trans|Raku}}
<lang Picat> % ...
<syntaxhighlight lang="picat"> % ...
println(missing13=to_fstring("%X",reduce(^,[parse_term("0x"++P):P in P1]))).</lang>
println(missing13=to_fstring("%X",reduce(^,[parse_term("0x"++P):P in P1]))).</syntaxhighlight>


===Count occurrences===
===Count occurrences===
Count the character with the least occurrence (=5) for each positions (1..4). Some variants.
Count the character with the least occurrence (=5) for each positions (1..4). Some variants.
{{trans|K}}
{{trans|K}}
<lang Picat> % ...
<syntaxhighlight lang="picat"> % ...
println(missing14=[[O:O=5 in Occ]:Occ in [occurrences([P[I]:P in P1]):I in 1..4]]),
println(missing14=[[O:O=5 in Occ]:Occ in [occurrences([P[I]:P in P1]):I in 1..4]]),


Line 2,704: Line 2,704:
% sort a map according to values
% sort a map according to values
sort2(Map) = [K=V:_=(K=V) in sort([V=(K=V): K=V in Map])]
sort2(Map) = [K=V:_=(K=V) in sort([V=(K=V): K=V in Map])]
</syntaxhighlight>
</lang>


Running all these snippets:
Running all these snippets:
Line 2,731: Line 2,731:


=={{header|PicoLisp}}==
=={{header|PicoLisp}}==
<lang PicoLisp>(setq *PermList
<syntaxhighlight lang="picolisp">(setq *PermList
(mapcar chop
(mapcar chop
(quote
(quote
Line 2,745: Line 2,745:
(rot L) )
(rot L) )
(unless (member Lst *PermList) # Check
(unless (member Lst *PermList) # Check
(prinl Lst) ) ) ) )</lang>
(prinl Lst) ) ) ) )</syntaxhighlight>
{{out}}
{{out}}
<pre>DBAC</pre>
<pre>DBAC</pre>
Line 2,752: Line 2,752:


{{works with|PowerShell|4.0}}
{{works with|PowerShell|4.0}}
<syntaxhighlight lang="powershell">
<lang PowerShell>
function permutation ($array) {
function permutation ($array) {
function generate($n, $array, $A) {
function generate($n, $array, $A) {
Line 2,809: Line 2,809:
)
)
$perm | where{-not $find.Contains($_)}
$perm | where{-not $find.Contains($_)}
</syntaxhighlight>
</lang>
<b>Output:</b>
<b>Output:</b>
<pre>
<pre>
Line 2,816: Line 2,816:


=={{header|PureBasic}}==
=={{header|PureBasic}}==
<lang PureBasic>Procedure in_List(in.s)
<syntaxhighlight lang="purebasic">Procedure in_List(in.s)
Define.i i, j
Define.i i, j
Define.s a
Define.s a
Line 2,854: Line 2,854:
Data.s "DABC","BCAD","CADB","CDBA","CBAD","ABDC","ADBC","BDCA"
Data.s "DABC","BCAD","CADB","CDBA","CBAD","ABDC","ADBC","BDCA"
Data.s "DCBA","BACD","BADC","BDAC","CBDA","DBCA","DCAB"
Data.s "DCBA","BACD","BADC","BDAC","CBDA","DBCA","DCAB"
EndDataSection</lang>
EndDataSection</syntaxhighlight>


Based on the [[Permutations#PureBasic|Permutations]] task,
Based on the [[Permutations#PureBasic|Permutations]] task,
the solution could be:
the solution could be:
<lang PureBasic>If OpenConsole()
<syntaxhighlight lang="purebasic">If OpenConsole()
NewList a.s()
NewList a.s()
findPermutations(a(), "ABCD", 4)
findPermutations(a(), "ABCD", 4)
Line 2,872: Line 2,872:
Print(#CRLF$ + "Press ENTER to exit"): Input()
Print(#CRLF$ + "Press ENTER to exit"): Input()
EndIf</lang>
EndIf</syntaxhighlight>


=={{header|Python}}==
=={{header|Python}}==
===Python: Calculate difference when compared to all permutations===
===Python: Calculate difference when compared to all permutations===
{{works with|Python|2.6+}}
{{works with|Python|2.6+}}
<lang python>from itertools import permutations
<syntaxhighlight lang="python">from itertools import permutations


given = '''ABCD CABD ACDB DACB BCDA ACBD ADCB CDAB DABC BCAD CADB CDBA
given = '''ABCD CABD ACDB DACB BCDA ACBD ADCB CDAB DABC BCAD CADB CDBA
Line 2,884: Line 2,884:
allPerms = [''.join(x) for x in permutations(given[0])]
allPerms = [''.join(x) for x in permutations(given[0])]


missing = list(set(allPerms) - set(given)) # ['DBAC']</lang>
missing = list(set(allPerms) - set(given)) # ['DBAC']</syntaxhighlight>


===Python:Counting lowest frequency character at each position===
===Python:Counting lowest frequency character at each position===
Line 2,890: Line 2,890:
i.e. it never needs to generate the full set of expected permutations.
i.e. it never needs to generate the full set of expected permutations.


<lang python>
<syntaxhighlight lang="python">
def missing_permutation(arr):
def missing_permutation(arr):
"Find the missing permutation in an array of N! - 1 permutations."
"Find the missing permutation in an array of N! - 1 permutations."
Line 2,921: Line 2,921:
print missing_permutation(given)
print missing_permutation(given)
</syntaxhighlight>
</lang>


===Python:Counting lowest frequency character at each position: functional===
===Python:Counting lowest frequency character at each position: functional===
Uses the same method as explained directly above,
Uses the same method as explained directly above,
but calculated in a more functional manner:
but calculated in a more functional manner:
<lang python>>>> from collections import Counter
<syntaxhighlight lang="python">>>> from collections import Counter
>>> given = '''ABCD CABD ACDB DACB BCDA ACBD ADCB CDAB DABC BCAD CADB CDBA
>>> given = '''ABCD CABD ACDB DACB BCDA ACBD ADCB CDAB DABC BCAD CADB CDBA
CBAD ABDC ADBC BDCA DCBA BACD BADC BDAC CBDA DBCA DCAB'''.split()
CBAD ABDC ADBC BDCA DCBA BACD BADC BDAC CBDA DBCA DCAB'''.split()
>>> ''.join(Counter(x).most_common()[-1][0] for x in zip(*given))
>>> ''.join(Counter(x).most_common()[-1][0] for x in zip(*given))
'DBAC'
'DBAC'
>>> </lang>
>>> </syntaxhighlight>


;Explanation
;Explanation
Line 2,940: Line 2,940:
created by the call to <code>most_common()</code>
created by the call to <code>most_common()</code>
is the least common character.
is the least common character.
<lang python>>>> from pprint import pprint as pp
<syntaxhighlight lang="python">>>> from pprint import pprint as pp
>>> pp(list(zip(*given)), width=120)
>>> pp(list(zip(*given)), width=120)
[('A', 'C', 'A', 'D', 'B', 'A', 'A', 'C', 'D', 'B', 'C', 'C', 'C', 'A', 'A', 'B', 'D', 'B', 'B', 'B', 'C', 'D', 'D'),
[('A', 'C', 'A', 'D', 'B', 'A', 'A', 'C', 'D', 'B', 'C', 'C', 'C', 'A', 'A', 'B', 'D', 'B', 'B', 'B', 'C', 'D', 'D'),
Line 2,957: Line 2,957:
>>> ''.join([Counter(x).most_common()[-1][0] for x in zip(*given)])
>>> ''.join([Counter(x).most_common()[-1][0] for x in zip(*given)])
'DBAC'
'DBAC'
>>> </lang>
>>> </syntaxhighlight>


===Python:Folding XOR over the set of strings===
===Python:Folding XOR over the set of strings===
Surfacing the missing bits:
Surfacing the missing bits:
{{Trans|JavaScript}}
{{Trans|JavaScript}}
<lang Python>'''Find the missing permutation'''
<syntaxhighlight lang="python">'''Find the missing permutation'''


from functools import reduce
from functools import reduce
Line 2,984: Line 2,984:
[0, 0, 0, 0]
[0, 0, 0, 0]
)
)
]))</lang>
]))</syntaxhighlight>
{{Out}}
{{Out}}
<pre>DBAC</pre>
<pre>DBAC</pre>
Line 2,992: Line 2,992:
Credit to [[#Raku|Raku]] for the method, and noting that the strings are valid hexadecimal numbers.
Credit to [[#Raku|Raku]] for the method, and noting that the strings are valid hexadecimal numbers.


<lang Quackery> $ "ABCD CABD ACDB DACB BCDA ACBD
<syntaxhighlight lang="quackery"> $ "ABCD CABD ACDB DACB BCDA ACBD
ADCB CDAB DABC BCAD CADB CDBA
ADCB CDAB DABC BCAD CADB CDBA
CBAD ABDC ADBC BDCA DCBA BACD
CBAD ABDC ADBC BDCA DCBA BACD
Line 3,001: Line 3,001:
0 swap witheach ^
0 swap witheach ^
number$ echo$
number$ echo$
base release</lang>
base release</syntaxhighlight>


{{out}}
{{out}}
Line 3,009: Line 3,009:
=={{header|R}}==
=={{header|R}}==
This uses the "combinat" package, which is a standard R package:
This uses the "combinat" package, which is a standard R package:
<lang>
<syntaxhighlight lang="text">
library(combinat)
library(combinat)


Line 3,022: Line 3,022:


setdiff(perms3, incomplete)
setdiff(perms3, incomplete)
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 3,030: Line 3,030:


=={{header|Racket}}==
=={{header|Racket}}==
<lang racket>
<syntaxhighlight lang="racket">
#lang racket
#lang racket


Line 3,066: Line 3,066:
c))
c))
;; -> '(D B A C)
;; -> '(D B A C)
</syntaxhighlight>
</lang>


=={{header|Raku}}==
=={{header|Raku}}==
(formerly Perl 6)
(formerly Perl 6)
<lang perl6>my @givens = <ABCD CABD ACDB DACB BCDA ACBD ADCB CDAB DABC BCAD CADB CDBA
<syntaxhighlight lang="raku" line>my @givens = <ABCD CABD ACDB DACB BCDA ACBD ADCB CDAB DABC BCAD CADB CDBA
CBAD ABDC ADBC BDCA DCBA BACD BADC BDAC CBDA DBCA DCAB>;
CBAD ABDC ADBC BDCA DCBA BACD BADC BDAC CBDA DBCA DCAB>;


my @perms = <A B C D>.permutations.map: *.join;
my @perms = <A B C D>.permutations.map: *.join;


.say when none(@givens) for @perms;</lang>
.say when none(@givens) for @perms;</syntaxhighlight>
{{out}}<pre>DBAC</pre>
{{out}}<pre>DBAC</pre>
Of course, all of these solutions are working way too hard,
Of course, all of these solutions are working way too hard,
when you can just xor all the bits,
when you can just xor all the bits,
and the missing one will just pop right out:
and the missing one will just pop right out:
<lang perl6>say [~^] <ABCD CABD ACDB DACB BCDA ACBD ADCB CDAB DABC BCAD CADB CDBA
<syntaxhighlight lang="raku" line>say [~^] <ABCD CABD ACDB DACB BCDA ACBD ADCB CDAB DABC BCAD CADB CDBA
CBAD ABDC ADBC BDCA DCBA BACD BADC BDAC CBDA DBCA DCAB>;</lang>
CBAD ABDC ADBC BDCA DCBA BACD BADC BDAC CBDA DBCA DCAB>;</syntaxhighlight>
{{out}}<pre>DBAC</pre>
{{out}}<pre>DBAC</pre>


=={{header|RapidQ}}==
=={{header|RapidQ}}==
<syntaxhighlight lang="vb">
<lang vb>
Dim PList as QStringList
Dim PList as QStringList
PList.addItems "ABCD", "CABD", "ACDB", "DACB", "BCDA", "ACBD", "ADCB", "CDAB"
PList.addItems "ABCD", "CABD", "ACDB", "DACB", "BCDA", "ACBD", "ADCB", "CDAB"
Line 3,110: Line 3,110:
showmessage MPerm
showmessage MPerm
'= DBAC
'= DBAC
</syntaxhighlight>
</lang>


=={{header|REXX}}==
=={{header|REXX}}==
<lang rexx>/*REXX pgm finds one or more missing permutations from an internal list & displays them.*/
<syntaxhighlight lang="rexx">/*REXX pgm finds one or more missing permutations from an internal list & displays them.*/
list= 'ABCD CABD ACDB DACB BCDA ACBD ADCB CDAB DABC BCAD CADB CDBA CBAD ABDC ADBC BDCA',
list= 'ABCD CABD ACDB DACB BCDA ACBD ADCB CDAB DABC BCAD CADB CDBA CBAD ABDC ADBC BDCA',
"DCBA BACD BADC BDAC CBDA DBCA DCAB" /*list that is missing one permutation.*/
"DCBA BACD BADC BDAC CBDA DBCA DCAB" /*list that is missing one permutation.*/
Line 3,141: Line 3,141:
call permSet ?+1 /*call self recursively. */
call permSet ?+1 /*call self recursively. */
end /*x*/
end /*x*/
return</lang>
return</syntaxhighlight>
{{out|output|text=&nbsp; when using the default input:}}
{{out|output|text=&nbsp; when using the default input:}}
<pre>
<pre>
Line 3,148: Line 3,148:


=={{header|Ring}}==
=={{header|Ring}}==
<lang ring>
<syntaxhighlight lang="ring">
list = "ABCD CABD ACDB DACB BCDA ACBD ADCB CDAB DABC BCAD CADB CDBA CBAD ABDC ADBC BDCA DCBA BACD BADC BDAC CBDA DBCA DCAB"
list = "ABCD CABD ACDB DACB BCDA ACBD ADCB CDAB DABC BCAD CADB CDBA CBAD ABDC ADBC BDCA DCBA BACD BADC BDAC CBDA DBCA DCAB"
Line 3,162: Line 3,162:
next
next
next
next
</syntaxhighlight>
</lang>
Output:
Output:
<pre>
<pre>
Line 3,170: Line 3,170:
=={{header|Ruby}}==
=={{header|Ruby}}==
{{works with|Ruby|2.0+}}
{{works with|Ruby|2.0+}}
<lang ruby>given = %w{
<syntaxhighlight lang="ruby">given = %w{
ABCD CABD ACDB DACB BCDA ACBD ADCB CDAB DABC BCAD CADB CDBA
ABCD CABD ACDB DACB BCDA ACBD ADCB CDAB DABC BCAD CADB CDBA
CBAD ABDC ADBC BDCA DCBA BACD BADC BDAC CBDA DBCA DCAB
CBAD ABDC ADBC BDCA DCBA BACD BADC BDAC CBDA DBCA DCAB
Line 3,177: Line 3,177:
all = given[0].chars.permutation.collect(&:join)
all = given[0].chars.permutation.collect(&:join)
puts "missing: #{all - given}"</lang>
puts "missing: #{all - given}"</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 3,184: Line 3,184:


=={{header|Run BASIC}}==
=={{header|Run BASIC}}==
<lang runbasic>list$ = "ABCD CABD ACDB DACB BCDA ACBD ADCB CDAB DABC BCAD CADB CDBA CBAD ABDC ADBC BDCA DCBA BACD BADC BDAC CBDA DBCA DCAB"
<syntaxhighlight lang="runbasic">list$ = "ABCD CABD ACDB DACB BCDA ACBD ADCB CDAB DABC BCAD CADB CDBA CBAD ABDC ADBC BDCA DCBA BACD BADC BDAC CBDA DBCA DCAB"


for a = asc("A") to asc("D")
for a = asc("A") to asc("D")
Line 3,199: Line 3,199:
next c
next c
next b
next b
next a</lang>
next a</syntaxhighlight>
{{out}}
{{out}}
<pre>DBAC missing</pre>
<pre>DBAC missing</pre>
Line 3,206: Line 3,206:
{{trans|Go}}
{{trans|Go}}
Xor method suggested by Raku contributor:
Xor method suggested by Raku contributor:
<lang rust>const GIVEN_PERMUTATIONS: [&str; 23] = [
<syntaxhighlight lang="rust">const GIVEN_PERMUTATIONS: [&str; 23] = [
"ABCD",
"ABCD",
"CABD",
"CABD",
Line 3,245: Line 3,245:
}
}


</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 3,254: Line 3,254:
{{libheader|Scala}}
{{libheader|Scala}}
{{works with|Scala|2.8}}
{{works with|Scala|2.8}}
<lang scala>def fat(n: Int) = (2 to n).foldLeft(1)(_*_)
<syntaxhighlight lang="scala">def fat(n: Int) = (2 to n).foldLeft(1)(_*_)
def perm[A](x: Int, a: Seq[A]): Seq[A] = if (x == 0) a else {
def perm[A](x: Int, a: Seq[A]): Seq[A] = if (x == 0) a else {
val n = a.size
val n = a.size
Line 3,293: Line 3,293:
DBCA
DBCA
DCAB""".stripMargin.split("\n")
DCAB""".stripMargin.split("\n")
println(findMissingPerm(perms(0), perms))</lang>
println(findMissingPerm(perms(0), perms))</syntaxhighlight>


===Scala 2.9.x===
===Scala 2.9.x===
{{works with|Scala|2.9.1}}
{{works with|Scala|2.9.1}}
<lang Scala>println("missing perms: "+("ABCD".permutations.toSet
<syntaxhighlight lang="scala">println("missing perms: "+("ABCD".permutations.toSet
--"ABCD CABD ACDB DACB BCDA ACBD ADCB CDAB DABC BCAD CADB CDBA CBAD ABDC ADBC BDCA DCBA BACD BADC BDAC CBDA DBCA DCAB".stripMargin.split(" ").toSet))</lang>
--"ABCD CABD ACDB DACB BCDA ACBD ADCB CDAB DABC BCAD CADB CDBA CBAD ABDC ADBC BDCA DCBA BACD BADC BDAC CBDA DBCA DCAB".stripMargin.split(" ").toSet))</syntaxhighlight>


=={{header|Seed7}}==
=={{header|Seed7}}==
<lang seed7>$ include "seed7_05.s7i";
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i";
const func string: missingPermutation (in array string: perms) is func
const func string: missingPermutation (in array string: perms) is func
Line 3,333: Line 3,333:
"ADCB", "CDAB", "DABC", "BCAD", "CADB", "CDBA", "CBAD", "ABDC", "ADBC",
"ADCB", "CDAB", "DABC", "BCAD", "CADB", "CDBA", "CBAD", "ABDC", "ADBC",
"BDCA", "DCBA", "BACD", "BADC", "BDAC", "CBDA", "DBCA", "DCAB")));
"BDCA", "DCBA", "BACD", "BADC", "BDAC", "CBDA", "DBCA", "DCAB")));
end func;</lang>
end func;</syntaxhighlight>


{{out}}
{{out}}
Line 3,342: Line 3,342:
=={{header|Sidef}}==
=={{header|Sidef}}==
{{trans|Perl}}
{{trans|Perl}}
<lang ruby>func check_perm(arr) {
<syntaxhighlight lang="ruby">func check_perm(arr) {
var hash = Hash()
var hash = Hash()
hash.set_keys(arr...)
hash.set_keys(arr...)
Line 3,356: Line 3,356:
CBAD ABDC ADBC BDCA DCBA BACD BADC BDAC CBDA DBCA DCAB)
CBAD ABDC ADBC BDCA DCBA BACD BADC BDAC CBDA DBCA DCAB)


say check_perm(perms)</lang>
say check_perm(perms)</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 3,363: Line 3,363:


=={{header|TI-83 BASIC}}==
=={{header|TI-83 BASIC}}==
<lang ti83b>"ABCDCABDACDBDACBBCDAACBDADCBCDABDABCBCADCADBCDBACBADABDCADBCBDCADCBABACDBADCBDACCBDADBCADCAB"→Str0
<syntaxhighlight lang="ti83b">"ABCDCABDACDBDACBBCDAACBDADCBCDABDABCBCADCADBCDBACBADABDCADBCBDCADCBABACDBADCBDACCBDADBCADCAB"→Str0
"ABCD"→Str1
"ABCD"→Str1
length(Str0)→L
length(Str0)→L
Line 3,396: Line 3,396:
sub(Str4,2,4)→Str4
sub(Str4,2,4)→Str4
Disp "MISSING"
Disp "MISSING"
Disp Str4</lang>
Disp Str4</syntaxhighlight>


=={{header|Tcl}}==
=={{header|Tcl}}==
{{tcllib|struct::list}}
{{tcllib|struct::list}}
<lang tcl>
<syntaxhighlight lang="tcl">
package require struct::list
package require struct::list


Line 3,414: Line 3,414:
}
}
}
}
</syntaxhighlight>
</lang>


=={{header|Ursala}}==
=={{header|Ursala}}==
Line 3,420: Line 3,420:
and needn't be reinvented, but its definition is shown here in the interest of
and needn't be reinvented, but its definition is shown here in the interest of
comparison with other solutions.
comparison with other solutions.
<lang Ursala>permutations = ~&itB^?a\~&aNC *=ahPfatPRD refer ^C/~&a ~&ar&& ~&arh2falrtPXPRD</lang>
<syntaxhighlight lang="ursala">permutations = ~&itB^?a\~&aNC *=ahPfatPRD refer ^C/~&a ~&ar&& ~&arh2falrtPXPRD</syntaxhighlight>
The <code>~&j</code> operator computes set differences.
The <code>~&j</code> operator computes set differences.
<lang Ursala>#import std
<syntaxhighlight lang="ursala">#import std
#show+
#show+


Line 3,450: Line 3,450:
CBDA
CBDA
DBCA
DBCA
DCAB]-</lang>
DCAB]-</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 3,458: Line 3,458:
=={{header|VBScript}}==
=={{header|VBScript}}==
Uses the 3rd method approach by adding the columns.
Uses the 3rd method approach by adding the columns.
<syntaxhighlight lang="vb">
<lang vb>
arrp = Array("ABCD", "CABD", "ACDB", "DACB", "BCDA", "ACBD",_
arrp = Array("ABCD", "CABD", "ACDB", "DACB", "BCDA", "ACBD",_
"ADCB", "CDAB", "DABC", "BCAD", "CADB", "CDBA",_
"ADCB", "CDAB", "DABC", "BCAD", "CADB", "CDBA",_
Line 3,483: Line 3,483:


WScript.StdOut.WriteLine missing
WScript.StdOut.WriteLine missing
</syntaxhighlight>
</lang>


{{Out}}
{{Out}}
Line 3,492: Line 3,492:
{{libheader|Wren-set}}
{{libheader|Wren-set}}
{{libheader|Wren-perm}}
{{libheader|Wren-perm}}
<lang ecmascript>import "./set" for Set
<syntaxhighlight lang="ecmascript">import "./set" for Set
import "./perm" for Perm
import "./perm" for Perm


Line 3,516: Line 3,516:
System.print("There are %(missing.count) missing permutations, namely:\n")
System.print("There are %(missing.count) missing permutations, namely:\n")
System.print(missing)
System.print(missing)
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 3,527: Line 3,527:
missperm <missperm.txt
missperm <missperm.txt


<lang XPL0>code HexIn=26, HexOut=27;
<syntaxhighlight lang="xpl0">code HexIn=26, HexOut=27;
int P, I;
int P, I;
[P:= 0;
[P:= 0;
for I:= 1 to 24-1 do P:= P xor HexIn(1);
for I:= 1 to 24-1 do P:= P xor HexIn(1);
HexOut(0, P);
HexOut(0, P);
]</lang>
]</syntaxhighlight>


{{out}}
{{out}}
Line 3,541: Line 3,541:
=={{header|zkl}}==
=={{header|zkl}}==
Since I just did the "generate the permutations" task, I'm going to use it to do the brute force solution.
Since I just did the "generate the permutations" task, I'm going to use it to do the brute force solution.
<lang zkl>var data=L("ABCD","CABD","ACDB","DACB","BCDA","ACBD","ADCB","CDAB",
<syntaxhighlight lang="zkl">var data=L("ABCD","CABD","ACDB","DACB","BCDA","ACBD","ADCB","CDAB",
"DABC","BCAD","CADB","CDBA","CBAD","ABDC","ADBC","BDCA",
"DABC","BCAD","CADB","CDBA","CBAD","ABDC","ADBC","BDCA",
"DCBA","BACD","BADC","BDAC","CBDA","DBCA","DCAB");
"DCBA","BACD","BADC","BDAC","CBDA","DBCA","DCAB");
Utils.Helpers.permute(["A".."D"]).apply("concat").copy().remove(data.xplode());</lang>
Utils.Helpers.permute(["A".."D"]).apply("concat").copy().remove(data.xplode());</syntaxhighlight>
Copy creates a read/write list from a read only list.
Copy creates a read/write list from a read only list.
xplode() pushes all elements of data as parameters to remove.
xplode() pushes all elements of data as parameters to remove.
Line 3,553: Line 3,553:


=={{header|ZX Spectrum Basic}}==
=={{header|ZX Spectrum Basic}}==
<lang zxbasic>10 LET l$="ABCD CABD ACDB DACB BCDA ACBD ADCB CDAB DABC BCAD CADB CDBA CBAD ABDC ADBC BDCA DCBA BACD BADC BDAC CBDA DBCA DCAB"
<syntaxhighlight lang="zxbasic">10 LET l$="ABCD CABD ACDB DACB BCDA ACBD ADCB CDAB DABC BCAD CADB CDBA CBAD ABDC ADBC BDCA DCBA BACD BADC BDAC CBDA DBCA DCAB"
20 LET length=LEN l$
20 LET length=LEN l$
30 FOR a= CODE "A" TO CODE "D"
30 FOR a= CODE "A" TO CODE "D"
Line 3,566: Line 3,566:
120 NEXT i
120 NEXT i
130 PRINT x$;" is missing"
130 PRINT x$;" is missing"
140 NEXT d: NEXT c: NEXT b: NEXT a</lang>
140 NEXT d: NEXT c: NEXT b: NEXT a</syntaxhighlight>