i. cvicenie
Created: 2009-10-28 - 17:43
> with(StringTools):
> with(linalg):
> tmp:="Dolu fvb dlyl h ahkwvsl huk P dhz h mpzo Pu aol Whslvgvpj aptl, Huk zpkl if zpkl vu aol liipun apkl Dl zwyhdslk aoyvbno aol vvgl huk zsptl, Vy zrpaalylk dpao thuf h jhbkhs mspw aoyvbno aol klwaoz vm Jhtiyphu mlu Tf olhya dhz ypml dpao aol qvf vm spml, Mvy P svclk fvb lclu aolu.";
>
> str2int := proc (text)
> Select(IsAlpha, text);
> UpperCase(%);
> convert(%, bytes);
> return map(x -> x-65, %);
> end proc;
>
> int2str := proc (text)
> return convert(map(x -> x+65, text), bytes);
> end proc;
>
> freq := proc (text)
> local pole, f, i;
> pole := str2int(text);
> f := [seq(0, i=1..26)];
> for i to nops(pole) do f := subsop(1+pole[i]=op(pole[i]+1, f)+1, f); end do;
> return f;
> end proc;
>
> freq_perc := proc (text)
> local f, p;
> f := freq(text);
> p := add(f[k], k=1..26); #súčet všetkých prvkov
> return map(x->1.*x/p, f);
> end proc;
>
> find_max := proc (pole)
> return select(x->has(pole[x+1], max(op(pole))), [$0..nops(pole)-1]);
> end proc;
>
> engfreq :=[.082, .015, .028, .043, .127, .022, .020, .061, .070, .002, .008, .040, .024, .067, .075, .019, .001, .060, .063, .091, .028, .010, .023, .001, .020, .001];
>
> dotprod(engfreq, engfreq);
>
> corr := proc (v)
> local i,j;
> return [seq(add(engfreq[i]*v[1+((i+j-1) mod 26)],i=1..26),j=0..25)];
> end proc;
>
> coinc := proc (text, posun)
> local i, pole, z;
> pole := str2int(text);
> z:=0;
> for i while i <= nops(pole)-posun do
> if pole[i]=pole[i+posun] then z:=z+1 fi;
> end do;
> return z;
> end proc;
>
> choose := proc (text, dlzka, stlpec)
> local pole, p, i;
> pole := str2int(text);
> p := [];
> for i from stlpec by dlzka while i <= nops(pole) do
> p := [op(p), pole[i]];
> end do;
> return int2str(p);
> end proc;
tmp := "Dolu fvb dlyl h ahkwvsl huk P dhz h mpzo Pu aol Whslvgvpj aptl, Huk
zpkl if zpkl vu aol liipun apkl Dl zwyhdslk aoyvbno aol vvgl huk zsptl,
Vy zrpaalylk dpao thuf h jhbkhs mspw aoyvbno aol klwaoz vm Jhtiyphu mlu
Tf olhya dhz ypml dpao aol qvf vm spml, Mvy P svclk fvb lclu aolu."
str2int := proc(text)
(StringTools:-Select)(StringTools:-IsAlpha, text);
(StringTools:-UpperCase)(%);
convert(%, bytes);
return map(proc(x) option operator, arrow; x - 65 end proc;, %);
end proc;
int2str := proc(text)
return convert(map(proc(x) option operator, arrow; x + 65 end proc;, text),
bytes)
end proc;
freq := proc(text)
local pole, f, i;
pole := str2int(text);
f := [seq(0, i = 1 .. 26)];
for i to nops(pole) do f := subsop(1 + pole[i] = op(1 + pole[i], f) + 1, f)
end do;
return f;
end proc;
freq_perc := proc(text)
local f, p;
f := freq(text);
p := add(f[k], k = 1 .. 26);
return map(proc(x) option operator, arrow; (1.*x)/(p) end proc;, f);
end proc;
find_max := proc(pole)
return select(
proc(x) option operator, arrow; has(pole[x + 1], max(op(pole))) end proc;,
[`$`(0 .. nops(pole) - 1)])
end proc;
engfreq := [0.082, 0.015, 0.028, 0.043, 0.127, 0.022, 0.020, 0.061, 0.070,
0.002, 0.008, 0.040, 0.024, 0.067, 0.075, 0.019, 0.001, 0.060, 0.063, 0.091,
0.028, 0.010, 0.023, 0.001, 0.020, 0.001]
0.065601
corr := proc(v)
local i, j;
return [
seq(add(engfreq[i]*v[1 + `mod`(i + j - 1, 26)], i = 1 .. 26), j = 0 .. 25)]
end proc;
coinc := proc(text, posun)
local i, pole, z;
pole := str2int(text);
z := 0;
for i while i <= nops(pole) - posun do if pole[i] = pole[i + posun] then
z := z + 1
end if;
end do;
return z;
end proc;
choose := proc(text, dlzka, stlpec)
local pole, p, i;
pole := str2int(text);
p := [];
for i from stlpec by dlzka while i <= nops(pole) do p := [op(p), pole[i]]
end do;
return int2str(p);
end proc;
> find_max(corr(freq_perc(tmp)));
[7]
> tmp2 := "opkjcpcsrqtkhespzxxsjmuinieiovvryaaqaeicjnystxnemmbyzrvvvjopwxzinqjibxzjdmjfsxnizvjiqidfzzymlxyeiljiqidfzzzaspwxcbnihespzxxsjmuinbnijiehjzutsvrxdvmwmwkihaujharvzvgqsgfqhwtroqvsabnijycrzzgfwpzxtxuxsrkmvtxmgorxopkxwqvsaxafzmteoquroruxcmicpiirjbkwwwjyzqtavmtlopkzipeimihmzmkcvxviovvhnwlxkeiiqmxwwsewvzkmriexdnoirmwoiwcrhlzwdvlsfqrxdwtmgtiinmtxshfrgggwowlqhixcqsdtgmzirikedtyefirzvqreppvjmwsxvijspziiwrumxizirmexcmkrrrfxzxriowvrjbkxvekiqmtmtxyihmzlchfjvbzeqoyenvuxpivrpbopwdvhjzgrsbgpjqzwqvztoqyrcxtymzkrhppadlkpmemedtgfzifropkmbxvvimzedskiiboezzlpimxepmcmognegfviiqjibxzjdmjydhrxzazswxvqnivtseimioordvvzdwawwwjyzaujqcsimvuxswrvztowhiumijuprrvadvlsfqrxdwtgcrkedvkhwrklzcvhoxvadtredtvemqtmhecmxqfirgfpjzkhhioxrpkvsegtgqieppvxcmzeppvpdazwogmiicsfsvzrmmjavmtlxwxvswgsilyxcxyixwsqcmygvkvofzpdboigeehzfvsgyiinkbizmjxvkuqdmceoqurcjjxvvjefhzdzlteaijjjzbyzrvvvjopwxzinithcxyimqtjcvdeoqurgitymqzcsbgsncxig";
tmp2 := "opkjcpcsrqtkhespzxxsjmuinieiovvryaaqaeicjnystxnemmbyzrvvvjopwxzinqji\
bxzjdmjfsxnizvjiqidfzzymlxyeiljiqidfzzzaspwxcbnihespzxxsjmuinbnijiehjzutsvr\
xdvmwmwkihaujharvzvgqsgfqhwtroqvsabnijycrzzgfwpzxtxuxsrkmvtxmgorxopkxwqvsax\
afzmteoquroruxcmicpiirjbkwwwjyzqtavmtlopkzipeimihmzmkcvxviovvhnwlxkeiiqmxww\
sewvzkmriexdnoirmwoiwcrhlzwdvlsfqrxdwtmgtiinmtxshfrgggwowlqhixcqsdtgmzirike\
dtyefirzvqreppvjmwsxvijspziiwrumxizirmexcmkrrrfxzxriowvrjbkxvekiqmtmtxyihmz\
lchfjvbzeqoyenvuxpivrpbopwdvhjzgrsbgpjqzwqvztoqyrcxtymzkrhppadlkpmemedtgfzi\
fropkmbxvvimzedskiiboezzlpimxepmcmognegfviiqjibxzjdmjydhrxzazswxvqnivtseimi\
oordvvzdwawwwjyzaujqcsimvuxswrvztowhiumijuprrvadvlsfqrxdwtgcrkedvkhwrklzcvh\
oxvadtredtvemqtmhecmxqfirgfpjzkhhioxrpkvsegtgqieppvxcmzeppvpdazwogmiicsfsvz\
rmmjavmtlxwxvswgsilyxcxyixwsqcmygvkvofzpdboigeehzfvsgyiinkbizmjxvkuqdmceoqu\
rcjjxvvjefhzdzlteaijjjzbyzrvvvjopwxzinithcxyimqtjcvdeoqurgitymqzcsbgsncxig"
> for i from 1 to 25 do
> coinc(tmp2,i);
> end do;
>
> for i from 1 to 8 do
> find_max(corr(freq_perc(choose(tmp2,8,i))));
> end do;
>
47
46
28
49
34
33
21
58
26
41
32
48
23
41
33
66
30
37
35
46
38
51
39
53
40
[21]
[8]
[6]
[4]
[14]
[4]
[17]
[4]
> int2str([21,8,6,4,14,4,17,4]);
"VIGEOERE"