Just another update
This commit is contained in:
parent
a25938f123
commit
00a190b0a6
6591 changed files with 94363 additions and 23227 deletions
|
|
@ -1,8 +1,16 @@
|
|||
These are all of the permutations of the symbols A, B, C and D, except for one that's not listed. Find that missing permutation.
|
||||
These are all of the permutations of the symbols A, B, C and D,
|
||||
except for one that's not listed.
|
||||
Find that missing permutation.
|
||||
|
||||
(cf. [[Permutations]])
|
||||
|
||||
There is an obvious method : enumerating all permutations of A, B, C, D, and looking for the missing one. There is an alternate method. Hint : if all permutations were here, how many times would A appear in each position ? What is the parity of this number ?
|
||||
There is an obvious method :
|
||||
enumerating all permutations of A, B, C, D, and looking for the missing one.
|
||||
|
||||
There is an alternate method.
|
||||
Hint : if all permutations were here,
|
||||
how many times would A appear in each position ?
|
||||
What is the parity of this number ?
|
||||
|
||||
<pre>ABCD
|
||||
CABD
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
@ -1,35 +1,37 @@
|
|||
void main() {
|
||||
import std.stdio, std.string, std.algorithm, std.range, std.conv;
|
||||
|
||||
const 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;
|
||||
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.
|
||||
const permsSet = perms.zip(0.repeat).assocArray;
|
||||
auto perm = cast(ubyte[])perms[0].dup;
|
||||
immutable permsSet = perms
|
||||
.map!representation
|
||||
.zip(true.repeat)
|
||||
.assocArray;
|
||||
auto perm = perms[0].dup.representation;
|
||||
do {
|
||||
if (cast(char[])perm !in permsSet)
|
||||
writeln(cast(char[])perm);
|
||||
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 Perl 6 (via Go).
|
||||
enum int len = 4;
|
||||
// Version 2: xor all the ASCII values, the uneven one
|
||||
// gets flushed out. Based on Perl 6 (via Go).
|
||||
enum len = 4;
|
||||
char[len] b = 0;
|
||||
foreach (immutable p; perms)
|
||||
b[] ^= p[];
|
||||
b.writeln;
|
||||
|
||||
// Version 3 : Sum ASCII values.
|
||||
// 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: some sort of checksum, Java translation.
|
||||
// maxCode will be 36.
|
||||
// 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) {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,12 @@
|
|||
library(combinat)
|
||||
|
||||
permute.me <- c("A", "B", "C", "D")
|
||||
perms <- permn(permute.me) # list of all permutations
|
||||
perms2 <- matrix(unlist(perms), ncol=length(permute.me), byrow=T) # matrix of all permutations
|
||||
perms3 <- apply(perms2, 1, paste, collapse="") # vector of all permutations
|
||||
|
||||
incomplete <- c("ABCD", "CABD", "ACDB", "DACB", "BCDA", "ACBD", "ADCB", "CDAB",
|
||||
"DABC", "BCAD", "CADB", "CDBA", "CBAD", "ABDC", "ADBC", "BDCA",
|
||||
"DCBA", "BACD", "BADC", "BDAC", "CBDA", "DBCA", "DCAB")
|
||||
|
||||
setdiff(perms3, incomplete)
|
||||
|
|
@ -1,26 +1,30 @@
|
|||
/*REXX program finds a missing permuation from an internal list. */
|
||||
/*REXX program finds (a) missing permutation(s) from an internal list.*/
|
||||
list = 'ABCD CABD ACDB DACB BCDA ACBD ADCB CDAB DABC BCAD CADB CDBA',
|
||||
'CBAD ABDC ADBC BDCA DCBA BACD BADC BDAC CBDA DBCA DCAB'
|
||||
@.= /* [↓] needs to be THINGS long. */
|
||||
@abcU = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' /*an uppercase (Latin) alphabet. */
|
||||
things = 4 /*# of unique letters to be used.*/
|
||||
bunch = 4 /*# letters to be used at a time.*/
|
||||
do j=1 for things
|
||||
$.j=substr(@abcU,j,1)
|
||||
end /*j*/ /* [↑] construct a letter array.*/
|
||||
call permset 1 /*invoke PERMSET (recursively).*/
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*──────────────────────────────────PERMSET subroutine──────────────────*/
|
||||
permset: procedure expose $. @. bunch list things; parse arg ?
|
||||
if ?>bunch then do
|
||||
_=; do m=1 for bunch /*build permutation*/
|
||||
_=_ || @.m /*add perm ──► list*/
|
||||
end /*m*/
|
||||
/* [↓] is in list?*/
|
||||
if wordpos(_,list)==0 then say _ ' is missing from the list.'
|
||||
end
|
||||
else do x=1 for things /*build a new perm.*/
|
||||
|
||||
list='ABCD CABD ACDB DACB BCDA ACBD ADCB CDAB DABC BCAD CADB CDBA',
|
||||
'CBAD ABDC ADBC BDCA DCBA BACD BADC BDAC CBDA DBCA DCAB'
|
||||
|
||||
@.=; @abcU='ABCDEFGUIJKLMNOPQRSTUVWXYZ'
|
||||
things=4
|
||||
bunch=4
|
||||
do j=1 for things /*build list of permutation obj. */
|
||||
$.j=substr(@abcu,j,1)
|
||||
end /*j*/
|
||||
call permset 1
|
||||
exit
|
||||
/*─────────────────────────────────────PERMSET subroutine───────────────*/
|
||||
permset:procedure expose $. @. bunch list things; parse arg ?
|
||||
if ?>bunch then do; _=@.1; do m=2 to bunch
|
||||
_=_||@.m
|
||||
end /*m*/
|
||||
if wordpos(_,list)==0 then say _ ' is missing from the list.'
|
||||
end
|
||||
else do x=1 for things /*construction a new permuation. */
|
||||
do k=1 for ?-1; if @.k==$.x then iterate x; end /*k*/
|
||||
@.?=$.x
|
||||
call permset ?+1
|
||||
end /*x*/
|
||||
do k=1 for ?-1
|
||||
if @.k==$.x then iterate x /*been built?*/
|
||||
end /*k*/
|
||||
@.?=$.x /*define as built. */
|
||||
call permset ?+1 /*call recursively.*/
|
||||
end /*x*/
|
||||
return
|
||||
|
|
|
|||
|
|
@ -0,0 +1,24 @@
|
|||
Dim PList as QStringList
|
||||
PList.addItems "ABCD", "CABD", "ACDB", "DACB", "BCDA", "ACBD", "ADCB", "CDAB"
|
||||
PList.additems "DABC", "BCAD", "CADB", "CDBA", "CBAD", "ABDC", "ADBC", "BDCA"
|
||||
PList.additems "DCBA", "BACD", "BADC", "BDAC", "CBDA", "DBCA", "DCAB"
|
||||
|
||||
Dim NumChar(4, 65 to 68) as integer
|
||||
Dim MPerm as string
|
||||
|
||||
'Create table with occurences
|
||||
For x = 0 to PList.Itemcount -1
|
||||
for y = 1 to 4
|
||||
Inc(NumChar(y, asc(PList.Item(x)[y])))
|
||||
next
|
||||
next
|
||||
|
||||
'When a char only occurs 5 times it's the missing one
|
||||
for x = 1 to 4
|
||||
for y = 65 to 68
|
||||
MPerm = MPerm + iif(NumChar(x, y)=5, chr$(y), "")
|
||||
next
|
||||
next
|
||||
|
||||
showmessage MPerm
|
||||
'= DBAC
|
||||
|
|
@ -3,6 +3,6 @@ given = %w{
|
|||
CBAD ABDC ADBC BDCA DCBA BACD BADC BDAC CBDA DBCA DCAB
|
||||
}
|
||||
|
||||
all = given[0].split(//).permutation.collect {|perm| perm.join('')}
|
||||
all = given[0].chars.permutation.collect(&:join)
|
||||
|
||||
missing = all - given # ["DBAC"]
|
||||
puts "missing: #{all - given}"
|
||||
|
|
|
|||
|
|
@ -0,0 +1,33 @@
|
|||
$ include "seed7_05.s7i";
|
||||
|
||||
const func string: missingPermutation (in array string: perms) is func
|
||||
result
|
||||
var string: missing is "";
|
||||
local
|
||||
var integer: pos is 0;
|
||||
var set of char: chSet is (set of char).EMPTY_SET;
|
||||
var string: permutation is "";
|
||||
var char: ch is ' ';
|
||||
begin
|
||||
if length(perms) <> 0 then
|
||||
for key pos range perms[1] do
|
||||
chSet := (set of char).EMPTY_SET;
|
||||
for permutation range perms do
|
||||
ch := permutation[pos];
|
||||
if ch in chSet then
|
||||
excl(chSet, ch);
|
||||
else
|
||||
incl(chSet, ch);
|
||||
end if;
|
||||
end for;
|
||||
missing &:= min(chSet);
|
||||
end for;
|
||||
end if;
|
||||
end func;
|
||||
|
||||
const proc: main is func
|
||||
begin
|
||||
writeln(missingPermutation([] ("ABCD", "CABD", "ACDB", "DACB", "BCDA", "ACBD",
|
||||
"ADCB", "CDAB", "DABC", "BCAD", "CADB", "CDBA", "CBAD", "ABDC", "ADBC",
|
||||
"BDCA", "DCBA", "BACD", "BADC", "BDAC", "CBDA", "DBCA", "DCAB")));
|
||||
end func;
|
||||
Loading…
Add table
Add a link
Reference in a new issue