2016 Update

This commit is contained in:
Tina Müller 2016-12-05 22:15:40 +01:00
parent 948b86eafa
commit dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions

View file

@ -1,22 +1,5 @@
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 another alternate method.
Hint: if you add up the letter values of each column, does a missing letter A, B, C, D
from each column cause the total value for each column to be unique?
<pre>ABCD
<pre>
ABCD
CABD
ACDB
DACB
@ -38,4 +21,31 @@ BADC
BDAC
CBDA
DBCA
DCAB</pre>
DCAB
</pre>
Listed above are all of the permutations of the symbols &nbsp; '''A''', &nbsp; '''B''', &nbsp; '''C''', &nbsp; and &nbsp; '''D''', &nbsp; ''except'' &nbsp; for one permutation that's &nbsp; ''not'' &nbsp; 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:
* &nbsp; [[Permutations]])
<br><br>

View file

@ -0,0 +1,210 @@
use framework "Foundation"
-- UNDER-REPRESENTED CHARACTER IN EACH OF N COLUMNS
-- missingChar :: Record -> Character
script missingChar
-- mean :: [Num] -> Num
on mean(xs)
script sum
on lambda(a, b)
a + b
end lambda
end script
foldl(sum, 0, xs) / (length of xs)
end mean
-- Record -> Character
on lambda(rec)
set nMean to mean(allValues(rec))
script belowMean
on lambda(a, x, i)
set k to toLowerCase(x)
if a is missing value then
set v to keyValue(rec, k)
if v < nMean then
toUpperCase(x)
else
missing value
end if
else
a
end if
end lambda
end script
foldl(belowMean, missing value, allKeys(rec))
end lambda
end script
-- Count of each character type in each character column
-- colCounts :: [Character] -> Record
script colCounts
on lambda(xs)
script tally
on lambda(a, x)
set k to toLowerCase(x)
set v to keyValue(a, k)
if v is missing value then
set n to 1
else
set n to v + 1
end if
updatedRecord(a, k, n)
end lambda
end script
foldl(tally, {name:""}, xs)
end lambda
end script
-- TEST
on run
map(missingChar, ¬
map(colCounts, ¬
transpose(map(curry(splitOn)'s lambda(""), ¬
splitOn(space, ¬
"ABCD CABD ACDB DACB BCDA ACBD " & ¬
"ADCB CDAB DABC BCAD CADB CDBA " & ¬
"CBAD ABDC ADBC BDCA DCBA BACD " & ¬
"BADC BDAC CBDA DBCA DCAB"))))) as text
--> "DBAC"
end run
---------------------------------------------------------------------------
-- GENERIC FUNCTIONS
-- transpose :: [[a]] -> [[a]]
on transpose(xss)
script column
on lambda(_, iCol)
script row
on lambda(xs)
item iCol of xs
end lambda
end script
map(row, xss)
end lambda
end script
map(column, item 1 of xss)
end transpose
-- curry :: (Script|Handler) -> Script
on curry(f)
script
on lambda(a)
script
on lambda(b)
lambda(a, b) of mReturn(f)
end lambda
end script
end lambda
end script
end curry
-- 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 lambda(item i of xs, i, xs)
end repeat
return lst
end tell
end map
-- 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 lambda(v, item i of xs, i, xs)
end repeat
return v
end tell
end foldl
-- splitOn :: Text -> Text -> [Text]
on splitOn(strDelim, strMain)
set {dlm, my text item delimiters} to {my text item delimiters, strDelim}
set xs to text items of strMain
set my text item delimiters to dlm
return xs
end splitOn
-- ord :: Character -> Int
on ord(x)
id of x
end ord
-- 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 lambda : f
end script
end if
end mReturn
-- NSString
-- toLowerCase :: String -> String
on toLowerCase(str)
set ca to current application
((ca's NSString's stringWithString:(str))'s ¬
lowercaseStringWithLocale:(ca's NSLocale's currentLocale())) as text
end toLowerCase
-- toUpperCase :: String -> String
on toUpperCase(str)
set ca to current application
((ca's NSString's stringWithString:(str))'s ¬
uppercaseStringWithLocale:(ca's NSLocale's currentLocale())) as text
end toUpperCase
-- NSDictionary
-- allKeys :: Record -> [String]
on allKeys(rec)
(current application's NSDictionary's dictionaryWithDictionary:rec)'s allKeys() as list
end allKeys
-- allValues :: Record -> [a]
on allValues(rec)
(current application's NSDictionary's dictionaryWithDictionary:rec)'s allValues() as list
end allValues
-- keyValue :: Record -> String -> Maybe String
on keyValue(rec, strKey)
set ca to current application
set v to (ca's NSDictionary's dictionaryWithDictionary:rec)'s objectForKey:strKey
if v is not missing value then
item 1 of ((ca's NSArray's arrayWithObject:v) as list)
else
missing value
end if
end keyValue
-- updatedRecord :: Record -> String -> a -> Record
on updatedRecord(rec, strKey, varValue)
set ca to current application
set nsDct to (ca's NSMutableDictionary's dictionaryWithDictionary:rec)
nsDct's setValue:varValue forKey:strKey
item 1 of ((ca's NSArray's arrayWithObject:nsDct) as list)
end updatedRecord

