Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
2
Task/Find-the-missing-permutation/00-META.yaml
Normal file
2
Task/Find-the-missing-permutation/00-META.yaml
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
---
|
||||
from: http://rosettacode.org/wiki/Find_the_missing_permutation
|
||||
53
Task/Find-the-missing-permutation/00-TASK.txt
Normal file
53
Task/Find-the-missing-permutation/00-TASK.txt
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
<pre>
|
||||
ABCD
|
||||
CABD
|
||||
ACDB
|
||||
DACB
|
||||
BCDA
|
||||
ACBD
|
||||
ADCB
|
||||
CDAB
|
||||
DABC
|
||||
BCAD
|
||||
CADB
|
||||
CDBA
|
||||
CBAD
|
||||
ABDC
|
||||
ADBC
|
||||
BDCA
|
||||
DCBA
|
||||
BACD
|
||||
BADC
|
||||
BDAC
|
||||
CBDA
|
||||
DBCA
|
||||
DCAB
|
||||
</pre>
|
||||
|
||||
Listed above are all-but-one of the permutations of the symbols '''A''', '''B''', '''C''', and '''D''', ''except'' for one permutation that's ''not'' listed.
|
||||
|
||||
|
||||
;Task:
|
||||
Find that missing permutation.
|
||||
|
||||
|
||||
;Methods:
|
||||
* Obvious method:
|
||||
enumerate all permutations of '''A''', '''B''', '''C''', and '''D''',
|
||||
and then look for the missing permutation.
|
||||
|
||||
* alternate method:
|
||||
Hint: if all permutations were shown above, how many
|
||||
times would '''A''' appear in each position?
|
||||
What is the ''parity'' of this number?
|
||||
|
||||
* another alternate method:
|
||||
Hint: if you add up the letter values of each column,
|
||||
does a missing letter '''A''', '''B''', '''C''', and '''D''' from each
|
||||
column cause the total value for each column to be unique?
|
||||
|
||||
|
||||
;Related task:
|
||||
* [[Permutations]])
|
||||
<br><br>
|
||||
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
V perms = [‘ABCD’, ‘CABD’, ‘ACDB’, ‘DACB’, ‘BCDA’, ‘ACBD’, ‘ADCB’, ‘CDAB’,
|
||||
‘DABC’, ‘BCAD’, ‘CADB’, ‘CDBA’, ‘CBAD’, ‘ABDC’, ‘ADBC’, ‘BDCA’,
|
||||
‘DCBA’, ‘BACD’, ‘BADC’, ‘BDAC’, ‘CBDA’, ‘DBCA’, ‘DCAB’]
|
||||
|
||||
V missing = ‘’
|
||||
L(i) 4
|
||||
V cnt = [0] * 4
|
||||
L(j) 0 .< perms.len
|
||||
cnt[perms[j][i].code - ‘A’.code]++
|
||||
L(j) 4
|
||||
I cnt[j] != factorial(4-1)
|
||||
missing ‘’= Char(code' ‘A’.code + j)
|
||||
L.break
|
||||
|
||||
print(missing)
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
* Find the missing permutation - 19/10/2015
|
||||
PERMMISX CSECT
|
||||
USING PERMMISX,R15 set base register
|
||||
LA R4,0 i=0
|
||||
LA R6,1 step
|
||||
LA R7,23 to
|
||||
LOOPI BXH R4,R6,ELOOPI do i=1 to hbound(perms)
|
||||
LA R5,0 j=0
|
||||
LA R8,1 step
|
||||
LA R9,4 to
|
||||
LOOPJ BXH R5,R8,ELOOPJ do j=1 to hbound(miss)
|
||||
LR R1,R4 i
|
||||
SLA R1,2 *4
|
||||
LA R3,PERMS-5(R1) @perms(i)
|
||||
AR R3,R5 j
|
||||
LA R2,MISS-1(R5) @miss(j)
|
||||
XC 0(1,R2),0(R3) miss(j)=miss(j) xor substr(perms(i),j,1)
|
||||
B LOOPJ
|
||||
ELOOPJ B LOOPI
|
||||
ELOOPI XPRNT MISS,15 print buffer
|
||||
XR R15,R15 set return code
|
||||
BR R14 return to caller
|
||||
PERMS DC C'ABCD',C'CABD',C'ACDB',C'DACB',C'BCDA',C'ACBD'
|
||||
DC C'ADCB',C'CDAB',C'DABC',C'BCAD',C'CADB',C'CDBA'
|
||||
DC C'CBAD',C'ABDC',C'ADBC',C'BDCA',C'DCBA',C'BACD'
|
||||
DC C'BADC',C'BDAC',C'CBDA',C'DBCA',C'DCAB'
|
||||
MISS DC 4XL1'00',C' is missing' buffer
|
||||
YREGS
|
||||
END PERMMISX
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
PRMLEN: equ 4 ; length of permutation string
|
||||
puts: equ 9 ; CP/M print string
|
||||
org 100h
|
||||
lxi d,perms ; Start with first permutation
|
||||
perm: lxi h,mperm ; Missing permutation
|
||||
mvi b,PRMLEN ; Length of permutation
|
||||
char: ldax d ; Load character
|
||||
ora a ; Done?
|
||||
jz done
|
||||
xra m ; If not, XOR into missing permutation
|
||||
mov m,a
|
||||
inx h ; Increment pointers
|
||||
inx d
|
||||
dcr b ; Next character of current permutation
|
||||
jnz char
|
||||
jmp perm ; Next permutation
|
||||
done: lxi d,msg ; Print the message and exit
|
||||
mvi c,puts
|
||||
jmp 5
|
||||
msg: db 'Missing permutation: '
|
||||
mperm: db 0,0,0,0,'$' ; placeholder
|
||||
perms: db 'ABCD','CABD','ACDB','DACB','BCDA','ACBD','ADCB','CDAB'
|
||||
db 'DABC','BCAD','CADB','CDBA','CBAD','ABDC','ADBC','BDCA'
|
||||
db 'DCBA','BACD','BADC','BDAC','CBDA','DBCA','DCAB'
|
||||
db 0 ; end marker
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
cpu 8086
|
||||
org 100h
|
||||
mov si,perms ; Start of permutations
|
||||
xor bx,bx ; First word of permutation
|
||||
xor dx,dx ; Second word of permutation
|
||||
mov cx,23 ; There are 23 permutations given
|
||||
perm: lodsw ; Load first word of permutation
|
||||
xor bx,ax ; XOR with first word of missing
|
||||
lodsw ; Load second word of permutation
|
||||
xor dx,ax ; XOR with second word of missing
|
||||
loop perm ; Get next permutation
|
||||
mov [mperm],bx ; Store in placeholder
|
||||
mov [mperm+2],dx
|
||||
mov ah,9 ; Write output
|
||||
mov dx,msg
|
||||
int 21h
|
||||
ret
|
||||
msg: db 'Missing permutation: '
|
||||
mperm: db 0,0,0,0,'$' ; Placeholder
|
||||
perms: db 'ABCD','CABD','ACDB','DACB','BCDA','ACBD','ADCB','CDAB'
|
||||
db 'DABC','BCAD','CADB','CDBA','CBAD','ABDC','ADBC','BDCA'
|
||||
db 'DCBA','BACD','BADC','BDAC','CBDA','DBCA','DCAB'
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
BEGIN # find the missing permutation in a list using the XOR method of the Raku sample #
|
||||
# the list to find the missing permutation of #
|
||||
[]STRING list = ( "ABCD", "CABD", "ACDB", "DACB", "BCDA"
|
||||
, "ACBD", "ADCB", "CDAB", "DABC", "BCAD"
|
||||
, "CADB", "CDBA", "CBAD", "ABDC", "ADBC"
|
||||
, "BDCA", "DCBA", "BACD", "BADC", "BDAC"
|
||||
, "CBDA", "DBCA", "DCAB"
|
||||
);
|
||||
# sets b to b XOR v and returns b #
|
||||
PRIO XORAB = 1;
|
||||
OP XORAB = ( REF BITS b, BITS v )REF BITS: b := b XOR v;
|
||||
|
||||
# loop through each character of each element of the list #
|
||||
FOR c pos FROM LWB list[ LWB list ] TO UPB list[ LWB list ] DO
|
||||
# loop through each element of the list #
|
||||
BITS m := 16r0;
|
||||
FOR l pos FROM LWB list TO UPB list DO
|
||||
m XORAB BIN ABS list[ l pos ][ c pos ]
|
||||
OD;
|
||||
print( ( REPR ABS m ) )
|
||||
OD
|
||||
END
|
||||
|
|
@ -0,0 +1 @@
|
|||
missing ← ((⊂↓⍳¨⌊/) +⌿∘(⊢∘.=∪∘∊)) ⌷ ∪∘∊
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
perms←↑'ABCD' 'CABD' 'ACDB' 'DACB' 'BCDA' 'ACBD' 'ADCB' 'CDAB'
|
||||
perms⍪←↑'DABC' 'BCAD' 'CADB' 'CDBA' 'CBAD' 'ABDC' 'ADBC' 'BDCA'
|
||||
perms⍪←↑'DCBA' 'BACD' 'BADC' 'BDAC' 'CBDA' 'DBCA' 'DCAB'
|
||||
missing perms
|
||||
DBAC
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
{
|
||||
split($1,a,"");
|
||||
for (i=1;i<=4;++i) {
|
||||
t[i,a[i]]++;
|
||||
}
|
||||
}
|
||||
END {
|
||||
for (k in t) {
|
||||
split(k,a,SUBSEP)
|
||||
for (l in t) {
|
||||
split(l, b, SUBSEP)
|
||||
if (a[1] == b[1] && t[k] < t[l]) {
|
||||
s[a[1]] = a[2]
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
print s[1]s[2]s[3]s[4]
|
||||
}
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
PROC Main()
|
||||
DEFINE PTR="CARD"
|
||||
DEFINE COUNT="23"
|
||||
PTR ARRAY perm(COUNT)
|
||||
CHAR ARRAY s,missing=[4 0 0 0 0]
|
||||
BYTE i,j
|
||||
|
||||
perm(0)="ABCD" perm(1)="CABD"
|
||||
perm(2)="ACDB" perm(3)="DACB"
|
||||
perm(4)="BCDA" perm(5)="ACBD"
|
||||
perm(6)="ADCB" perm(7)="CDAB"
|
||||
perm(8)="DABC" perm(9)="BCAD"
|
||||
perm(10)="CADB" perm(11)="CDBA"
|
||||
perm(12)="CBAD" perm(13)="ABDC"
|
||||
perm(14)="ADBC" perm(15)="BDCA"
|
||||
perm(16)="DCBA" perm(17)="BACD"
|
||||
perm(18)="BADC" perm(19)="BDAC"
|
||||
perm(20)="CBDA" perm(21)="DBCA"
|
||||
perm(22)="DCAB"
|
||||
|
||||
FOR i=0 TO COUNT-1
|
||||
DO
|
||||
s=perm(i)
|
||||
FOR j=1 TO 4
|
||||
DO
|
||||
missing(j)==XOR s(j)
|
||||
OD
|
||||
OD
|
||||
|
||||
Print(missing)
|
||||
RETURN
|
||||
|
|
@ -0,0 +1,51 @@
|
|||
with Ada.Text_IO;
|
||||
procedure Missing_Permutations is
|
||||
subtype Permutation_Character is Character range 'A' .. 'D';
|
||||
|
||||
Character_Count : constant :=
|
||||
1 + Permutation_Character'Pos (Permutation_Character'Last)
|
||||
- Permutation_Character'Pos (Permutation_Character'First);
|
||||
|
||||
type Permutation_String is
|
||||
array (1 .. Character_Count) of Permutation_Character;
|
||||
|
||||
procedure Put (Item : Permutation_String) is
|
||||
begin
|
||||
for I in Item'Range loop
|
||||
Ada.Text_IO.Put (Item (I));
|
||||
end loop;
|
||||
end Put;
|
||||
|
||||
Given_Permutations : array (Positive range <>) of Permutation_String :=
|
||||
("ABCD", "CABD", "ACDB", "DACB", "BCDA", "ACBD",
|
||||
"ADCB", "CDAB", "DABC", "BCAD", "CADB", "CDBA",
|
||||
"CBAD", "ABDC", "ADBC", "BDCA", "DCBA", "BACD",
|
||||
"BADC", "BDAC", "CBDA", "DBCA", "DCAB");
|
||||
|
||||
Count : array (Permutation_Character, 1 .. Character_Count) of Natural
|
||||
:= (others => (others => 0));
|
||||
Max_Count : Positive := 1;
|
||||
|
||||
Missing_Permutation : Permutation_String;
|
||||
begin
|
||||
for I in Given_Permutations'Range loop
|
||||
for Pos in 1 .. Character_Count loop
|
||||
Count (Given_Permutations (I) (Pos), Pos) :=
|
||||
Count (Given_Permutations (I) (Pos), Pos) + 1;
|
||||
if Count (Given_Permutations (I) (Pos), Pos) > Max_Count then
|
||||
Max_Count := Count (Given_Permutations (I) (Pos), Pos);
|
||||
end if;
|
||||
end loop;
|
||||
end loop;
|
||||
|
||||
for Char in Permutation_Character loop
|
||||
for Pos in 1 .. Character_Count loop
|
||||
if Count (Char, Pos) < Max_Count then
|
||||
Missing_Permutation (Pos) := Char;
|
||||
end if;
|
||||
end loop;
|
||||
end loop;
|
||||
|
||||
Ada.Text_IO.Put_Line ("Missing Permutation:");
|
||||
Put (Missing_Permutation);
|
||||
end Missing_Permutations;
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
void
|
||||
paste(record r, index x, text p, integer a)
|
||||
{
|
||||
p = insert(p, -1, a);
|
||||
x.delete(a);
|
||||
if (~x) {
|
||||
x.vcall(paste, -1, r, x, p);
|
||||
} else {
|
||||
r[p] = 0;
|
||||
}
|
||||
x[a] = 0;
|
||||
}
|
||||
|
||||
integer
|
||||
main(void)
|
||||
{
|
||||
record r;
|
||||
list l;
|
||||
index x;
|
||||
|
||||
l.bill(0, "ABCD", "CABD", "ACDB", "DACB", "BCDA", "ACBD", "ADCB",
|
||||
"CDAB", "DABC", "BCAD", "CADB", "CDBA", "CBAD", "ABDC", "ADBC",
|
||||
"BDCA", "DCBA", "BACD", "BADC", "BDAC", "CBDA", "DBCA", "DCAB");
|
||||
|
||||
x['A'] = x['B'] = x['C'] = x['D'] = 0;
|
||||
|
||||
x.vcall(paste, -1, r, x, "");
|
||||
|
||||
l.ucall(r_delete, 1, r);
|
||||
|
||||
o_(r.low, "\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -0,0 +1,259 @@
|
|||
use framework "Foundation" -- ( sort )
|
||||
|
||||
--------------- RAREST LETTER IN EACH COLUMN -------------
|
||||
on run
|
||||
concat(map(composeList({¬
|
||||
head, ¬
|
||||
minimumBy(comparing(|length|)), ¬
|
||||
group, ¬
|
||||
sort}), ¬
|
||||
transpose(map(chars, ¬
|
||||
|words|("ABCD CABD ACDB DACB BCDA ACBD " & ¬
|
||||
"ADCB CDAB DABC BCAD CADB CDBA " & ¬
|
||||
"CBAD ABDC ADBC BDCA DCBA BACD " & ¬
|
||||
"BADC BDAC CBDA DBCA DCAB")))))
|
||||
|
||||
--> "DBAC"
|
||||
end run
|
||||
|
||||
|
||||
-------------------- GENERIC FUNCTIONS -------------------
|
||||
|
||||
-- chars :: String -> [String]
|
||||
on chars(s)
|
||||
characters of s
|
||||
end chars
|
||||
|
||||
|
||||
-- Ordering :: (-1 | 0 | 1)
|
||||
-- compare :: a -> a -> Ordering
|
||||
on compare(a, b)
|
||||
if a < b then
|
||||
-1
|
||||
else if a > b then
|
||||
1
|
||||
else
|
||||
0
|
||||
end if
|
||||
end compare
|
||||
|
||||
|
||||
-- comparing :: (a -> b) -> (a -> a -> Ordering)
|
||||
on comparing(f)
|
||||
script
|
||||
on |λ|(a, b)
|
||||
tell mReturn(f) to compare(|λ|(a), |λ|(b))
|
||||
end |λ|
|
||||
end script
|
||||
end comparing
|
||||
|
||||
|
||||
-- composeList :: [(a -> a)] -> (a -> a)
|
||||
on composeList(fs)
|
||||
script
|
||||
on |λ|(x)
|
||||
script go
|
||||
on |λ|(f, a)
|
||||
mReturn(f)'s |λ|(a)
|
||||
end |λ|
|
||||
end script
|
||||
foldr(go, x, fs)
|
||||
end |λ|
|
||||
end script
|
||||
end composeList
|
||||
|
||||
|
||||
-- concat :: [[a]] -> [a]
|
||||
-- concat :: [String] -> String
|
||||
on concat(xs)
|
||||
set lng to length of xs
|
||||
if 0 < lng and string is class of (item 1 of xs) then
|
||||
set acc to ""
|
||||
else
|
||||
set acc to {}
|
||||
end if
|
||||
repeat with i from 1 to lng
|
||||
set acc to acc & item i of xs
|
||||
end repeat
|
||||
acc
|
||||
end concat
|
||||
|
||||
|
||||
-- foldl :: (a -> b -> a) -> a -> [b] -> a
|
||||
on foldl(f, startValue, xs)
|
||||
tell mReturn(f)
|
||||
set v to startValue
|
||||
set lng to length of xs
|
||||
repeat with i from 1 to lng
|
||||
set v to |λ|(v, item i of xs, i, xs)
|
||||
end repeat
|
||||
return v
|
||||
end tell
|
||||
end foldl
|
||||
|
||||
|
||||
-- foldr :: (b -> a -> a) -> a -> [b] -> a
|
||||
on foldr(f, startValue, xs)
|
||||
tell mReturn(f)
|
||||
set v to startValue
|
||||
set lng to length of xs
|
||||
repeat with i from lng to 1 by -1
|
||||
set v to |λ|(item i of xs, v, i, xs)
|
||||
end repeat
|
||||
return v
|
||||
end tell
|
||||
end foldr
|
||||
|
||||
|
||||
-- group :: Eq a => [a] -> [[a]]
|
||||
on group(xs)
|
||||
script eq
|
||||
on |λ|(a, b)
|
||||
a = b
|
||||
end |λ|
|
||||
end script
|
||||
|
||||
groupBy(eq, xs)
|
||||
end group
|
||||
|
||||
|
||||
-- groupBy :: (a -> a -> Bool) -> [a] -> [[a]]
|
||||
on groupBy(f, xs)
|
||||
set mf to mReturn(f)
|
||||
|
||||
script enGroup
|
||||
on |λ|(a, x)
|
||||
if length of (active of a) > 0 then
|
||||
set h to item 1 of active of a
|
||||
else
|
||||
set h to missing value
|
||||
end if
|
||||
|
||||
if h is not missing value and mf's |λ|(h, x) then
|
||||
{active:(active of a) & x, sofar:sofar of a}
|
||||
else
|
||||
{active:{x}, sofar:(sofar of a) & {active of a}}
|
||||
end if
|
||||
end |λ|
|
||||
end script
|
||||
|
||||
if length of xs > 0 then
|
||||
set dct to foldl(enGroup, {active:{item 1 of xs}, sofar:{}}, tail(xs))
|
||||
if length of (active of dct) > 0 then
|
||||
sofar of dct & {active of dct}
|
||||
else
|
||||
sofar of dct
|
||||
end if
|
||||
else
|
||||
{}
|
||||
end if
|
||||
end groupBy
|
||||
|
||||
|
||||
-- head :: [a] -> a
|
||||
on head(xs)
|
||||
if length of xs > 0 then
|
||||
item 1 of xs
|
||||
else
|
||||
missing value
|
||||
end if
|
||||
end head
|
||||
|
||||
|
||||
-- intercalate :: Text -> [Text] -> Text
|
||||
on intercalate(strText, lstText)
|
||||
set {dlm, my text item delimiters} to {my text item delimiters, strText}
|
||||
set strJoined to lstText as text
|
||||
set my text item delimiters to dlm
|
||||
return strJoined
|
||||
end intercalate
|
||||
|
||||
|
||||
-- length :: [a] -> Int
|
||||
on |length|(xs)
|
||||
length of xs
|
||||
end |length|
|
||||
|
||||
|
||||
-- map :: (a -> b) -> [a] -> [b]
|
||||
on map(f, xs)
|
||||
tell mReturn(f)
|
||||
set lng to length of xs
|
||||
set lst to {}
|
||||
repeat with i from 1 to lng
|
||||
set end of lst to |λ|(item i of xs, i, xs)
|
||||
end repeat
|
||||
return lst
|
||||
end tell
|
||||
end map
|
||||
|
||||
|
||||
-- minimumBy :: (a -> a -> Ordering) -> [a] -> a
|
||||
on minimumBy(f)
|
||||
script
|
||||
on |λ|(xs)
|
||||
if length of xs < 1 then return missing value
|
||||
tell mReturn(f)
|
||||
set v to item 1 of xs
|
||||
repeat with x in xs
|
||||
if |λ|(x, v) < 0 then set v to x
|
||||
end repeat
|
||||
return v
|
||||
end tell
|
||||
end |λ|
|
||||
end script
|
||||
end minimumBy
|
||||
|
||||
|
||||
-- Lift 2nd class handler function into 1st class script wrapper
|
||||
-- mReturn :: Handler -> Script
|
||||
on mReturn(f)
|
||||
if class of f is script then
|
||||
f
|
||||
else
|
||||
script
|
||||
property |λ| : f
|
||||
end script
|
||||
end if
|
||||
end mReturn
|
||||
|
||||
|
||||
-- sort :: [a] -> [a]
|
||||
on sort(xs)
|
||||
((current application's NSArray's arrayWithArray:xs)'s ¬
|
||||
sortedArrayUsingSelector:"compare:") as list
|
||||
end sort
|
||||
|
||||
|
||||
-- tail :: [a] -> [a]
|
||||
on tail(xs)
|
||||
if length of xs > 1 then
|
||||
items 2 thru -1 of xs
|
||||
else
|
||||
{}
|
||||
end if
|
||||
end tail
|
||||
|
||||
|
||||
-- transpose :: [[a]] -> [[a]]
|
||||
on transpose(xss)
|
||||
script column
|
||||
on |λ|(_, iCol)
|
||||
script row
|
||||
on |λ|(xs)
|
||||
item iCol of xs
|
||||
end |λ|
|
||||
end script
|
||||
|
||||
map(row, xss)
|
||||
end |λ|
|
||||
end script
|
||||
|
||||
map(column, item 1 of xss)
|
||||
end transpose
|
||||
|
||||
|
||||
-- words :: String -> [String]
|
||||
on |words|(s)
|
||||
words of s
|
||||
end |words|
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
perms: [
|
||||
"ABCD" "CABD" "ACDB" "DACB" "BCDA" "ACBD" "ADCB" "CDAB" "DABC"
|
||||
"BCAD" "CADB" "CDBA" "CBAD" "ABDC" "ADBC" "BDCA" "DCBA" "BACD"
|
||||
"BADC" "BDAC" "CBDA" "DBCA" "DCAB"
|
||||
]
|
||||
|
||||
allPerms: map permutate split "ABCD" => join
|
||||
|
||||
print first difference allPerms perms
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
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" )
|
||||
Missing := ""
|
||||
|
||||
Loop, Parse, CompleteList, `n, `r
|
||||
If !InStr( IncompleteList , A_LoopField )
|
||||
Missing .= "`n" A_LoopField
|
||||
|
||||
MsgBox Missing Permutation(s):%Missing%
|
||||
|
||||
;-------------------------------------------------
|
||||
|
||||
; Shortened version of [VxE]'s permutation function
|
||||
; http://www.autohotkey.com/forum/post-322251.html#322251
|
||||
Perm( s , dL="" , t="" , p="") {
|
||||
StringSplit, m, s, % d := SubStr(dL,1,1) , %t%
|
||||
IfEqual, m0, 1, return m1 d p
|
||||
Loop %m0%
|
||||
{
|
||||
r := m1
|
||||
Loop % m0-2
|
||||
x := A_Index + 1, r .= d m%x%
|
||||
L .= Perm(r, d, t, m%m0% d p)"`n" , mx := m1
|
||||
Loop % m0-1
|
||||
x := A_Index + 1, m%A_Index% := m%x%
|
||||
m%m0% := mx
|
||||
}
|
||||
return substr(L, 1, -1)
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
DIM perms$(22), miss&(4)
|
||||
perms$() = "ABCD", "CABD", "ACDB", "DACB", "BCDA", "ACBD", "ADCB", \
|
||||
\ "CDAB", "DABC", "BCAD", "CADB", "CDBA", "CBAD", "ABDC", "ADBC", \
|
||||
\ "BDCA", "DCBA", "BACD", "BADC", "BDAC", "CBDA", "DBCA", "DCAB"
|
||||
|
||||
FOR i% = 0 TO DIM(perms$(),1)
|
||||
FOR j% = 1 TO DIM(miss&(),1)
|
||||
miss&(j%-1) EOR= ASCMID$(perms$(i%),j%)
|
||||
NEXT
|
||||
NEXT
|
||||
PRINT $$^miss&(0) " is missing"
|
||||
END
|
||||
|
|
@ -0,0 +1 @@
|
|||
ln"ABCD"r@\/\\
|
||||
|
|
@ -0,0 +1 @@
|
|||
ln)XXtp)><)F:)<]u[/v\[
|
||||
|
|
@ -0,0 +1,39 @@
|
|||
#include <algorithm>
|
||||
#include <vector>
|
||||
#include <set>
|
||||
#include <iterator>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
static const std::string GivenPermutations[] = {
|
||||
"ABCD","CABD","ACDB","DACB",
|
||||
"BCDA","ACBD","ADCB","CDAB",
|
||||
"DABC","BCAD","CADB","CDBA",
|
||||
"CBAD","ABDC","ADBC","BDCA",
|
||||
"DCBA","BACD","BADC","BDAC",
|
||||
"CBDA","DBCA","DCAB"
|
||||
};
|
||||
static const size_t NumGivenPermutations = sizeof(GivenPermutations) / sizeof(*GivenPermutations);
|
||||
|
||||
int main()
|
||||
{
|
||||
std::vector<std::string> permutations;
|
||||
std::string initial = "ABCD";
|
||||
permutations.push_back(initial);
|
||||
|
||||
while(true)
|
||||
{
|
||||
std::string p = permutations.back();
|
||||
std::next_permutation(p.begin(), p.end());
|
||||
if(p == permutations.front())
|
||||
break;
|
||||
permutations.push_back(p);
|
||||
}
|
||||
|
||||
std::vector<std::string> missing;
|
||||
std::set<std::string> given_permutations(GivenPermutations, GivenPermutations + NumGivenPermutations);
|
||||
std::set_difference(permutations.begin(), permutations.end(), given_permutations.begin(),
|
||||
given_permutations.end(), std::back_inserter(missing));
|
||||
std::copy(missing.begin(), missing.end(), std::ostream_iterator<std::string>(std::cout, "\n"));
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -0,0 +1,41 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace MissingPermutation
|
||||
{
|
||||
class Program
|
||||
{
|
||||
static void Main()
|
||||
{
|
||||
string[] given = new string[] { "ABCD", "CABD", "ACDB", "DACB",
|
||||
"BCDA", "ACBD", "ADCB", "CDAB",
|
||||
"DABC", "BCAD", "CADB", "CDBA",
|
||||
"CBAD", "ABDC", "ADBC", "BDCA",
|
||||
"DCBA", "BACD", "BADC", "BDAC",
|
||||
"CBDA", "DBCA", "DCAB" };
|
||||
|
||||
List<string> result = new List<string>();
|
||||
permuteString(ref result, "", "ABCD");
|
||||
|
||||
foreach (string a in result)
|
||||
if (Array.IndexOf(given, a) == -1)
|
||||
Console.WriteLine(a + " is a missing Permutation");
|
||||
}
|
||||
|
||||
public static void permuteString(ref List<string> result, string beginningString, string endingString)
|
||||
{
|
||||
if (endingString.Length <= 1)
|
||||
{
|
||||
result.Add(beginningString + endingString);
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int i = 0; i < endingString.Length; i++)
|
||||
{
|
||||
string newString = endingString.Substring(0, i) + endingString.Substring(i + 1);
|
||||
permuteString(ref result, beginningString + (endingString.ToCharArray())[i], newString);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
using System;
|
||||
using System.Linq;
|
||||
|
||||
public class Test
|
||||
{
|
||||
public static void Main()
|
||||
{
|
||||
var input = new [] {"ABCD","CABD","ACDB","DACB","BCDA",
|
||||
"ACBD","ADCB","CDAB","DABC","BCAD","CADB",
|
||||
"CDBA","CBAD","ABDC","ADBC","BDCA","DCBA",
|
||||
"BACD","BADC","BDAC","CBDA","DBCA","DCAB"};
|
||||
|
||||
int[] values = {0,0,0,0};
|
||||
foreach (string s in input)
|
||||
for (int i = 0; i < 4; i++)
|
||||
values[i] ^= s[i];
|
||||
Console.WriteLine(string.Join("", values.Select(i => (char)i)));
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
#include <stdio.h>
|
||||
|
||||
#define N 4
|
||||
const char *perms[] = {
|
||||
"ABCD", "CABD", "ACDB", "DACB", "BCDA", "ACBD", "ADCB", "CDAB",
|
||||
"DABC", "BCAD", "CADB", "CDBA", "CBAD", "ABDC", "ADBC", "BDCA",
|
||||
"DCBA", "BACD", "BADC", "BDAC", "CBDA", "DBCA", "DCAB",
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
int i, j, n, cnt[N];
|
||||
char miss[N];
|
||||
|
||||
for (n = i = 1; i < N; i++) n *= i; /* n = (N-1)!, # of occurrence */
|
||||
|
||||
for (i = 0; i < N; i++) {
|
||||
for (j = 0; j < N; j++) cnt[j] = 0;
|
||||
|
||||
/* count how many times each letter occur at position i */
|
||||
for (j = 0; j < sizeof(perms)/sizeof(const char*); j++)
|
||||
cnt[perms[j][i] - 'A']++;
|
||||
|
||||
/* letter not occurring (N-1)! times is the missing one */
|
||||
for (j = 0; j < N && cnt[j] == n; j++);
|
||||
|
||||
miss[i] = j + 'A';
|
||||
}
|
||||
printf("Missing: %.*s\n", N, miss);
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
(use 'clojure.math.combinatorics)
|
||||
(use 'clojure.set)
|
||||
|
||||
(def given (apply hash-set (partition 4 5 "ABCD CABD ACDB DACB BCDA ACBD ADCB CDAB DABC BCAD CADB CDBA CBAD ABDC ADBC BDCA DCBA BACD BADC BDAC CBDA DBCA DCAB" )))
|
||||
(def s1 (apply hash-set (permutations "ABCD")))
|
||||
(def missing (difference s1 given))
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
(def abcds ["ABCD" "CABD" "ACDB" "DACB" "BCDA" "ACBD" "ADCB" "CDAB"
|
||||
"DABC" "BCAD" "CADB" "CDBA" "CBAD" "ABDC" "ADBC" "BDCA"
|
||||
"DCBA" "BACD" "BADC" "BDAC" "CBDA" "DBCA" "DCAB"])
|
||||
|
||||
(def freqs (->> abcds (apply map vector) (map frequencies)))
|
||||
|
||||
(defn v->k [fqmap v] (->> fqmap (filter #(-> % second (= v))) ffirst))
|
||||
|
||||
(->> freqs (map #(v->k % 5)) (apply str) println)
|
||||
|
|
@ -0,0 +1,35 @@
|
|||
missing_permutation = (arr) ->
|
||||
# Find the missing permutation in an array of N! - 1 permutations.
|
||||
|
||||
# We won't validate every precondition, but we do have some basic
|
||||
# guards.
|
||||
if arr.length == 0
|
||||
throw Error "Need more data"
|
||||
if arr.length == 1
|
||||
return [arr[0][1] + arr[0][0]]
|
||||
|
||||
# Now we know that for each position in the string, elements should appear
|
||||
# an even number of times (N-1 >= 2). We can use a set to detect the element appearing
|
||||
# an odd number of times. Detect odd occurrences by toggling admission/expulsion
|
||||
# to and from the set for each value encountered. At the end of each pass one element
|
||||
# will remain in the set.
|
||||
result = ''
|
||||
for pos in [0...arr[0].length]
|
||||
set = {}
|
||||
for permutation in arr
|
||||
c = permutation[pos]
|
||||
if set[c]
|
||||
delete set[c]
|
||||
else
|
||||
set[c] = true
|
||||
for c of set
|
||||
result += c
|
||||
break
|
||||
result
|
||||
|
||||
given = '''ABCD CABD ACDB DACB BCDA ACBD ADCB CDAB DABC BCAD CADB CDBA
|
||||
CBAD ABDC ADBC BDCA DCBA BACD BADC BDAC CBDA DBCA DCAB'''
|
||||
|
||||
arr = (s for s in given.replace('\n', ' ').split ' ' when s != '')
|
||||
|
||||
console.log missing_permutation(arr)
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
(defparameter *permutations*
|
||||
'("ABCD" "CABD" "ACDB" "DACB" "BCDA" "ACBD" "ADCB" "CDAB" "DABC" "BCAD" "CADB" "CDBA"
|
||||
"CBAD" "ABDC" "ADBC" "BDCA" "DCBA" "BACD" "BADC" "BDAC" "CBDA" "DBCA" "DCAB"))
|
||||
|
||||
(defun missing-perm (perms)
|
||||
(let* ((letters (loop for i across (car perms) collecting i))
|
||||
(l (/ (1+ (length perms)) (length letters))))
|
||||
(labels ((enum (n) (loop for i below n collecting i))
|
||||
(least-occurs (pos)
|
||||
(let ((occurs (loop for i in perms collecting (aref i pos))))
|
||||
(cdr (assoc (1- l) (mapcar #'(lambda (letter)
|
||||
(cons (count letter occurs) letter))
|
||||
letters))))))
|
||||
(concatenate 'string (mapcar #'least-occurs (enum (length letters)))))))
|
||||
|
|
@ -0,0 +1,43 @@
|
|||
void main() {
|
||||
import std.stdio, std.string, std.algorithm, std.range, std.conv;
|
||||
|
||||
immutable perms = "ABCD CABD ACDB DACB BCDA ACBD ADCB CDAB DABC
|
||||
BCAD CADB CDBA CBAD ABDC ADBC BDCA DCBA BACD
|
||||
BADC BDAC CBDA DBCA DCAB".split;
|
||||
|
||||
// Version 1: test all permutations.
|
||||
immutable permsSet = perms
|
||||
.map!representation
|
||||
.zip(true.repeat)
|
||||
.assocArray;
|
||||
auto perm = perms[0].dup.representation;
|
||||
do {
|
||||
if (perm !in permsSet)
|
||||
writeln(perm.map!(c => char(c)));
|
||||
} while (perm.nextPermutation);
|
||||
|
||||
// Version 2: xor all the ASCII values, the uneven one
|
||||
// gets flushed out. Based on Raku (via Go).
|
||||
enum len = 4;
|
||||
char[len] b = 0;
|
||||
foreach (immutable p; perms)
|
||||
b[] ^= p[];
|
||||
b.writeln;
|
||||
|
||||
// Version 3: sum ASCII values.
|
||||
immutable rowSum = perms[0].sum;
|
||||
len
|
||||
.iota
|
||||
.map!(i => to!char(rowSum - perms.transversal(i).sum % rowSum))
|
||||
.writeln;
|
||||
|
||||
// Version 4: a checksum, Java translation. maxCode will be 36.
|
||||
immutable maxCode = reduce!q{a * b}(len - 1, iota(3, len + 1));
|
||||
|
||||
foreach (immutable i; 0 .. len) {
|
||||
immutable code = perms.map!(p => perms[0].countUntil(p[i])).sum;
|
||||
|
||||
// Code will come up 3, 1, 0, 2 short of 36.
|
||||
perms[0][maxCode - code].write;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
PROGRAM MISSING
|
||||
|
||||
CONST N=4
|
||||
|
||||
DIM PERMS$[23]
|
||||
|
||||
BEGIN
|
||||
PRINT(CHR$(12);) ! CLS
|
||||
DATA("ABCD","CABD","ACDB","DACB","BCDA","ACBD","ADCB")
|
||||
DATA("CDAB","DABC","BCAD","CADB","CDBA","CBAD","ABDC","ADBC")
|
||||
DATA("BDCA","DCBA","BACD","BADC","BDAC","CBDA","DBCA","DCAB")
|
||||
|
||||
FOR I%=1 TO UBOUND(PERMS$,1) DO
|
||||
READ(PERMS$[I%])
|
||||
END FOR
|
||||
|
||||
SOL$="...."
|
||||
|
||||
FOR I%=1 TO N DO
|
||||
CH$=CHR$(I%+64)
|
||||
COUNT%=0
|
||||
FOR Z%=1 TO N DO
|
||||
COUNT%=0
|
||||
FOR J%=1 TO UBOUND(PERMS$,1) DO
|
||||
IF CH$=MID$(PERMS$[J%],Z%,1) THEN COUNT%=COUNT%+1 END IF
|
||||
END FOR
|
||||
IF COUNT%<>6 THEN
|
||||
!$RCODE="MID$(SOL$,Z%,1)=CH$"
|
||||
END IF
|
||||
END FOR
|
||||
END FOR
|
||||
PRINT("Solution is: ";SOL$)
|
||||
END PROGRAM
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
;; use the obvious methos
|
||||
(lib 'list) ; for (permutations) function
|
||||
|
||||
;; input
|
||||
(define perms '
|
||||
(ABCD CABD ACDB DACB BCDA ACBD ADCB CDAB DABC BCAD CADB CDBA CBAD ABDC ADBC BDCA DCBA BACD BADC BDAC CBDA DBCA DCAB))
|
||||
|
||||
;; generate all permutations
|
||||
(define all-perms (map list->string (permutations '(A B C D))))
|
||||
→ all-perms
|
||||
|
||||
;; {set} substraction
|
||||
(set-substract (make-set all-perms) (make-set perms))
|
||||
→ { DBAC }
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
defmodule RC do
|
||||
def find_miss_perm(head, perms) do
|
||||
all_permutations(head) -- perms
|
||||
end
|
||||
|
||||
defp all_permutations(string) do
|
||||
list = String.split(string, "", trim: true)
|
||||
Enum.map(permutations(list), fn x -> Enum.join(x) end)
|
||||
end
|
||||
|
||||
defp permutations([]), do: [[]]
|
||||
defp permutations(list), do: (for x <- list, y <- permutations(list -- [x]), do: [x|y])
|
||||
end
|
||||
|
||||
perms = ["ABCD", "CABD", "ACDB", "DACB", "BCDA", "ACBD", "ADCB", "CDAB", "DABC", "BCAD", "CADB", "CDBA",
|
||||
"CBAD", "ABDC", "ADBC", "BDCA", "DCBA", "BACD", "BADC", "BDAC", "CBDA", "DBCA", "DCAB"]
|
||||
|
||||
IO.inspect RC.find_miss_perm( hd(perms), perms )
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
-module( find_missing_permutation ).
|
||||
|
||||
-export( [difference/2, task/0] ).
|
||||
|
||||
difference( Permutate_this, Existing_permutations ) -> all_permutations( Permutate_this ) -- Existing_permutations.
|
||||
|
||||
task() -> difference( "ABCD", existing_permutations() ).
|
||||
|
||||
|
||||
|
||||
all_permutations( String ) -> [[A, B, C, D] || A <- String, B <- String, C <- String, D <- String, is_different([A, B, C, D])].
|
||||
|
||||
existing_permutations() -> ["ABCD", "CABD", "ACDB", "DACB", "BCDA", "ACBD", "ADCB", "CDAB", "DABC", "BCAD", "CADB", "CDBA", "CBAD", "ABDC", "ADBC", "BDCA", "DCBA", "BACD", "BADC", "BDAC", "CBDA", "DBCA", "DCAB"].
|
||||
|
||||
is_different( [_H] ) -> true;
|
||||
is_different( [H | T] ) -> not lists:member(H, T) andalso is_different( T ).
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
USING: io math.combinatorics sequences sets ;
|
||||
|
||||
"ABCD" all-permutations lines diff first print
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
hex
|
||||
ABCD CABD xor ACDB xor DACB xor BCDA xor ACBD xor
|
||||
ADCB xor CDAB xor DABC xor BCAD xor CADB xor CDBA xor
|
||||
CBAD xor ABDC xor ADBC xor BDCA xor DCBA xor BACD xor
|
||||
BADC xor BDAC xor CBDA xor DBCA xor DCAB xor
|
||||
cr .( Missing permutation: ) u.
|
||||
decimal
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
program missing_permutation
|
||||
|
||||
implicit none
|
||||
character (4), dimension (23), parameter :: list = &
|
||||
& (/'ABCD', 'CABD', 'ACDB', 'DACB', 'BCDA', 'ACBD', 'ADCB', 'CDAB', &
|
||||
& 'DABC', 'BCAD', 'CADB', 'CDBA', 'CBAD', 'ABDC', 'ADBC', 'BDCA', &
|
||||
& 'DCBA', 'BACD', 'BADC', 'BDAC', 'CBDA', 'DBCA', 'DCAB'/)
|
||||
integer :: i, j, k
|
||||
|
||||
do i = 1, 4
|
||||
j = minloc ((/(count (list (:) (i : i) == list (1) (k : k)), k = 1, 4)/), 1)
|
||||
write (*, '(a)', advance = 'no') list (1) (j : j)
|
||||
end do
|
||||
write (*, *)
|
||||
|
||||
end program missing_permutation
|
||||
|
|
@ -0,0 +1,37 @@
|
|||
' version 30-03-2017
|
||||
' compile with: fbc -s console
|
||||
|
||||
Data "ABCD", "CABD", "ACDB", "DACB", "BCDA", "ACBD"
|
||||
Data "ADCB", "CDAB", "DABC", "BCAD", "CADB", "CDBA"
|
||||
Data "CBAD", "ABDC", "ADBC", "BDCA", "DCBA", "BACD"
|
||||
Data "BADC", "BDAC", "CBDA", "DBCA", "DCAB"
|
||||
|
||||
' ------=< MAIN >=------
|
||||
|
||||
Dim As ulong total(3, Asc("A") To Asc("D")) ' total(0 to 3, 65 to 68)
|
||||
Dim As ULong i, j, n = 24 \ 4 ' n! \ n
|
||||
Dim As String tmp
|
||||
|
||||
For i = 1 To 23
|
||||
Read tmp
|
||||
For j = 0 To 3
|
||||
total(j, tmp[j]) += 1
|
||||
Next
|
||||
Next
|
||||
|
||||
tmp = Space(4)
|
||||
For i = 0 To 3
|
||||
For j = Asc("A") To Asc("D")
|
||||
If total(i, j) <> n Then
|
||||
tmp[i] = j
|
||||
End If
|
||||
Next
|
||||
Next
|
||||
|
||||
Print "The missing permutation is : "; tmp
|
||||
|
||||
' empty keyboard buffer
|
||||
While InKey <> "" : Wend
|
||||
Print : Print "hit any key to end program"
|
||||
Sleep
|
||||
End
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
' version 30-03-2017
|
||||
' compile with: fbc -s console
|
||||
|
||||
Data "ABCD", "CABD", "ACDB", "DACB", "BCDA", "ACBD"
|
||||
Data "ADCB", "CDAB", "DABC", "BCAD", "CADB", "CDBA"
|
||||
Data "CBAD", "ABDC", "ADBC", "BDCA", "DCBA", "BACD"
|
||||
Data "BADC", "BDAC", "CBDA", "DBCA", "DCAB"
|
||||
|
||||
' ------=< MAIN >=------
|
||||
|
||||
Dim As ULong total(3) ' total(0 to 3)
|
||||
Dim As ULong i, j, n = 24 \ 4 ' n! \ n
|
||||
Dim As ULong total_val = (Asc("A") + Asc("B") + Asc("C") + Asc("D")) * n
|
||||
Dim As String tmp
|
||||
|
||||
For i = 1 To 23
|
||||
Read tmp
|
||||
For j = 0 To 3
|
||||
total(j) += tmp[j]
|
||||
Next
|
||||
Next
|
||||
|
||||
tmp = Space(4)
|
||||
For i = 0 To 3
|
||||
tmp[i] = total_val - total(i)
|
||||
Next
|
||||
|
||||
Print "The missing permutation is : "; tmp
|
||||
|
||||
' empty keyboard buffer
|
||||
While Inkey <> "" : Wend
|
||||
Print : Print "hit any key to end program"
|
||||
Sleep
|
||||
End
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
' version 30-03-2017
|
||||
' compile with: fbc -s console
|
||||
|
||||
Data "ABCD", "CABD", "ACDB", "DACB", "BCDA", "ACBD"
|
||||
Data "ADCB", "CDAB", "DABC", "BCAD", "CADB", "CDBA"
|
||||
Data "CBAD", "ABDC", "ADBC", "BDCA", "DCBA", "BACD"
|
||||
Data "BADC", "BDAC", "CBDA", "DBCA", "DCAB"
|
||||
|
||||
' ------=< MAIN >=------
|
||||
|
||||
Dim As ULong i,j
|
||||
Dim As String tmp, missing = chr(0, 0, 0, 0) ' or string(4, 0)
|
||||
|
||||
For i = 1 To 23
|
||||
Read tmp
|
||||
For j = 0 To 3
|
||||
missing[j] Xor= tmp[j]
|
||||
Next
|
||||
Next
|
||||
|
||||
Print "The missing permutation is : "; missing
|
||||
|
||||
' empty keyboard buffer
|
||||
While Inkey <> "" : Wend
|
||||
Print : Print "hit any key to end program"
|
||||
Sleep
|
||||
End
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
p = toSet[trim[splitLines["""ABCD
|
||||
CABD
|
||||
ACDB
|
||||
DACB
|
||||
BCDA
|
||||
ACBD
|
||||
ADCB
|
||||
CDAB
|
||||
DABC
|
||||
BCAD
|
||||
CADB
|
||||
CDBA
|
||||
CBAD
|
||||
ABDC
|
||||
ADBC
|
||||
BDCA
|
||||
DCBA
|
||||
BACD
|
||||
BADC
|
||||
BDAC
|
||||
CBDA
|
||||
DBCA
|
||||
DCAB"""]]]
|
||||
|
||||
s = ["A","B","C","D"]
|
||||
for n = s.lexicographicPermute[]
|
||||
{
|
||||
str = join["", n]
|
||||
if ! p.contains[str]
|
||||
println[str]
|
||||
}
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
# our deficient list
|
||||
L :=
|
||||
[ "ABCD", "CABD", "ACDB", "DACB", "BCDA",
|
||||
"ACBD", "ADCB", "CDAB", "DABC", "BCAD",
|
||||
"CADB", "CDBA", "CBAD", "ABDC", "ADBC",
|
||||
"BDCA", "DCBA", "BACD", "BADC", "BDAC",
|
||||
"CBDA", "DBCA", "DCAB" ];
|
||||
|
||||
# convert L to permutations on 1..4
|
||||
u := List(L, s -> List([1..4], i -> Position("ABCD", s[i])));
|
||||
|
||||
# set difference (with all permutations)
|
||||
v := Difference(PermutationsList([1..4]), u);
|
||||
|
||||
# convert back to letters
|
||||
s := "ABCD";
|
||||
List(v, p -> List(p, i -> s[i]));
|
||||
|
|
@ -0,0 +1,47 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var given = strings.Split(`ABCD
|
||||
CABD
|
||||
ACDB
|
||||
DACB
|
||||
BCDA
|
||||
ACBD
|
||||
ADCB
|
||||
CDAB
|
||||
DABC
|
||||
BCAD
|
||||
CADB
|
||||
CDBA
|
||||
CBAD
|
||||
ABDC
|
||||
ADBC
|
||||
BDCA
|
||||
DCBA
|
||||
BACD
|
||||
BADC
|
||||
BDAC
|
||||
CBDA
|
||||
DBCA
|
||||
DCAB`, "\n")
|
||||
|
||||
func main() {
|
||||
b := make([]byte, len(given[0]))
|
||||
for i := range b {
|
||||
m := make(map[byte]int)
|
||||
for _, p := range given {
|
||||
m[p[i]]++
|
||||
}
|
||||
for char, count := range m {
|
||||
if count&1 == 1 {
|
||||
b[i] = char
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
fmt.Println(string(b))
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
func main() {
|
||||
b := make([]byte, len(given[0]))
|
||||
for _, p := range given {
|
||||
for i, c := range []byte(p) {
|
||||
b[i] ^= c
|
||||
}
|
||||
}
|
||||
fmt.Println(string(b))
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
def fact = { n -> [1,(1..<(n+1)).inject(1) { prod, i -> prod * i }].max() }
|
||||
def missingPerms
|
||||
missingPerms = {List elts, List perms ->
|
||||
perms.empty ? elts.permutations() : elts.collect { e ->
|
||||
def ePerms = perms.findAll { e == it[0] }.collect { it[1..-1] }
|
||||
ePerms.size() == fact(elts.size() - 1) ? [] \
|
||||
: missingPerms(elts - e, ePerms).collect { [e] + it }
|
||||
}.sum()
|
||||
}
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
def e = 'ABCD' as List
|
||||
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 }
|
||||
|
||||
def mp = missingPerms(e, p)
|
||||
mp.each { println it }
|
||||
|
|
@ -0,0 +1,37 @@
|
|||
import Data.List ((\\), permutations, nub)
|
||||
import Control.Monad (join)
|
||||
|
||||
missingPerm
|
||||
:: Eq a
|
||||
=> [[a]] -> [[a]]
|
||||
missingPerm = (\\) =<< permutations . nub . join
|
||||
|
||||
deficientPermsList :: [String]
|
||||
deficientPermsList =
|
||||
[ "ABCD"
|
||||
, "CABD"
|
||||
, "ACDB"
|
||||
, "DACB"
|
||||
, "BCDA"
|
||||
, "ACBD"
|
||||
, "ADCB"
|
||||
, "CDAB"
|
||||
, "DABC"
|
||||
, "BCAD"
|
||||
, "CADB"
|
||||
, "CDBA"
|
||||
, "CBAD"
|
||||
, "ABDC"
|
||||
, "ADBC"
|
||||
, "BDCA"
|
||||
, "DCBA"
|
||||
, "BACD"
|
||||
, "BADC"
|
||||
, "BDAC"
|
||||
, "CBDA"
|
||||
, "DBCA"
|
||||
, "DCAB"
|
||||
]
|
||||
|
||||
main :: IO ()
|
||||
main = print $ missingPerm deficientPermsList
|
||||
|
|
@ -0,0 +1,37 @@
|
|||
import Data.List (minimumBy, group, sort, transpose)
|
||||
import Data.Ord (comparing)
|
||||
|
||||
missingPerm
|
||||
:: Ord a
|
||||
=> [[a]] -> [a]
|
||||
missingPerm = fmap (head . minimumBy (comparing length) . group . sort) . transpose
|
||||
|
||||
deficientPermsList :: [String]
|
||||
deficientPermsList =
|
||||
[ "ABCD"
|
||||
, "CABD"
|
||||
, "ACDB"
|
||||
, "DACB"
|
||||
, "BCDA"
|
||||
, "ACBD"
|
||||
, "ADCB"
|
||||
, "CDAB"
|
||||
, "DABC"
|
||||
, "BCAD"
|
||||
, "CADB"
|
||||
, "CDBA"
|
||||
, "CBAD"
|
||||
, "ABDC"
|
||||
, "ADBC"
|
||||
, "BDCA"
|
||||
, "DCBA"
|
||||
, "BACD"
|
||||
, "BADC"
|
||||
, "BDAC"
|
||||
, "CBDA"
|
||||
, "DBCA"
|
||||
, "DCAB"
|
||||
]
|
||||
|
||||
main :: IO ()
|
||||
main = print $ missingPerm deficientPermsList
|
||||
|
|
@ -0,0 +1,35 @@
|
|||
import Data.Char (chr, ord)
|
||||
import Data.Bits (xor)
|
||||
|
||||
missingPerm :: [String] -> String
|
||||
missingPerm = fmap chr . foldr (zipWith xor . fmap ord) [0, 0, 0, 0]
|
||||
|
||||
deficientPermsList :: [String]
|
||||
deficientPermsList =
|
||||
[ "ABCD"
|
||||
, "CABD"
|
||||
, "ACDB"
|
||||
, "DACB"
|
||||
, "BCDA"
|
||||
, "ACBD"
|
||||
, "ADCB"
|
||||
, "CDAB"
|
||||
, "DABC"
|
||||
, "BCAD"
|
||||
, "CADB"
|
||||
, "CDBA"
|
||||
, "CBAD"
|
||||
, "ABDC"
|
||||
, "ADBC"
|
||||
, "BDCA"
|
||||
, "DCBA"
|
||||
, "BACD"
|
||||
, "BADC"
|
||||
, "BDAC"
|
||||
, "CBDA"
|
||||
, "DBCA"
|
||||
, "DCAB"
|
||||
]
|
||||
|
||||
main :: IO ()
|
||||
main = putStrLn $ missingPerm deficientPermsList
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
link strings # for permutes
|
||||
|
||||
procedure main()
|
||||
givens := set![ "ABCD", "CABD", "ACDB", "DACB", "BCDA", "ACBD", "ADCB", "CDAB", "DABC", "BCAD", "CADB",
|
||||
"CDBA", "CBAD", "ABDC", "ADBC", "BDCA", "DCBA", "BACD", "BADC", "BDAC", "CBDA", "DBCA", "DCAB"]
|
||||
|
||||
every insert(full := set(), permutes("ABCD")) # generate all permutations
|
||||
givens := full--givens # and difference
|
||||
|
||||
write("The difference is : ")
|
||||
every write(!givens, " ")
|
||||
end
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
every x := permutes("ABCD") do # generate all permutations
|
||||
if member(givens,x) then delete(givens,x) # remove givens as they are generated
|
||||
else insert(givens,x) # add back any not given
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
link strings
|
||||
|
||||
procedure main()
|
||||
givens := set("ABCD", "CABD", "ACDB", "DACB", "BCDA", "ACBD",
|
||||
"ADCB", "CDAB", "DABC", "BCAD", "CADB", "CDBA",
|
||||
"CBAD", "ABDC", "ADBC", "BDCA", "DCBA", "BACD",
|
||||
"BADC", "BDAC", "CBDA", "DBCA", "DCAB")
|
||||
|
||||
every p := permutes("ABCD") do
|
||||
if not member(givens, p) then write(p)
|
||||
|
||||
end
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
permutations=: A.~ i.@!@#
|
||||
missingPerms=: -.~ permutations @ {.
|
||||
|
|
@ -0,0 +1 @@
|
|||
missingPerms=: -.~ (A.~ i.@!@#) @ {.
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
missingPerms=: monad define
|
||||
item=. {. y
|
||||
y -.~ item A.~ i.! #item
|
||||
)
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
data -.~ 'ABCD' A.~ i.!4
|
||||
DBAC
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
((i.!4) A. 'ABCD') -. data
|
||||
DBAC
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
'ABCD'{~,I.@(= <./)@(#/.~)@('ABCD' , ])"1 |:perms
|
||||
DBAC
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
,(~.#~2|(#/.~))"1|:data
|
||||
DBAC
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
({.data){~|(->./)+/({.i.])data
|
||||
DBAC
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
import java.util.ArrayList;
|
||||
|
||||
import com.google.common.base.Joiner;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import com.google.common.collect.Lists;
|
||||
|
||||
public class FindMissingPermutation {
|
||||
public static void main(String[] args) {
|
||||
Joiner joiner = Joiner.on("").skipNulls();
|
||||
ImmutableSet<String> s = ImmutableSet.of("ABCD", "CABD", "ACDB",
|
||||
"DACB", "BCDA", "ACBD", "ADCB", "CDAB", "DABC", "BCAD", "CADB",
|
||||
"CDBA", "CBAD", "ABDC", "ADBC", "BDCA", "DCBA", "BACD", "BADC",
|
||||
"BDAC", "CBDA", "DBCA", "DCAB");
|
||||
|
||||
for (ArrayList<Character> cs : Utils.Permutations(Lists.newArrayList(
|
||||
'A', 'B', 'C', 'D')))
|
||||
if (!s.contains(joiner.join(cs)))
|
||||
System.out.println(joiner.join(cs));
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
public class FindMissingPermutation
|
||||
{
|
||||
public static void main(String[] args)
|
||||
{
|
||||
String[] givenPermutations = { "ABCD", "CABD", "ACDB", "DACB", "BCDA", "ACBD",
|
||||
"ADCB", "CDAB", "DABC", "BCAD", "CADB", "CDBA",
|
||||
"CBAD", "ABDC", "ADBC", "BDCA", "DCBA", "BACD",
|
||||
"BADC", "BDAC", "CBDA", "DBCA", "DCAB" };
|
||||
String characterSet = givenPermutations[0];
|
||||
// Compute n! * (n - 1) / 2
|
||||
int maxCode = characterSet.length() - 1;
|
||||
for (int i = characterSet.length(); i >= 3; i--)
|
||||
maxCode *= i;
|
||||
StringBuilder missingPermutation = new StringBuilder();
|
||||
for (int i = 0; i < characterSet.length(); i++)
|
||||
{
|
||||
int code = 0;
|
||||
for (String permutation : givenPermutations)
|
||||
code += characterSet.indexOf(permutation.charAt(i));
|
||||
missingPermutation.append(characterSet.charAt(maxCode - code));
|
||||
}
|
||||
System.out.println("Missing permutation: " + missingPermutation.toString());
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
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(x = [new Array(l), new Array(l), new Array(l), new Array(l)], j = q, k = l + 1, i = -1;
|
||||
++i < l; x[2][i] = i, x[1][i] = x[0][i] = j /= --k);
|
||||
for(r = new Array(q); ++p < q;)
|
||||
for(r[p] = new Array(l), i = -1; ++i < l; !--x[1][i] && (x[1][i] = x[0][i],
|
||||
x[2][i] = (x[2][i] + 1) % l), r[p][i] = m ? x[3][i] : v[x[3][i]])
|
||||
for(x[3][i] = x[2][i], f = 0; !f; f = !f)
|
||||
for(j = i; j; x[3][--j] == x[2][i] && (x[3][i] = x[2][i] = (x[2][i] + 1) % l, f = 1));
|
||||
return r;
|
||||
};
|
||||
|
||||
list = [ 'ABCD', 'CABD', 'ACDB', 'DACB', 'BCDA', 'ACBD', 'ADCB', 'CDAB',
|
||||
'DABC', 'BCAD', 'CADB', 'CDBA', 'CBAD', 'ABDC', 'ADBC', 'BDCA',
|
||||
'DCBA', 'BACD', 'BADC', 'BDAC', 'CBDA', 'DBCA', 'DCAB'];
|
||||
|
||||
all = permute(list[0].split('')).map(function(elem) {return elem.join('')});
|
||||
|
||||
missing = all.filter(function(elem) {return list.indexOf(elem) == -1});
|
||||
print(missing); // ==> DBAC
|
||||
|
|
@ -0,0 +1,40 @@
|
|||
(function (strList) {
|
||||
|
||||
// [a] -> [[a]]
|
||||
function permutations(xs) {
|
||||
return xs.length ? (
|
||||
chain(xs, function (x) {
|
||||
return chain(permutations(deleted(x, xs)), function (ys) {
|
||||
return [[x].concat(ys).join('')];
|
||||
})
|
||||
})) : [[]];
|
||||
}
|
||||
|
||||
// Monadic bind/chain for lists
|
||||
// [a] -> (a -> b) -> [b]
|
||||
function chain(xs, f) {
|
||||
return [].concat.apply([], xs.map(f));
|
||||
}
|
||||
|
||||
// a -> [a] -> [a]
|
||||
function deleted(x, xs) {
|
||||
return xs.length ? (
|
||||
x === xs[0] ? xs.slice(1) : [xs[0]].concat(
|
||||
deleted(x, xs.slice(1))
|
||||
)
|
||||
) : [];
|
||||
}
|
||||
|
||||
// Provided subset
|
||||
var lstSubSet = strList.split('\n');
|
||||
|
||||
// Any missing permutations
|
||||
// (we can use fold/reduce, filter, or chain (concat map) here)
|
||||
return chain(permutations('ABCD'.split('')), function (x) {
|
||||
return lstSubSet.indexOf(x) === -1 ? [x] : [];
|
||||
});
|
||||
|
||||
})(
|
||||
'ABCD\nCABD\nACDB\nDACB\nBCDA\nACBD\nADCB\nCDAB\nDABC\nBCAD\nCADB\n\
|
||||
CDBA\nCBAD\nABDC\nADBC\nBDCA\nDCBA\nBACD\nBADC\nBDAC\nCBDA\nDBCA\nDCAB'
|
||||
);
|
||||
|
|
@ -0,0 +1 @@
|
|||
["DBAC"]
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
(() => {
|
||||
'use strict';
|
||||
|
||||
// transpose :: [[a]] -> [[a]]
|
||||
let transpose = xs =>
|
||||
xs[0].map((_, iCol) => xs
|
||||
.map((row) => row[iCol]));
|
||||
|
||||
|
||||
let xs = 'ABCD CABD ACDB DACB BCDA ACBD ADCB CDAB' +
|
||||
' DABC BCAD CADB CDBA CBAD ABDC ADBC BDCA DCBA' +
|
||||
' BACD BADC BDAC CBDA DBCA DCAB'
|
||||
|
||||
return transpose(xs.split(' ')
|
||||
.map(x => x.split('')))
|
||||
.map(col => col.reduce((a, x) => ( // count of each character in each column
|
||||
a[x] = (a[x] || 0) + 1,
|
||||
a
|
||||
), {}))
|
||||
.map(dct => { // character with frequency below mean of distribution ?
|
||||
let ks = Object.keys(dct),
|
||||
xs = ks.map(k => dct[k]),
|
||||
mean = xs.reduce((a, b) => a + b, 0) / xs.length;
|
||||
|
||||
return ks.reduce(
|
||||
(a, k) => a ? a : (dct[k] < mean ? k : undefined),
|
||||
undefined
|
||||
);
|
||||
})
|
||||
.join(''); // 4 chars as single string
|
||||
|
||||
// --> 'DBAC'
|
||||
})();
|
||||
|
|
@ -0,0 +1,96 @@
|
|||
(() => {
|
||||
'use strict';
|
||||
|
||||
// MISSING PERMUTATION ---------------------------------------------------
|
||||
|
||||
// missingPermutation :: [String] -> String
|
||||
const missingPermutation = xs =>
|
||||
map(
|
||||
// Rarest letter,
|
||||
compose([
|
||||
sort,
|
||||
group,
|
||||
curry(minimumBy)(comparing(length)),
|
||||
head
|
||||
]),
|
||||
|
||||
// in each column.
|
||||
transpose(map(stringChars, xs))
|
||||
)
|
||||
.join('');
|
||||
|
||||
|
||||
// GENERIC FUNCTIONAL PRIMITIVES -----------------------------------------
|
||||
|
||||
// transpose :: [[a]] -> [[a]]
|
||||
const transpose = xs =>
|
||||
xs[0].map((_, iCol) => xs.map(row => row[iCol]));
|
||||
|
||||
// sort :: Ord a => [a] -> [a]
|
||||
const sort = xs => xs.sort();
|
||||
|
||||
// group :: Eq a => [a] -> [[a]]
|
||||
const group = xs => groupBy((a, b) => a === b, xs);
|
||||
|
||||
// groupBy :: (a -> a -> Bool) -> [a] -> [[a]]
|
||||
const groupBy = (f, xs) => {
|
||||
const dct = xs.slice(1)
|
||||
.reduce((a, x) => {
|
||||
const
|
||||
h = a.active.length > 0 ? a.active[0] : undefined,
|
||||
blnGroup = h !== undefined && f(h, x);
|
||||
|
||||
return {
|
||||
active: blnGroup ? a.active.concat(x) : [x],
|
||||
sofar: blnGroup ? a.sofar : a.sofar.concat([a.active])
|
||||
};
|
||||
}, {
|
||||
active: xs.length > 0 ? [xs[0]] : [],
|
||||
sofar: []
|
||||
});
|
||||
return dct.sofar.concat(dct.active.length > 0 ? [dct.active] : []);
|
||||
};
|
||||
|
||||
// length :: [a] -> Int
|
||||
const length = xs => xs.length;
|
||||
|
||||
// comparing :: (a -> b) -> (a -> a -> Ordering)
|
||||
const comparing = f =>
|
||||
(x, y) => {
|
||||
const
|
||||
a = f(x),
|
||||
b = f(y);
|
||||
return a < b ? -1 : a > b ? 1 : 0
|
||||
};
|
||||
|
||||
// minimumBy :: (a -> a -> Ordering) -> [a] -> a
|
||||
const minimumBy = (f, xs) =>
|
||||
xs.reduce((a, x) => a === undefined ? x : (
|
||||
f(x, a) < 0 ? x : a
|
||||
), undefined);
|
||||
|
||||
// head :: [a] -> a
|
||||
const head = xs => xs.length ? xs[0] : undefined;
|
||||
|
||||
// map :: (a -> b) -> [a] -> [b]
|
||||
const map = (f, xs) => xs.map(f)
|
||||
|
||||
// compose :: [(a -> a)] -> (a -> a)
|
||||
const compose = fs => x => fs.reduce((a, f) => f(a), x);
|
||||
|
||||
// curry :: ((a, b) -> c) -> a -> b -> c
|
||||
const curry = f => a => b => f(a, b);
|
||||
|
||||
// stringChars :: String -> [Char]
|
||||
const stringChars = s => s.split('');
|
||||
|
||||
|
||||
// TEST ------------------------------------------------------------------
|
||||
|
||||
return missingPermutation(["ABCD", "CABD", "ACDB", "DACB", "BCDA", "ACBD",
|
||||
"ADCB", "CDAB", "DABC", "BCAD", "CADB", "CDBA", "CBAD", "ABDC", "ADBC",
|
||||
"BDCA", "DCBA", "BACD", "BADC", "BDAC", "CBDA", "DBCA", "DCAB"
|
||||
]);
|
||||
|
||||
// -> "DBAC"
|
||||
})();
|
||||
|
|
@ -0,0 +1,39 @@
|
|||
(() => {
|
||||
'use strict';
|
||||
|
||||
// main :: IO ()
|
||||
const main = () => {
|
||||
const xs = [
|
||||
'ABCD', 'CABD', 'ACDB', 'DACB', 'BCDA', 'ACBD',
|
||||
'ADCB', 'CDAB', 'DABC', 'BCAD', 'CADB', 'CDBA',
|
||||
'CBAD', 'ABDC', 'ADBC', 'BDCA', 'DCBA', 'BACD',
|
||||
'BADC', 'BDAC', 'CBDA', 'DBCA', 'DCAB'
|
||||
];
|
||||
|
||||
return xs.reduce(
|
||||
(a, x) => zipWith(xor)(a)(codes(x)),
|
||||
[0, 0, 0, 0]
|
||||
).map(x => String.fromCodePoint(x)).join('')
|
||||
};
|
||||
|
||||
// ---------------------- GENERIC ----------------------
|
||||
|
||||
// codes :: String -> [Int]
|
||||
const codes = s =>
|
||||
s.split('').map(c => c.codePointAt(0));
|
||||
|
||||
// xor :: Int -> Int -> Int
|
||||
const xor = a =>
|
||||
b => (a ^ b)
|
||||
|
||||
// zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
|
||||
const zipWith = f =>
|
||||
// A list constructed by zipping with a
|
||||
// custom function, rather than with the
|
||||
// default tuple constructor.
|
||||
xs => ys => xs.slice(0).map(
|
||||
(x, i) => f(x)(ys[i])
|
||||
);
|
||||
|
||||
return main()
|
||||
})();
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
def transpose:
|
||||
if (.[0] | length) == 0 then []
|
||||
else [map(.[0])] + (map(.[1:]) | transpose)
|
||||
end ;
|
||||
|
||||
# Input: an array of integers (based on the encoding of A=0, B=1, etc)
|
||||
# corresponding to the occurrences in any one position of the
|
||||
# letters in the list of permutations.
|
||||
# Output: a tally in the form of an array recording in position i the
|
||||
# parity of the number of occurrences of the letter corresponding to i.
|
||||
# Example: given [0,1,0,1,2], the array of counts of 0, 1, and 2 is [2, 2, 1],
|
||||
# and thus the final result is [0, 0, 1].
|
||||
def parities:
|
||||
reduce .[] as $x ( []; .[$x] = (1 + .[$x]) % 2);
|
||||
|
||||
# Input: an array of parity-counts, e.g. [0, 1, 0, 0]
|
||||
# Output: the corresponding letter, e.g. "B".
|
||||
def decode:
|
||||
[index(1) + 65] | implode;
|
||||
|
||||
# encode a string (e.g. "ABCD") as an array (e.g. [0,1,2,3]):
|
||||
def encode_string: [explode[] - 65];
|
||||
|
|
@ -0,0 +1 @@
|
|||
map(encode_string) | transpose | map(parities | decode) | join("")
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
$ jq -R . Find_the_missing_permutation.txt | jq -s -f Find_the_missing_permutation.jq
|
||||
"DBAC"
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
using BenchmarkTools, Combinatorics
|
||||
|
||||
function missingperm(arr::Vector)
|
||||
allperms = String.(permutations(arr[1])) # revised for type safety
|
||||
for perm in allperms
|
||||
if perm ∉ arr return perm end
|
||||
end
|
||||
end
|
||||
|
||||
arr = ["ABCD", "CABD", "ACDB", "DACB", "BCDA", "ACBD", "ADCB", "CDAB", "DABC", "BCAD",
|
||||
"CADB", "CDBA", "CBAD", "ABDC", "ADBC", "BDCA", "DCBA", "BACD", "BADC", "BDAC",
|
||||
"CBDA", "DBCA", "DCAB"]
|
||||
@show missingperm(arr)
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
function missingperm1(arr::Vector{<:AbstractString})
|
||||
missperm = string()
|
||||
for pos in 1:length(arr[1])
|
||||
s = Set()
|
||||
for perm in arr
|
||||
c = perm[pos]
|
||||
if c ∈ s pop!(s, c) else push!(s, c) end
|
||||
end
|
||||
missperm *= first(s)
|
||||
end
|
||||
return missperm
|
||||
end
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
function missingperm2(arr::Vector)
|
||||
len = length(arr[1])
|
||||
xorval = zeros(UInt8, len)
|
||||
for perm in [Vector{UInt8}(s) for s in arr], i in 1:len
|
||||
xorval[i] ⊻= perm[i]
|
||||
end
|
||||
return String(xorval)
|
||||
end
|
||||
|
||||
@show missingperm(arr)
|
||||
@show missingperm1(arr)
|
||||
@show missingperm2(arr)
|
||||
|
||||
@btime missingperm(arr)
|
||||
@btime missingperm1(arr)
|
||||
@btime missingperm2(arr)
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
split:{1_'(&x=y)_ x:y,x}
|
||||
|
||||
g: ("ABCD CABD ACDB DACB BCDA ACBD ADCB CDAB DABC BCAD CADB")
|
||||
g,:(" CDBA CBAD ABDC ADBC BDCA DCBA BACD BADC BDAC CBDA DBCA DCAB")
|
||||
p: split[g;" "];
|
||||
|
||||
/ All permutations of "ABCD"
|
||||
perm:{:[1<x;,/(>:'(x,x)#1,x#0)[;0,'1+_f x-1];,!x]}
|
||||
p2:a@(perm(#a:"ABCD"));
|
||||
|
||||
/ Which permutations in p are there in p2?
|
||||
p2 _lin p
|
||||
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1
|
||||
|
||||
/ Invert the result
|
||||
~p2 _lin p
|
||||
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0
|
||||
|
||||
/ It's the 20th permutation that is missing
|
||||
&~p2 _lin p
|
||||
,20
|
||||
|
||||
p2@&~p2 _lin p
|
||||
"DBAC"
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
table:{b@<b:(x@*:'a),'#:'a:=x}
|
||||
,/"ABCD"@&:'{5=(table p[;x])[;1]}'!4
|
||||
"DBAC"
|
||||
|
|
@ -0,0 +1 @@
|
|||
,/p2@&~(p2:{x@m@&n=(#?:)'m:!n#n:#x}[*p]) _lin p
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
// version 1.1.2
|
||||
|
||||
fun <T> permute(input: List<T>): List<List<T>> {
|
||||
if (input.size == 1) return listOf(input)
|
||||
val perms = mutableListOf<List<T>>()
|
||||
val toInsert = input[0]
|
||||
for (perm in permute(input.drop(1))) {
|
||||
for (i in 0..perm.size) {
|
||||
val newPerm = perm.toMutableList()
|
||||
newPerm.add(i, toInsert)
|
||||
perms.add(newPerm)
|
||||
}
|
||||
}
|
||||
return perms
|
||||
}
|
||||
|
||||
fun <T> missingPerms(input: List<T>, perms: List<List<T>>) = permute(input) - perms
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val input = listOf('A', 'B', 'C', 'D')
|
||||
val strings = listOf(
|
||||
"ABCD", "CABD", "ACDB", "DACB", "BCDA", "ACBD", "ADCB", "CDAB",
|
||||
"DABC", "BCAD", "CADB", "CDBA", "CBAD", "ABDC", "ADBC", "BDCA",
|
||||
"DCBA", "BACD", "BADC", "BDAC", "CBDA", "DBCA", "DCAB"
|
||||
)
|
||||
val perms = strings.map { it.toList() }
|
||||
val missing = missingPerms(input, perms)
|
||||
if (missing.size == 1)
|
||||
print("The missing permutation is ${missing[0].joinToString("")}")
|
||||
else {
|
||||
println("There are ${missing.size} missing permutations, namely:\n")
|
||||
for (perm in missing) println(perm.joinToString(""))
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
local permute, tablex = require("pl.permute"), require("pl.tablex")
|
||||
local permList, pStr = {
|
||||
"ABCD", "CABD", "ACDB", "DACB", "BCDA", "ACBD", "ADCB", "CDAB",
|
||||
"DABC", "BCAD", "CADB", "CDBA", "CBAD", "ABDC", "ADBC", "BDCA",
|
||||
"DCBA", "BACD", "BADC", "BDAC", "CBDA", "DBCA", "DCAB"
|
||||
}
|
||||
for perm in permute.iter({"A","B","C","D"}) do
|
||||
pStr = table.concat(perm)
|
||||
if not tablex.find(permList, pStr) then print(pStr) end
|
||||
end
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
function perm = findMissingPerms(list)
|
||||
|
||||
permsList = perms(list(1,:)); %Generate all permutations of the 4 letters
|
||||
perm = []; %This is the functions return value if the list is not missing a permutation
|
||||
|
||||
%Normally the rest of this would be vectorized, but because this is
|
||||
%done on a vector of strings, the vectorized functions will only access
|
||||
%one character at a time. So, in order for this to work we have to use
|
||||
%loops.
|
||||
for i = (1:size(permsList,1))
|
||||
|
||||
found = false;
|
||||
|
||||
for j = (1:size(list,1))
|
||||
if (permsList(i,:) == list(j,:))
|
||||
found = true;
|
||||
break
|
||||
end
|
||||
end
|
||||
|
||||
if not(found)
|
||||
perm = permsList(i,:);
|
||||
return
|
||||
end
|
||||
|
||||
end %for
|
||||
end %fingMissingPerms
|
||||
|
|
@ -0,0 +1,55 @@
|
|||
>> 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
|
||||
|
||||
>> findMissingPerms(list)
|
||||
|
||||
ans =
|
||||
|
||||
DBAC
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
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():
|
||||
for letter in "ABCD" do
|
||||
perm[letter] := 0:
|
||||
end do:
|
||||
for item in lst do
|
||||
for letter in "ABCD" do
|
||||
perm[letter] += StringTools:-FirstFromLeft(letter, item):
|
||||
end do:
|
||||
end do:
|
||||
print(StringTools:-Join(ListTools:-Flatten([indices(perm)], 4)[sort(map(x->60-x, ListTools:-Flatten([entries(perm)],4)),'output=permutation')], "")):
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
ProvidedSet = {"ABCD" , "CABD" , "ACDB" , "DACB" , "BCDA" , "ACBD",
|
||||
"ADCB" , "CDAB", "DABC", "BCAD" , "CADB", "CDBA" , "CBAD" , "ABDC",
|
||||
"ADBC" , "BDCA", "DCBA" , "BACD", "BADC", "BDAC" , "CBDA", "DBCA", "DCAB"};
|
||||
|
||||
Complement[StringJoin /@ Permutations@Characters@First@#, #] &@ProvidedSet
|
||||
|
||||
|
||||
->{"DBAC"}
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
import strutils
|
||||
|
||||
proc missingPermutation(arr: openArray[string]): string =
|
||||
result = ""
|
||||
if arr.len == 0: return
|
||||
if arr.len == 1: return arr[0][1] & arr[0][0]
|
||||
|
||||
for pos in 0 ..< arr[0].len:
|
||||
var s: set[char] = {}
|
||||
for permutation in arr:
|
||||
let c = permutation[pos]
|
||||
if c in s: s.excl c
|
||||
else: s.incl c
|
||||
for c in s: result.add c
|
||||
|
||||
const given = """ABCD CABD ACDB DACB BCDA ACBD ADCB CDAB DABC BCAD CADB CDBA
|
||||
CBAD ABDC ADBC BDCA DCBA BACD BADC BDAC CBDA DBCA DCAB""".splitWhiteSpace()
|
||||
|
||||
echo missingPermutation(given)
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
(* insert x at all positions into li and return the list of results *)
|
||||
let rec insert x = function
|
||||
| [] -> [[x]]
|
||||
| a::m as li -> (x::li) :: (List.map (fun y -> a::y) (insert x m))
|
||||
|
||||
(* list of all permutations of li *)
|
||||
let permutations li =
|
||||
List.fold_right (fun a z -> List.concat (List.map (insert a) z)) li [[]]
|
||||
|
||||
(* convert a string to a char list *)
|
||||
let chars_of_string s =
|
||||
let cl = ref [] in
|
||||
String.iter (fun c -> cl := c :: !cl) s;
|
||||
(List.rev !cl)
|
||||
|
||||
(* convert a char list to a string *)
|
||||
let string_of_chars cl =
|
||||
String.concat "" (List.map (String.make 1) cl)
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
let deficient_perms = [
|
||||
"ABCD";"CABD";"ACDB";"DACB";
|
||||
"BCDA";"ACBD";"ADCB";"CDAB";
|
||||
"DABC";"BCAD";"CADB";"CDBA";
|
||||
"CBAD";"ABDC";"ADBC";"BDCA";
|
||||
"DCBA";"BACD";"BADC";"BDAC";
|
||||
"CBDA";"DBCA";"DCAB";
|
||||
]
|
||||
|
||||
let it = chars_of_string (List.hd deficient_perms)
|
||||
|
||||
let perms = List.map string_of_chars (permutations it)
|
||||
|
||||
let results = List.filter (fun v -> not(List.mem v deficient_perms)) perms
|
||||
|
||||
let () = List.iter print_endline results
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
let array_of_perm s =
|
||||
let n = String.length s in
|
||||
Array.init n (fun i -> int_of_char s.[i] - 65);;
|
||||
|
||||
let perm_of_array a =
|
||||
let n = Array.length a in
|
||||
let s = String.create n in
|
||||
Array.iteri (fun i x ->
|
||||
s.[i] <- char_of_int (x + 65)
|
||||
) a;
|
||||
s;;
|
||||
|
||||
let find_missing v =
|
||||
let n = String.length (List.hd v) in
|
||||
let a = Array.make_matrix n n 0
|
||||
and r = ref v in
|
||||
List.iter (fun s ->
|
||||
let u = array_of_perm s in
|
||||
Array.iteri (fun i x -> x.(u.(i)) <- x.(u.(i)) + 1) a
|
||||
) v;
|
||||
let q = Array.make n 0 in
|
||||
Array.iteri (fun i x ->
|
||||
Array.iteri (fun j y ->
|
||||
if y mod 2 != 0 then q.(i) <- j
|
||||
) x
|
||||
) a;
|
||||
perm_of_array q;;
|
||||
|
||||
find_missing deficient_perms;;
|
||||
(* - : string = "DBAC" *)
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
given = [ 'ABCD';'CABD';'ACDB';'DACB'; ...
|
||||
'BCDA';'ACBD';'ADCB';'CDAB'; ...
|
||||
'DABC';'BCAD';'CADB';'CDBA'; ...
|
||||
'CBAD';'ABDC';'ADBC';'BDCA'; ...
|
||||
'DCBA';'BACD';'BADC';'BDAC'; ...
|
||||
'CBDA';'DBCA';'DCAB' ];
|
||||
val = 4.^(3:-1:0)';
|
||||
there = 1+(toascii(given)-toascii('A'))*val;
|
||||
every = 1+perms(0:3)*val;
|
||||
|
||||
bits = zeros(max(every),1);
|
||||
bits(every) = 1;
|
||||
bits(there) = 0;
|
||||
missing = dec2base(find(bits)-1,'ABCD')
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
declare
|
||||
GivenPermutations =
|
||||
["ABCD" "CABD" "ACDB" "DACB" "BCDA" "ACBD" "ADCB" "CDAB" "DABC" "BCAD" "CADB" "CDBA"
|
||||
"CBAD" "ABDC" "ADBC" "BDCA" "DCBA" "BACD" "BADC" "BDAC" "CBDA" "DBCA" "DCAB"]
|
||||
|
||||
%% four distinct variables between "A" and "D":
|
||||
proc {Description Root}
|
||||
Root = {FD.list 4 &A#&D}
|
||||
{FD.distinct Root}
|
||||
{FD.distribute naiv Root}
|
||||
end
|
||||
|
||||
AllPermutations = {SearchAll Description}
|
||||
in
|
||||
for P in AllPermutations do
|
||||
if {Not {Member P GivenPermutations}} then
|
||||
{System.showInfo "Missing: "#P}
|
||||
end
|
||||
end
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
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);
|
||||
t=numtoperm(4, binomial(4!,2)-sum(i=1,#v,v[i]));
|
||||
Strchr(apply(n->n+64,t))
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
<?php
|
||||
$finalres = Array();
|
||||
function permut($arr,$result=array()){
|
||||
global $finalres;
|
||||
if(empty($arr)){
|
||||
$finalres[] = implode("",$result);
|
||||
}else{
|
||||
foreach($arr as $key => $val){
|
||||
$newArr = $arr;
|
||||
$newres = $result;
|
||||
$newres[] = $val;
|
||||
unset($newArr[$key]);
|
||||
permut($newArr,$newres);
|
||||
}
|
||||
}
|
||||
}
|
||||
$givenPerms = Array("ABCD","CABD","ACDB","DACB","BCDA","ACBD","ADCB","CDAB","DABC","BCAD","CADB","CDBA","CBAD","ABDC","ADBC","BDCA","DCBA","BACD","BADC","BDAC","CBDA","DBCA","DCAB");
|
||||
$given = Array("A","B","C","D");
|
||||
permut($given);
|
||||
print_r(array_diff($finalres,$givenPerms)); // Array ( [20] => DBAC )
|
||||
|
|
@ -0,0 +1,63 @@
|
|||
program MissPerm;
|
||||
{$MODE DELPHI} //for result
|
||||
|
||||
const
|
||||
maxcol = 4;
|
||||
type
|
||||
tmissPerm = 1..23;
|
||||
tcol = 1..maxcol;
|
||||
tResString = String[maxcol];
|
||||
const
|
||||
Given_Permutations : array [tmissPerm] of tResString =
|
||||
('ABCD', 'CABD', 'ACDB', 'DACB', 'BCDA', 'ACBD',
|
||||
'ADCB', 'CDAB', 'DABC', 'BCAD', 'CADB', 'CDBA',
|
||||
'CBAD', 'ABDC', 'ADBC', 'BDCA', 'DCBA', 'BACD',
|
||||
'BADC', 'BDAC', 'CBDA', 'DBCA', 'DCAB');
|
||||
chOfs = Ord('A')-1;
|
||||
var
|
||||
SumElemCol: array[tcol,tcol] of NativeInt;
|
||||
function fib(n: NativeUint): NativeUint;
|
||||
var
|
||||
i : NativeUint;
|
||||
Begin
|
||||
result := 1;
|
||||
For i := 2 to n do
|
||||
result:= result*i;
|
||||
end;
|
||||
|
||||
function CountOccurences: tresString;
|
||||
//count the number of every letter in every column
|
||||
//should be (colmax-1)! => 6
|
||||
//the missing should count (colmax-1)! -1 => 5
|
||||
var
|
||||
fibN_1 : NativeUint;
|
||||
row, col: NativeInt;
|
||||
Begin
|
||||
For row := low(tmissPerm) to High(tmissPerm) do
|
||||
For col := low(tcol) to High(tcol) do
|
||||
inc(SumElemCol[col,ORD(Given_Permutations[row,col])-chOfs]);
|
||||
|
||||
//search the missing
|
||||
fibN_1 := fib(maxcol-1)-1;
|
||||
setlength(result,maxcol);
|
||||
For col := low(tcol) to High(tcol) do
|
||||
For row := low(tcol) to High(tcol) do
|
||||
IF SumElemCol[col,row]=fibN_1 then
|
||||
result[col]:= ansichar(row+chOfs);
|
||||
end;
|
||||
|
||||
function CheckXOR: tresString;
|
||||
var
|
||||
row,col: NativeUint;
|
||||
Begin
|
||||
setlength(result,maxcol);
|
||||
fillchar(result[1],maxcol,#0);
|
||||
For row := low(tmissPerm) to High(tmissPerm) do
|
||||
For col := low(tcol) to High(tcol) do
|
||||
result[col] := ansichar(ord(result[col]) XOR ord(Given_Permutations[row,col]));
|
||||
end;
|
||||
|
||||
Begin
|
||||
writeln(CountOccurences,' is missing');
|
||||
writeln(CheckXOR,' is missing');
|
||||
end.
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
sub check_perm {
|
||||
my %hash; @hash{@_} = ();
|
||||
for my $s (@_) { exists $hash{$_} or return $_
|
||||
for map substr($s,1) . substr($s,0,1), (1..length $s); }
|
||||
}
|
||||
|
||||
# Check and display
|
||||
@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);
|
||||
print check_perm(@perms), "\n";
|
||||
|
|
@ -0,0 +1 @@
|
|||
print eval join '^', map "'$_'", <>;
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
$\ ^= $_ while <>;
|
||||
print '';
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
local $_ = join '', <>;
|
||||
my %h = map { $_, '' } reverse =~ /\w+/g;
|
||||
delete @h{ /\w+/g };
|
||||
print %h, "\n";
|
||||
|
|
@ -0,0 +1,50 @@
|
|||
(phixonline)-->
|
||||
<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: #008000;">"DABC"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"BCAD"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"CADB"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"CDBA"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"CBAD"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"ABDC"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"ADBC"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"BDCA"</span><span style="color: #0000FF;">,</span>
|
||||
<span style="color: #008000;">"DCBA"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"BACD"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"BADC"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"BDAC"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"CBDA"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"DBCA"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"DCAB"</span><span style="color: #0000FF;">}</span>
|
||||
|
||||
<span style="color: #000080;font-style:italic;">-- 1: sum of letters</span>
|
||||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">r</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">perms</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #000000;">r</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sq_add</span><span style="color: #0000FF;">(</span><span style="color: #000000;">r</span><span style="color: #0000FF;">,</span><span style="color: #000000;">perms</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">])</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<span style="color: #000000;">r</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sq_sub</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">max</span><span style="color: #0000FF;">(</span><span style="color: #000000;">r</span><span style="color: #0000FF;">)+</span><span style="color: #008000;">'A'</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;">-- based on the notion that missing = sum(full)-sum(partial) would be true,
|
||||
-- and that sum(full) would be like {M,M,M,M} rather than a mix of numbers.
|
||||
-- the final step is equivalent to eg {1528,1530,1531,1529}
|
||||
-- max-r[i] -> { 3, 1, 0, 2}
|
||||
-- to chars -> { D, B, A, C}
|
||||
-- (but obviously both done in one line)
|
||||
|
||||
-- 2: the xor trick</span>
|
||||
<span style="color: #000000;">r</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">perms</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #000000;">r</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sq_xor_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">r</span><span style="color: #0000FF;">,</span><span style="color: #000000;">perms</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">])</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</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 the missing chars being present an odd number of times, non-missing chars an even number of times)
|
||||
|
||||
-- 3: find least frequent letters</span>
|
||||
<span style="color: #000000;">r</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">" "</span>
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">r</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">count</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">perms</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #004080;">integer</span> <span style="color: #000000;">cdx</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">perms</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">][</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]-</span><span style="color: #008000;">'A'</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span>
|
||||
<span style="color: #000000;">count</span><span style="color: #0000FF;">[</span><span style="color: #000000;">cdx</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<span style="color: #000000;">r</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">smallest</span><span style="color: #0000FF;">(</span><span style="color: #000000;">count</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)+</span><span style="color: #008000;">'A'</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</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 the assumption that a full set would have each letter occurring the same number of times in each position)
|
||||
-- (smallest(count,1) returns the index position of the smallest, rather than it's value)
|
||||
|
||||
-- 4: test all permutations</span>
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">factorial</span><span style="color: #0000FF;">(</span><span style="color: #000000;">4</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #000000;">r</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">permute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"ABCD"</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #008080;">not</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #000000;">r</span><span style="color: #0000FF;">,</span><span style="color: #000000;">perms</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span> <span style="color: #008080;">exit</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</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 >1 omission)</span>
|
||||
<!--
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
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"),
|
||||
% ...
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
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],
|
||||
Missing3 = new_list(4), Missing3 :: 1..4,
|
||||
all_different(Missing3),
|
||||
table_notin({Missing3[1],Missing3[2],Missing3[3],Missing3[4]},P1Table),
|
||||
solve(Missing3),
|
||||
ABCD2 = "ABCD",
|
||||
println(missing11=[ABCD2[I] : I in Missing3]).
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
% ...
|
||||
PermsLen = Perms.length,
|
||||
P1Len = P1.length,
|
||||
A2 = new_array(PermsLen,P1Len), bind_vars(A2,0),
|
||||
foreach(I in 1..PermsLen, J in 1..P1Len, Perms[I] = P1[J])
|
||||
A2[I,J] := 1
|
||||
end,
|
||||
println(missing12=[Perms[I] : I in 1..PermsLen, sum([A2[I,J] : J in 1..P1Len])=0]).
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
% ...
|
||||
println(missing13=to_fstring("%X",reduce(^,[parse_term("0x"++P):P in P1]))).
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
% ...
|
||||
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,
|
||||
println(missing16=Perms2),
|
||||
|
||||
nl.
|
||||
|
||||
% sort a map according to values
|
||||
sort2(Map) = [K=V:_=(K=V) in sort([V=(K=V): K=V in Map])]
|
||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue