Substitution cipher: Difference between revisions

No edit summary
Line 1,002:
=={{header|Lambdatalk}}==
The {substitution shift text} function accepts any text containing characters in the set [! " # $ % & ' ( ) * + , - . / 0 1 2 3 4 5 6 7 8 9 : ; < = > ? @ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ \ ] ^ _ ` a b c d e f g h i j k l m n o p q r s t u v w x y z], except the ` character. These characters have charCodes in the range [33..122], (length = 90). The ` character is used to catch and prevent spaces and must not be used. The shift argument can be in the range [0...90] except 5 10 15 29 30 50 53 70 74 which are in conflict with the lambdatalk evaluator.
 
1) lambdatalk code:
 
<lang scheme>
Line 1,018 ⟶ 1,016:
{if {> :i :n}
then
else {let { {:s :s} {:c {charCodeAt 0W.char2code {A.get :i :w}}}
} {if {and {>= :c 33} {<= :c 122}}
then {fromCharCodeW.code2char {+ 33 {% {+ :c {- :s 33}} 90}}}
else {if {= :c 248} then :c else}}
}{substitution.r :w :n :s {+ :i 1}} }}}
Line 1,033 ⟶ 1,031:
0}} }}}
-> substitution
</lang>
 
2) javascript code
 
The substitution function uses two primitives added to the lambdatalk's dictionary
 
<lang javascript>
LAMBDATALK.DICT["charCodeAt"] = function() {
var args = arguments[0].split(" ");
var i = parseInt(args.shift());
var x = args.join(" ");
return x.charCodeAt(i);
};
 
LAMBDATALK.DICT["fromCharCode"] = function() {
var args = arguments[0].trim();
return String.fromCharCode(args);
};
</lang>
 
3) test
 
<lang scheme>
1) intitial text:
 
Line 1,064 ⟶ 1,040:
} -> txt
 
and2) choose the shift:
 
{def shift 13}
-> shift // valid in [0...90] except 5 10 15 29 30 50 53 70 74
 
23) encoding the text
 
{substitution {shift} {txt}}
Line 1,075 ⟶ 1,051:
(!qmADmOPmns'r%murmunqmnpuvr)rqmnm$(vpxm)vp'"%,mv!muv&m&u"%'m*n%mntnv!&'m]un%!npr&mVVm"sm]"!'(&mn'm'urmOn''yrm"smgryn;
 
34) decoding the text
 
{substitution {- 90 {shift}} {substitution {shift} {txt}}}