Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
45
Task/Chaocipher/Mathematica/chaocipher.math
Normal file
45
Task/Chaocipher/Mathematica/chaocipher.math
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
ClearAll[ichaoalphabet, iMoveToFront, ChaoCipher]
|
||||
ichaoalphabet = CharacterRange["A", "Z"];
|
||||
iMoveToFront[l_List, sel_] := Module[{p},
|
||||
p = FirstPosition[l, sel];
|
||||
RotateLeft[l, p - 1]
|
||||
]
|
||||
ChaoCipher::wrongcipheralpha =
|
||||
"The cipher alphabet `1` is not a permutation of \
|
||||
\"A\"\[LongDash]\"Z\".";
|
||||
ChaoCipher::wrongplainalpha =
|
||||
"The plain alphabet `1` is not a permutation of \"A\"\[LongDash]\"Z\
|
||||
\".";
|
||||
ChaoCipher[str_String, {plainalpha_List, cipheralpha_List}] :=
|
||||
Module[{pa, ca, plain, new, papermute, capermute, out},
|
||||
ca = ToUpperCase[cipheralpha];
|
||||
pa = ToUpperCase[plainalpha];
|
||||
If[Sort[ca] =!= Sort[ichaoalphabet],
|
||||
Message[ChaoCipher::wrongcipheralpha, ca];
|
||||
$Failed
|
||||
,
|
||||
If[Sort[pa] =!= Sort[ichaoalphabet],
|
||||
Message[ChaoCipher::wrongplainalpha, pa];
|
||||
$Failed
|
||||
,
|
||||
capermute = SubsetMap[RotateLeft, Range[26], Range[2, 14]];
|
||||
papermute =
|
||||
SubsetMap[RotateLeft, RotateLeft[Range[26], 1], Range[3, 14]];
|
||||
plain =
|
||||
Select[Characters[ToUpperCase[str]], MemberQ[ichaoalphabet, #] &];
|
||||
|
||||
out = Table[
|
||||
new = Association[Thread[pa -> ca]][p];
|
||||
pa = iMoveToFront[pa, p];
|
||||
ca = iMoveToFront[ca, new];
|
||||
pa = pa[[papermute]];
|
||||
ca = ca[[capermute]];
|
||||
new
|
||||
,
|
||||
{p, plain}
|
||||
];
|
||||
StringJoin[out]
|
||||
]
|
||||
]
|
||||
]
|
||||
ChaoCipher["WELLDONEISBETTERTHANWELLSAID",{Characters@"PTLNBQDEOYSFAVZKGJRIHWXUMC",Characters@"HXUCZVAMDSLKPEFJRIGTWOBNYQ"}]
|
||||
Loading…
Add table
Add a link
Reference in a new issue