update
This commit is contained in:
parent
1f1ad49427
commit
6f050a029e
2496 changed files with 37609 additions and 3031 deletions
|
|
@ -0,0 +1,45 @@
|
|||
import std.stdio, std.string, std.algorithm, std.range;
|
||||
|
||||
alias sum = reduce!q{a + b};
|
||||
|
||||
void main() {
|
||||
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;
|
||||
|
||||
// Version 1: test all permutations.
|
||||
const permsSet = perms.zip(0.repeat).assocArray;
|
||||
auto perm = cast(ubyte[])perms[0].dup;
|
||||
do {
|
||||
if (cast(char[])perm !in permsSet)
|
||||
writeln(cast(char[])perm);
|
||||
} 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;
|
||||
char[len] b = 0;
|
||||
foreach (immutable p; perms)
|
||||
b[] ^= p[];
|
||||
b.writeln;
|
||||
|
||||
// Version 3 : Sum ASCII values.
|
||||
immutable rowSum = perms[0].sum;
|
||||
foreach (immutable i; 0 .. len) {
|
||||
immutable sumCols = sum(0, perms.transversal(i));
|
||||
// See how much it falls short.
|
||||
write(cast(char)(rowSum - sumCols % rowSum));
|
||||
}
|
||||
writeln;
|
||||
|
||||
// Version 4: some sort of 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;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,7 +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"}
|
||||
"ADBC" , "BDCA", "DCBA" , "BACD", "BADC", "BDAC" , "CBDA", "DBCA", "DCAB"};
|
||||
|
||||
Part[Complement[ Map[ StringJoin , Permutations[Characters[RandomChoice[ProvidedSet]]]], ProvidedSet], 1]
|
||||
Complement[StringJoin /@ Permutations@Characters@First@#, #] &@ProvidedSet
|
||||
|
||||
->"DBAC"
|
||||
|
||||
->{"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,35 @@
|
|||
#lang racket
|
||||
|
||||
(define almost-all
|
||||
'([A B C D] [C A B D] [A C D B] [D A C B] [B C D A] [A C B D] [A D C B]
|
||||
[C D A B] [D A B C] [B C A D] [C A D B] [C D B A] [C B A D] [A B D C]
|
||||
[A D B C] [B D C A] [D C B A] [B A C D] [B A D C] [B D A C] [C B D A]
|
||||
[D B C A] [D C A B]))
|
||||
|
||||
|
||||
;; Obvious method:
|
||||
(for/first ([p (in-permutations (car almost-all))]
|
||||
#:unless (member p almost-all))
|
||||
p)
|
||||
;; -> '(D B A C)
|
||||
|
||||
|
||||
;; For permutations of any set
|
||||
(define charmap
|
||||
(for/hash ([x (in-list (car almost-all))] [i (in-naturals)])
|
||||
(values x i)))
|
||||
(define size (hash-count charmap))
|
||||
|
||||
;; Illustrating approach mentioned in the task description.
|
||||
;; For each position, character with odd parity at that position.
|
||||
|
||||
(require data/bit-vector)
|
||||
|
||||
(for/list ([i (in-range size)])
|
||||
(define parities (make-bit-vector size #f))
|
||||
(for ([permutation (in-list almost-all)])
|
||||
(define n (hash-ref charmap (list-ref permutation i)))
|
||||
(bit-vector-set! parities n (not (bit-vector-ref parities n))))
|
||||
(for/first ([(c i) charmap] #:when (bit-vector-ref parities i))
|
||||
c))
|
||||
;; -> '(D B A C)
|
||||
Loading…
Add table
Add a link
Reference in a new issue