June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
42
Task/Caesar-cipher/Applesoft-BASIC/caesar-cipher.applesoft
Normal file
42
Task/Caesar-cipher/Applesoft-BASIC/caesar-cipher.applesoft
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
100 INPUT ""; T$
|
||||
|
||||
110 LET K% = RND(1) * 25 + 1
|
||||
120 PRINT "ENCODED WITH ";
|
||||
130 GOSUB 200ENCODED
|
||||
|
||||
140 LET K% = 26 - K%
|
||||
150 PRINT "DECODED WITH ";
|
||||
160 GOSUB 200DECODED
|
||||
|
||||
170 END
|
||||
|
||||
REM ENCODED/DECODED
|
||||
200 PRINT "CAESAR " K%;
|
||||
210 LET K$(1) = " (ROT-13)"
|
||||
220 PRINT K$(K% = 13)
|
||||
230 GOSUB 300CAESAR
|
||||
240 PRINT T$
|
||||
250 RETURN
|
||||
|
||||
REM CAESAR T$ K%
|
||||
300 FOR I = 1 TO LEN(T$)
|
||||
310 LET C$ = MID$(T$, I, 1)
|
||||
320 GOSUB 400ENCODE
|
||||
330 LET L = I - 1
|
||||
340 LET T$(0) = MID$(T$, 1, L)
|
||||
350 LET L = I + 1
|
||||
360 LET T$ = C$ + MID$(T$, L)
|
||||
370 LET T$ = T$(0) + T$
|
||||
380 NEXT I
|
||||
390 RETURN
|
||||
|
||||
REM ENCODE C$ K%
|
||||
400 LET C = ASC(C$)
|
||||
410 LET L = (C > 95) * 32
|
||||
420 LET C = C - L
|
||||
430 IF C < 65 THEN RETURN
|
||||
440 IF C > 90 THEN RETURN
|
||||
450 LET C = C + K%
|
||||
460 IF C > 90 THEN C = C - 26
|
||||
470 LET C$ = CHR$(C + L)
|
||||
480 RETURN
|
||||
|
|
@ -1,8 +1,8 @@
|
|||
fun caesar(s, k, decode: false):
|
||||
if decode: k = 26 - k
|
||||
join(char((ord(c) - 65 + k) % 26 + 65) | c in s.upper() where "a" <= c <= "A")
|
||||
char({(ord(c) - 65 + k) mod 26 + 65) | c in s.upper() where "a" <= c <= "A"}).join()
|
||||
|
||||
let msg = "The quick brown fox jumped over the lazy dogs"
|
||||
print msg
|
||||
let enc = caesar(msg, 11)
|
||||
print ..(enc) ..(caesar(enc, 11, decode: true))
|
||||
print~(enc)~(caesar(enc, 11, decode: true))
|
||||
|
|
|
|||
37
Task/Caesar-cipher/COBOL/caesar-cipher-1.cobol
Normal file
37
Task/Caesar-cipher/COBOL/caesar-cipher-1.cobol
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
identification division.
|
||||
program-id. caesar.
|
||||
data division.
|
||||
1 msg pic x(50)
|
||||
value "The quick brown fox jumped over the lazy dog.".
|
||||
1 offset binary pic 9(4) value 7.
|
||||
1 from-chars pic x(52).
|
||||
1 to-chars pic x(52).
|
||||
1 tabl.
|
||||
2 pic x(26) value "abcdefghijklmnopqrstuvwxyz".
|
||||
2 pic x(26) value "ABCDEFGHIJKLMNOPQRSTUVWXYZ".
|
||||
2 pic x(26) value "abcdefghijklmnopqrstuvwxyz".
|
||||
2 pic x(26) value "ABCDEFGHIJKLMNOPQRSTUVWXYZ".
|
||||
procedure division.
|
||||
begin.
|
||||
display msg
|
||||
perform encrypt
|
||||
display msg
|
||||
perform decrypt
|
||||
display msg
|
||||
stop run
|
||||
.
|
||||
|
||||
encrypt.
|
||||
move tabl (1:52) to from-chars
|
||||
move tabl (1 + offset:52) to to-chars
|
||||
inspect msg converting from-chars
|
||||
to to-chars
|
||||
.
|
||||
|
||||
decrypt.
|
||||
move tabl (1 + offset:52) to from-chars
|
||||
move tabl (1:52) to to-chars
|
||||
inspect msg converting from-chars
|
||||
to to-chars
|
||||
.
|
||||
end program caesar.
|
||||
7
Task/Caesar-cipher/Clojure/caesar-cipher-2.clj
Normal file
7
Task/Caesar-cipher/Clojure/caesar-cipher-2.clj
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
(defn encode [k s]
|
||||
(let [f #(take 26 (drop %3 (cycle (range (int %1) (inc (int %2))))))
|
||||
a #(map char (concat (f \a \z %) (f \A \Z %)))]
|
||||
(apply str (replace (zipmap (a 0) (a k)) s))))
|
||||
|
||||
(defn decode [k s]
|
||||
(encode (- 26 k) s))
|
||||
11
Task/Caesar-cipher/Crystal/caesar-cipher.crystal
Normal file
11
Task/Caesar-cipher/Crystal/caesar-cipher.crystal
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
class String
|
||||
ALPHABET = ("A".."Z").to_a
|
||||
|
||||
def caesar_cipher(num)
|
||||
self.tr(ALPHABET.join, ALPHABET.rotate(num).join)
|
||||
end
|
||||
end
|
||||
|
||||
# demo
|
||||
encrypted = "THEQUICKBROWNFOXJUMPSOVERTHELAZYDOG".caesar_cipher(5)
|
||||
decrypted = encrypted.caesar_cipher(-5)
|
||||
|
|
@ -1,6 +1,7 @@
|
|||
import system'routines.
|
||||
import system'math.
|
||||
import extensions.
|
||||
import extensions'text.
|
||||
|
||||
const Letters = "abcdefghijklmnopqrstuvwxyz".
|
||||
const BigLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".
|
||||
|
|
@ -12,16 +13,18 @@ class Encrypting :: Enumerator
|
|||
object theKey.
|
||||
object theEnumerator.
|
||||
|
||||
constructor new key:aKey text:aText
|
||||
constructor key:aKey text:aText
|
||||
[
|
||||
theKey := aKey.
|
||||
theEnumerator := aText enumerator.
|
||||
]
|
||||
|
||||
next => theEnumerator.
|
||||
bool next => theEnumerator.
|
||||
|
||||
reset => theEnumerator.
|
||||
|
||||
enumerable => theEnumerator.
|
||||
|
||||
get
|
||||
[
|
||||
var aChar := theEnumerator get.
|
||||
|
|
@ -48,13 +51,13 @@ class Encrypting :: Enumerator
|
|||
extension encryptOp
|
||||
{
|
||||
encrypt : aKey
|
||||
= Encrypting new key:aKey text:self; summarize(String new).
|
||||
= Encrypting key:aKey text:self; summarize(StringWriter new).
|
||||
|
||||
decrypt :aKey
|
||||
= Encrypting new key(26 - aKey) text:self; summarize(String new).
|
||||
= Encrypting key(26 - aKey) text:self; summarize(StringWriter new).
|
||||
}
|
||||
|
||||
program =
|
||||
public program =
|
||||
[
|
||||
console printLine("Original text :",TestText).
|
||||
|
||||
|
|
|
|||
18
Task/Caesar-cipher/Factor/caesar-cipher.factor
Normal file
18
Task/Caesar-cipher/Factor/caesar-cipher.factor
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
USING: io kernel locals math sequences unicode.categories ;
|
||||
IN: rosetta-code.caesar-cipher
|
||||
|
||||
:: cipher ( n s -- s' )
|
||||
[| c |
|
||||
c Letter? [
|
||||
c letter? CHAR: a CHAR: A ? :> a
|
||||
c a - n + 26 mod a +
|
||||
]
|
||||
[ c ] if
|
||||
] :> shift
|
||||
s [ shift call ] map ;
|
||||
|
||||
: encrypt ( n s -- s' ) cipher ;
|
||||
: decrypt ( n s -- s' ) [ 26 swap - ] dip cipher ;
|
||||
|
||||
11 "Esp bftnv mczhy qzi ufxapo zgpc esp wlkj ozr." decrypt print
|
||||
11 "The quick brown fox jumped over the lazy dog." encrypt print
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
function caesar (text, shift) {
|
||||
return text.toUpperCase().replace(/[^A-Z]/g,'').replace(/[A-Z]/g, function(a) {
|
||||
return text.toUpperCase().replace(/[^A-Z]/g,'').replace(/./g, function(a) {
|
||||
return String.fromCharCode(65+(a.charCodeAt(0)-65+shift)%26);
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
var caesar = (text, shift) => text
|
||||
.toUpperCase()
|
||||
.replace(/[^A-Z]/g, '')
|
||||
.replace(/[A-Z]/g, a =>
|
||||
.replace(/./g, a =>
|
||||
String.fromCharCode(65 + (a.charCodeAt(0) - 65 + shift) % 26));
|
||||
|
|
|
|||
|
|
@ -1,18 +1,15 @@
|
|||
function rot(s::String, key::Integer)
|
||||
map(s) do c
|
||||
if 'a' <= c <= 'z'
|
||||
char( mod(c - 'a' + key, 26) + 'a')
|
||||
elseif 'A' <= c <= 'Z'
|
||||
char( mod(c - 'A' + key, 26) + 'A')
|
||||
else
|
||||
c
|
||||
end
|
||||
function rot(ch::Char, key::Integer)
|
||||
if key < 1 || key > 25 end
|
||||
if isalpha(ch)
|
||||
shft = ifelse(islower(ch), 'a', 'A')
|
||||
ch = (ch - shft + key) % 26 + shft
|
||||
end
|
||||
return ch
|
||||
end
|
||||
rot(str::AbstractString, key::Integer) = map(x -> rot(x, key), str)
|
||||
|
||||
msg = "The five boxing wizards jump quickly"
|
||||
key = 3
|
||||
txt = "The five boxing wizards jump quickly"
|
||||
invkey = 26 - 3
|
||||
|
||||
println("Original: ", txt);
|
||||
println("Encrypted: ", rot(txt, key))
|
||||
println("Decrypted: ", rot(rot(txt, key), 26 - key))
|
||||
println("# original: $msg\n encrypted: $(rot(msg, key))\n decrypted: $(rot(rot(msg, key), invkey))")
|
||||
|
|
|
|||
77
Task/Caesar-cipher/Modula-2/caesar-cipher.mod2
Normal file
77
Task/Caesar-cipher/Modula-2/caesar-cipher.mod2
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
MODULE CaesarCipher;
|
||||
FROM Conversions IMPORT IntToStr;
|
||||
FROM Terminal IMPORT WriteString, WriteLn, ReadChar;
|
||||
|
||||
TYPE String = ARRAY[0..64] OF CHAR;
|
||||
|
||||
PROCEDURE Encrypt(p : String; key : CARDINAL) : String;
|
||||
VAR e : String;
|
||||
VAR i,t : CARDINAL;
|
||||
VAR c : CHAR;
|
||||
BEGIN
|
||||
FOR i:=0 TO HIGH(p) DO
|
||||
IF p[i]=0C THEN BREAK; END;
|
||||
|
||||
t := ORD(p[i]);
|
||||
IF (p[i]>='A') AND (p[i]<='Z') THEN
|
||||
t := t + key;
|
||||
IF t>ORD('Z') THEN
|
||||
t := t - 26;
|
||||
END;
|
||||
ELSIF (p[i]>='a') AND (p[i]<='z') THEN
|
||||
t := t + key;
|
||||
IF t>ORD('z') THEN
|
||||
t := t - 26;
|
||||
END;
|
||||
END;
|
||||
e[i] := CHR(t);
|
||||
END;
|
||||
RETURN e;
|
||||
END Encrypt;
|
||||
|
||||
PROCEDURE Decrypt(p : String; key : CARDINAL) : String;
|
||||
VAR e : String;
|
||||
VAR i,t : CARDINAL;
|
||||
VAR c : CHAR;
|
||||
BEGIN
|
||||
FOR i:=0 TO HIGH(p) DO
|
||||
IF p[i]=0C THEN BREAK; END;
|
||||
|
||||
t := ORD(p[i]);
|
||||
IF (p[i]>='A') AND (p[i]<='Z') THEN
|
||||
t := t - key;
|
||||
IF t<ORD('A') THEN
|
||||
t := t + 26;
|
||||
END;
|
||||
ELSIF (p[i]>='a') AND (p[i]<='z') THEN
|
||||
t := t - key;
|
||||
IF t<ORD('a') THEN
|
||||
t := t + 26;
|
||||
END;
|
||||
END;
|
||||
e[i] := CHR(t);
|
||||
END;
|
||||
RETURN e;
|
||||
END Decrypt;
|
||||
|
||||
VAR txt,enc : String;
|
||||
VAR key : CARDINAL;
|
||||
BEGIN
|
||||
txt := "The five boxing wizards jump quickly";
|
||||
key := 3;
|
||||
|
||||
WriteString("Original: ");
|
||||
WriteString(txt);
|
||||
WriteLn;
|
||||
|
||||
enc := Encrypt(txt, key);
|
||||
WriteString("Encrypted: ");
|
||||
WriteString(enc);
|
||||
WriteLn;
|
||||
|
||||
WriteString("Decrypted: ");
|
||||
WriteString(Decrypt(enc, key));
|
||||
WriteLn;
|
||||
|
||||
ReadChar;
|
||||
END CaesarCipher.
|
||||
60
Task/Caesar-cipher/Ring/caesar-cipher.ring
Normal file
60
Task/Caesar-cipher/Ring/caesar-cipher.ring
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
# Project : Caesar cipher
|
||||
# Date : 2017/11/11
|
||||
# Author : Gal Zsolt (~ CalmoSoft ~)
|
||||
# Email : <calmosoft@gmail.com>
|
||||
|
||||
cipher = "pack my box with five dozen liquor jugs"
|
||||
abc = "abcdefghijklmnopqrstuvwxyz"
|
||||
see "text is to be encrypted:" + nl
|
||||
see cipher+ nl + nl
|
||||
str = ""
|
||||
key = random(24) + 1
|
||||
see "key = " + key + nl + nl
|
||||
see "encrypted:" + nl
|
||||
caesarencode(cipher, key)
|
||||
see str + nl + nl
|
||||
cipher = str
|
||||
see "decrypted again:" + nl
|
||||
caesardecode(cipher, key)
|
||||
see str + nl
|
||||
|
||||
func caesarencode(cipher, key)
|
||||
str = ""
|
||||
for n = 1 to len(cipher)
|
||||
if cipher[n] != " "
|
||||
pos = substr(abc, cipher[n])
|
||||
if pos + key < len(abc)
|
||||
str = str + abc[pos + key]
|
||||
else
|
||||
if (pos+key)-len(abc) != 0
|
||||
str = str + abc[(pos+key)%len(abc)]
|
||||
else
|
||||
str = str +abc[key+pos]
|
||||
ok
|
||||
ok
|
||||
else
|
||||
str = str + " "
|
||||
ok
|
||||
next
|
||||
return str
|
||||
|
||||
func caesardecode(cipher, key)
|
||||
str = ""
|
||||
for n= 1 to len(cipher)
|
||||
if cipher[n] != " "
|
||||
pos = substr(abc, cipher[n])
|
||||
if (pos - key) > 0 and pos != key
|
||||
str = str + abc[pos - key]
|
||||
loop
|
||||
else
|
||||
if pos = key
|
||||
str = str + char(122)
|
||||
else
|
||||
str = str + abc[len(abc)-(key-pos)]
|
||||
ok
|
||||
ok
|
||||
else
|
||||
str = str + " "
|
||||
ok
|
||||
next
|
||||
return str
|
||||
11
Task/Caesar-cipher/Stata/caesar-cipher.stata
Normal file
11
Task/Caesar-cipher/Stata/caesar-cipher.stata
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
function caesar(s, k) {
|
||||
u = ascii(s)
|
||||
i = selectindex(u:>=65 :& u:<=90)
|
||||
if (length(i)>0) u[i] = mod(u[i]:+(k-65), 26):+65
|
||||
i = selectindex(u:>=97 :& u:<=122)
|
||||
if (length(i)>0) u[i] = mod(u[i]:+(k-97), 26):+97
|
||||
return(char(u))
|
||||
}
|
||||
|
||||
caesar("layout", 20)
|
||||
fusion
|
||||
34
Task/Caesar-cipher/VBA/caesar-cipher.vba
Normal file
34
Task/Caesar-cipher/VBA/caesar-cipher.vba
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
Option Explicit
|
||||
|
||||
Sub Main_Caesar()
|
||||
Dim ch As String
|
||||
ch = Caesar_Cipher("CAESAR: Who is it in the press that calls on me? I hear a tongue, shriller than all the music, Cry 'Caesar!' Speak; Caesar is turn'd to hear.", 14)
|
||||
Debug.Print ch
|
||||
Debug.Print Caesar_Cipher(ch, -14)
|
||||
End Sub
|
||||
|
||||
Function Caesar_Cipher(sText As String, lngNumber As Long) As String
|
||||
Dim Tbl, strGlob As String, strTemp As String, i As Long, bytAscii As Byte
|
||||
Const MAJ As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
Const NB_LETTERS As Byte = 26
|
||||
Const DIFFASCIIMAJ As Byte = 65 - NB_LETTERS
|
||||
Const DIFFASCIIMIN As Byte = 97 - NB_LETTERS
|
||||
|
||||
strTemp = sText
|
||||
If lngNumber < NB_LETTERS And lngNumber > NB_LETTERS * -1 Then
|
||||
strGlob = String(NB_LETTERS * 4, " ")
|
||||
LSet strGlob = MAJ & MAJ & MAJ
|
||||
Tbl = Split(StrConv(strGlob, vbUnicode), Chr(0))
|
||||
For i = 1 To Len(strTemp)
|
||||
If Mid(strTemp, i, 1) Like "[a-zA-Z]" Then
|
||||
bytAscii = Asc(Mid(strTemp, i, 1))
|
||||
If Mid(strTemp, i, 1) = Tbl(bytAscii - DIFFASCIIMAJ) Then
|
||||
Mid(strTemp, i) = Tbl(bytAscii - DIFFASCIIMAJ + lngNumber)
|
||||
Else
|
||||
Mid(strTemp, i) = LCase(Tbl(bytAscii - DIFFASCIIMIN + lngNumber))
|
||||
End If
|
||||
End If
|
||||
Next i
|
||||
End If
|
||||
Caesar_Cipher = strTemp
|
||||
End Function
|
||||
Loading…
Add table
Add a link
Reference in a new issue