View file

@ -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'
})();

View file

@ -1,3 +1,3 @@
table:{b@<b:(x@*:'a),'#:'a:=x}
,/"ABCD"@&:'{5=(table p[;x])[;1]}'!4
table:{b@<b:(x@*:'a),'#:'a:=x}
,/"ABCD"@&:'{5=(table p[;x])[;1]}'!4
"DBAC"

View file

@ -0,0 +1 @@
,/p2@&~(p2:{x@m@&n=(#?:)'m:!n#n:#x}[*p]) _lin p

View file

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

View file

@ -1,29 +1,29 @@
/*REXX program finds one or more missing permutations from an internal list.*/
/*REXX pgm finds one or more missing permutations from an internal list & displays them.*/
list = 'ABCD CABD ACDB DACB BCDA ACBD ADCB CDAB DABC BCAD CADB CDBA',
'CBAD ABDC ADBC BDCA DCBA BACD BADC BDAC CBDA DBCA DCAB'
@.= /* [↓] needs to be as long as THINGS.*/
@abcU = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' /*an uppercase (Latin/Roman) alphabet. */
things = 4 /*number of unique letters to be used. */
bunch = 4 /*number letters to be used at a time. */
do j=1 for things /* [↓] only get a portion of alphabet.*/
$.j=substr(@abcU,j,1) /*extract just one letter from alphabet*/
end /*j*/ /* [↑] build a letter array for speed.*/
call permSet 1 /*invoke PERMSET sub. (recursively). */
exit /*stick a fork in it, we're all done. */
/*────────────────────────────────────────────────────────────────────────────*/
@.= /* [↓] needs to be as long as THINGS.*/
@abcU = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' /*an uppercase (Latin/Roman) alphabet. */
things = 4 /*number of unique letters to be used. */
bunch = 4 /*number letters to be used at a time. */
do j=1 for things /* [↓] only get a portion of alphabet.*/
$.j=substr(@abcU,j,1) /*extract just one letter from alphabet*/
end /*j*/ /* [↑] build a letter array for speed.*/
call permSet 1 /*invoke PERMSET subroutine recursively*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
permSet: procedure expose $. @. bunch list things; parse arg ?
if ?>bunch then do
_=; do m=1 for bunch /*build a permutation. */
_=_ || @.m /*add permutation──►list*/
end /*m*/
/* [↓] is in the list? */
if wordpos(_,list)==0 then say _ ' is missing from the list.'
end
else do x=1 for things /*build a permutation. */
do k=1 for ?-1
if @.k==$.x then iterate x /*was permutation built?*/
end /*k*/
@.?=$.x /*define as being built.*/
call permSet ?+1 /*call subr. recursively*/
end /*x*/
return
if ?>bunch then do; _=
do m=1 for bunch /*build a permutation. */
_=_ || @.m /*add permutation──►list.*/
end /*m*/
/* [↓] is in the list? */
if wordpos(_,list)==0 then say _ ' is missing from the list.'
end
else do x=1 for things /*build a permutation. */
do k=1 for ?-1
if @.k==$.x then iterate x /*was permutation built? */
end /*k*/
@.?=$.x /*define as being built. */
call permSet ?+1 /*call subr. recursively.*/
end /*x*/
return

View file

@ -0,0 +1,14 @@
10 LET l$="ABCD CABD ACDB DACB BCDA ACBD ADCB CDAB DABC BCAD CADB CDBA CBAD ABDC ADBC BDCA DCBA BACD BADC BDAC CBDA DBCA DCAB"
20 LET length=LEN l$
30 FOR a= CODE "A" TO CODE "D"
40 FOR b= CODE "A" TO CODE "D"
50 FOR c= CODE "A" TO CODE "D"
60 FOR d= CODE "A" TO CODE "D"
70 LET x$=""
80 IF a=b OR a=c OR a=d OR b=c OR b=d OR c=d THEN GO TO 140
90 LET x$=CHR$ a+CHR$ b+CHR$ c+CHR$ d
100 FOR i=1 TO length STEP 5
110 IF x$=l$(i TO i+3) THEN GO TO 140
120 NEXT i
130 PRINT x$;" is missing"
140 NEXT d: NEXT c: NEXT b: NEXT a