Add tasks for all the new languages

This commit is contained in:
Tina Müller 2016-12-05 23:44:36 +01:00
parent 9dc3c2bb62
commit bba7bfd280
13208 changed files with 134745 additions and 0 deletions

View file

@ -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

View file

@ -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 }

View file

@ -0,0 +1,19 @@
import strutils
proc missingPermutation(arr): 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""".split()
echo missingPermutation(given)

View file

@ -0,0 +1,46 @@
constant perms = {"ABCD", "CABD", "ACDB", "DACB", "BCDA", "ACBD", "ADCB", "CDAB",
"DABC", "BCAD", "CADB", "CDBA", "CBAD", "ABDC", "ADBC", "BDCA",
"DCBA", "BACD", "BADC", "BDAC", "CBDA", "DBCA", "DCAB"}
-- 1: sum of letters
sequence r = repeat(0,4)
for i=1 to length(perms) do
r = sq_add(r,perms[i])
end for
r = sq_sub(max(r)+'A',r)
puts(1,r&'\n')
-- 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
r = repeat(0,4)
for i=1 to length(perms) do
r = sq_xor_bits(r,perms[i])
end for
puts(1,r&'\n')
-- (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
r = " "
for i=1 to length(r) do
sequence count = repeat(0,4)
for j=1 to length(perms) do
count[perms[j][i]-'A'+1] += 1
end for
r[i] = smallest(count,1)+'A'-1
end for
puts(1,r&'\n')
-- (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
for i=1 to factorial(4) do
r = permute(i,"ABCD")
if not find(r,perms) then exit end if
end for
puts(1,r&'\n')
-- (relies on brute force(!) - but this is the only method that could be made to cope with >1 omission)

View file

@ -0,0 +1,13 @@
list = "ABCD CABD ACDB DACB BCDA ACBD ADCB CDAB DABC BCAD CADB CDBA CBAD ABDC ADBC BDCA DCBA BACD BADC BDAC CBDA DBCA DCAB"
for a = ascii("A") to ascii("D")
for b = ascii("A") to ascii("D")
for c = ascii("A") to ascii("D")
for d = ascii("A") to ascii("D")
x = char(a) + char(b) + char(c)+ char(d)
if a!=b and a!=c and a!=d and b!=c and b!=d and c!=d
if substr(list,x) = 0 see x + " missing" + nl ok ok
next
next
next
next

View file

@ -0,0 +1,14 @@
func check_perm(arr) {
(var hash = Hash.new).@{arr} = @[1]*arr.len;
arr.each { |s|
s.len.times {
var t = (s.substr(1) + s.substr(0, 1));
hash.has_key(t) || return t;
}
}
}
var perms = %w(ABCD CABD ACDB DACB BCDA ACBD ADCB CDAB DABC BCAD CADB CDBA
CBAD ABDC ADBC BDCA DCBA BACD BADC BDAC CBDA DBCA DCAB);
say check_perm(perms);

View file

@ -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];

View file

@ -0,0 +1 @@
map(encode_string) | transpose | map(parities | decode) | join("")

View file

@ -0,0 +1,2 @@
$ jq -R . Find_the_missing_permutation.txt | jq -s -f Find_the_missing_permutation.jq
"DBAC"