September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
23
Task/Caesar-cipher/8th/caesar-cipher.8th
Normal file
23
Task/Caesar-cipher/8th/caesar-cipher.8th
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
\ Ensure the output char is in the correct range:
|
||||
: modulate \ char base -- char
|
||||
tuck n:- 26 n:+ 26 n:mod n:+ ;
|
||||
|
||||
\ Symmetric Caesar cipher. Input is text and number of characters to advance
|
||||
\ (or retreat, if negative). That value should be in the range 1..26
|
||||
: caesar \ intext key -- outext
|
||||
>r
|
||||
(
|
||||
\ Ignore anything below '.' as punctuation:
|
||||
dup '. n:> if
|
||||
\ Do the conversion
|
||||
dup r@ n:+ swap
|
||||
\ Wrap appropriately
|
||||
'A 'Z between if 'A else 'a then modulate
|
||||
then
|
||||
) s:map rdrop ;
|
||||
|
||||
"The five boxing wizards jump quickly!"
|
||||
dup . cr
|
||||
1 caesar dup . cr
|
||||
-1 caesar . cr
|
||||
bye
|
||||
8
Task/Caesar-cipher/Astro/caesar-cipher.astro
Normal file
8
Task/Caesar-cipher/Astro/caesar-cipher.astro
Normal file
|
|
@ -0,0 +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")
|
||||
|
||||
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))
|
||||
21
Task/Caesar-cipher/BaCon/caesar-cipher.bacon
Normal file
21
Task/Caesar-cipher/BaCon/caesar-cipher.bacon
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
CONST lc$ = "abcdefghijklmnopqrstuvwxyz"
|
||||
CONST uc$ = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
|
||||
CONST txt$ = "The quick brown fox jumps over the lazy dog."
|
||||
|
||||
FUNCTION Ceasar$(t$, k)
|
||||
|
||||
lk$ = MID$(lc$ & lc$, k+1, 26)
|
||||
uk$ = MID$(uc$ & uc$, k+1, 26)
|
||||
|
||||
RETURN REPLACE$(t$, lc$ & uc$, lk$ & uk$, 2)
|
||||
|
||||
END FUNCTION
|
||||
|
||||
tokey = RANDOM(25)+1
|
||||
PRINT "Encrypting text with key: ", tokey
|
||||
|
||||
en$ = Ceasar$(txt$, tokey)
|
||||
|
||||
PRINT "Encrypted: ", en$
|
||||
PRINT "Decrypted: ", Ceasar$(en$, 26-tokey)
|
||||
54
Task/Caesar-cipher/C++/caesar-cipher-2.cpp
Normal file
54
Task/Caesar-cipher/C++/caesar-cipher-2.cpp
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
/* caesar cipher */
|
||||
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <cctype>
|
||||
|
||||
int main( ) {
|
||||
|
||||
using namespace std;
|
||||
|
||||
string input ;
|
||||
int key = 0;
|
||||
|
||||
// lambda functions
|
||||
|
||||
auto encrypt = [&](char c, int key ) {
|
||||
char A = ( islower(c) )? 'a': 'A';
|
||||
c = (isalpha(c))? (c - A + key) % 26 + A : c;
|
||||
return (char) c;
|
||||
};
|
||||
|
||||
auto decrypt = [&](char c, int key ) {
|
||||
char A = ( islower(c) )? 'a': 'A';
|
||||
c = (isalpha(c))? (c - A + (26 - key) ) % 26 + A : c;
|
||||
return (char) c;
|
||||
};
|
||||
|
||||
|
||||
cout << "Enter a line of text.\n";
|
||||
getline( cin , input );
|
||||
|
||||
cout << "Enter an integer to shift text.\n";
|
||||
cin >> key;
|
||||
|
||||
while ( (key < 1) || (key > 25) )
|
||||
{
|
||||
cout << "must be an integer between 1 and 25 -->" << endl;
|
||||
cin >> key;
|
||||
}
|
||||
|
||||
cout << "Plain: \t" << input << endl ;
|
||||
|
||||
for ( auto & cp : input) // use & for mutability
|
||||
cp = encrypt(cp, key);
|
||||
|
||||
cout << "Encrypted:\t" << input << endl;
|
||||
|
||||
for ( auto & cp : input)
|
||||
cp = decrypt(cp, key);
|
||||
|
||||
cout << "Decrypted:\t" << input << endl;
|
||||
|
||||
return 0 ;
|
||||
}
|
||||
|
|
@ -1,71 +1,70 @@
|
|||
#define system.
|
||||
#define system'routines.
|
||||
#define system'math.
|
||||
#define extensions.
|
||||
import system'routines.
|
||||
import system'math.
|
||||
import extensions.
|
||||
|
||||
#symbol Letters = "abcdefghijklmnopqrstuvwxyz".
|
||||
#symbol BigLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".
|
||||
#symbol TestText = "Pack my box with five dozen liquor jugs.".
|
||||
#symbol Key = 12.
|
||||
const Letters = "abcdefghijklmnopqrstuvwxyz".
|
||||
const BigLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".
|
||||
const TestText = "Pack my box with five dozen liquor jugs.".
|
||||
const Key = 12.
|
||||
|
||||
#class Encrypting :: Enumerator
|
||||
class Encrypting :: Enumerator
|
||||
{
|
||||
#field theKey.
|
||||
#field theEnumerator.
|
||||
object theKey.
|
||||
object theEnumerator.
|
||||
|
||||
#constructor new &key:aKey &text:aText
|
||||
constructor new key:aKey text:aText
|
||||
[
|
||||
theKey := aKey.
|
||||
theEnumerator := aText enumerator.
|
||||
]
|
||||
|
||||
#method next => theEnumerator.
|
||||
next => theEnumerator.
|
||||
|
||||
#method reset => theEnumerator.
|
||||
reset => theEnumerator.
|
||||
|
||||
#method get
|
||||
get
|
||||
[
|
||||
#var aChar := theEnumerator get.
|
||||
var aChar := theEnumerator get.
|
||||
|
||||
#var anIndex := Letters indexOf:0:aChar.
|
||||
var anIndex := Letters indexOf:aChar at:0.
|
||||
|
||||
(-1 < anIndex)
|
||||
? [
|
||||
^ Letters @ ((theKey+anIndex) mod:26).
|
||||
]
|
||||
! [
|
||||
anIndex := BigLetters indexOf:0:aChar.
|
||||
(-1 < anIndex)
|
||||
? [
|
||||
^ BigLetters @ ((theKey+anIndex) mod:26).
|
||||
]
|
||||
! [
|
||||
^ aChar.
|
||||
if (-1 < anIndex)
|
||||
[
|
||||
^ Letters[(theKey+anIndex) mod:26]
|
||||
];
|
||||
[
|
||||
anIndex := BigLetters indexOf:aChar at:0.
|
||||
if (-1 < anIndex)
|
||||
[
|
||||
^ BigLetters[(theKey+anIndex) mod:26]
|
||||
];
|
||||
[
|
||||
^ aChar
|
||||
].
|
||||
].
|
||||
]
|
||||
}
|
||||
|
||||
#class(extension)encryptOp
|
||||
extension encryptOp
|
||||
{
|
||||
#method encrypt : aKey
|
||||
= Encrypting new &key:aKey &text:self summarize:(String new).
|
||||
encrypt : aKey
|
||||
= Encrypting new key:aKey text:self; summarize(String new).
|
||||
|
||||
#method decrypt :aKey
|
||||
= Encrypting new &key:(26 - aKey) &text:self summarize:(String new).
|
||||
decrypt :aKey
|
||||
= Encrypting new key(26 - aKey) text:self; summarize(String new).
|
||||
}
|
||||
|
||||
#symbol program =
|
||||
program =
|
||||
[
|
||||
console writeLine:"Original text :" :TestText.
|
||||
console printLine("Original text :",TestText).
|
||||
|
||||
#var anEncryptedText := TestText encrypt:Key.
|
||||
var anEncryptedText := TestText encrypt:Key.
|
||||
|
||||
console writeLine:"Encrypted text:" :anEncryptedText.
|
||||
console printLine("Encrypted text:",anEncryptedText).
|
||||
|
||||
#var aDecryptedText := anEncryptedText decrypt:Key.
|
||||
var aDecryptedText := anEncryptedText decrypt:Key.
|
||||
|
||||
console writeLine:"Decrypted text:" :aDecryptedText.
|
||||
console printLine("Decrypted text:",aDecryptedText).
|
||||
|
||||
console readChar.
|
||||
].
|
||||
|
|
|
|||
20
Task/Caesar-cipher/Gambas/caesar-cipher.gambas
Normal file
20
Task/Caesar-cipher/Gambas/caesar-cipher.gambas
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
Public Sub Main()
|
||||
Dim byKey As Byte = 3 'The key (Enter 26 to get the same output as input)
|
||||
Dim byCount As Byte 'Counter
|
||||
Dim sCeasar As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ" 'Used to calculate the cipher
|
||||
Dim sString As String = "The five boxing wizards jump quickly" 'Phrase to encrypt
|
||||
Dim sCoded, sTemp As String 'Various strings
|
||||
|
||||
For byCount = 1 To Len(sString) 'Count through each letter in the phrase
|
||||
If Mid(sString, byCount, 1) = " " Then 'If it's a space..
|
||||
sCoded &= " " 'Keep it a space
|
||||
Continue 'Jump to the next iteration of the loop
|
||||
Endif
|
||||
sTemp = Mid(sCeasar, InStr(sCeasar, Mid(UCase(sString), byCount, 1)) + byKey, 1) 'Get the new 'coded' letter
|
||||
If Asc(Mid(sString, byCount, 1)) > 96 Then sTemp = Chr(Asc(sTemp) + 32) 'If the original was lower case then make the new 'coded' letter lower case
|
||||
sCoded &= sTemp 'Add the result to the code string
|
||||
Next
|
||||
|
||||
Print sString & gb.NewLine & sCoded 'Print the result
|
||||
|
||||
End
|
||||
|
|
@ -1,32 +1,23 @@
|
|||
{-# LANGUAGE LambdaCase #-}
|
||||
module Main where
|
||||
import Data.Char (ord, chr, isUpper, isAlpha)
|
||||
|
||||
import Control.Error (tryRead, tryAt)
|
||||
import Control.Monad.Trans (liftIO)
|
||||
import Control.Monad.Trans.Except (ExceptT, runExceptT)
|
||||
caesar, uncaesar :: Int -> String -> String
|
||||
caesar = (<$>) . tr
|
||||
|
||||
import Data.Char
|
||||
import System.Exit (die)
|
||||
import System.Environment (getArgs)
|
||||
uncaesar = caesar . negate
|
||||
|
||||
tr :: Int -> Char -> Char
|
||||
tr offset c
|
||||
| isAlpha c = chr $ intAlpha + mod ((ord c - intAlpha) + offset) 26
|
||||
| otherwise = c
|
||||
where
|
||||
intAlpha =
|
||||
ord
|
||||
(if isUpper c
|
||||
then 'A'
|
||||
else 'a')
|
||||
|
||||
main :: IO ()
|
||||
main = runExceptT parseKey >>= \case
|
||||
Left err -> die err
|
||||
Right k -> interact $ caesar k
|
||||
|
||||
parseKey :: (Read a, Integral a) => ExceptT String IO a
|
||||
parseKey = liftIO getArgs >>=
|
||||
flip (tryAt "Not enough arguments") 0 >>=
|
||||
tryRead "Key is not a valid integer"
|
||||
|
||||
caesar :: (Integral a) => a -> String -> String
|
||||
caesar k = map f
|
||||
where f c = case generalCategory c of
|
||||
LowercaseLetter -> addChar 'a' k c
|
||||
UppercaseLetter -> addChar 'A' k c
|
||||
_ -> c
|
||||
|
||||
addChar :: (Integral a) => Char -> a -> Char -> Char
|
||||
addChar b o c = chr $ fromIntegral (b' + (c' - b' + o) `mod` 26)
|
||||
where b' = fromIntegral $ ord b
|
||||
c' = fromIntegral $ ord c
|
||||
main = mapM_ print [encoded, decoded]
|
||||
where
|
||||
encoded = caesar (-114) "Curio, Cesare venne, e vide e vinse ? "
|
||||
decoded = uncaesar (-114) encoded
|
||||
|
|
|
|||
32
Task/Caesar-cipher/Haskell/caesar-cipher-3.hs
Normal file
32
Task/Caesar-cipher/Haskell/caesar-cipher-3.hs
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
{-# LANGUAGE LambdaCase #-}
|
||||
module Main where
|
||||
|
||||
import Control.Error (tryRead, tryAt)
|
||||
import Control.Monad.Trans (liftIO)
|
||||
import Control.Monad.Trans.Except (ExceptT, runExceptT)
|
||||
|
||||
import Data.Char
|
||||
import System.Exit (die)
|
||||
import System.Environment (getArgs)
|
||||
|
||||
main :: IO ()
|
||||
main = runExceptT parseKey >>= \case
|
||||
Left err -> die err
|
||||
Right k -> interact $ caesar k
|
||||
|
||||
parseKey :: (Read a, Integral a) => ExceptT String IO a
|
||||
parseKey = liftIO getArgs >>=
|
||||
flip (tryAt "Not enough arguments") 0 >>=
|
||||
tryRead "Key is not a valid integer"
|
||||
|
||||
caesar :: (Integral a) => a -> String -> String
|
||||
caesar k = map f
|
||||
where f c = case generalCategory c of
|
||||
LowercaseLetter -> addChar 'a' k c
|
||||
UppercaseLetter -> addChar 'A' k c
|
||||
_ -> c
|
||||
|
||||
addChar :: (Integral a) => Char -> a -> Char -> Char
|
||||
addChar b o c = chr $ fromIntegral (b' + (c' - b' + o) `mod` 26)
|
||||
where b' = fromIntegral $ ord b
|
||||
c' = fromIntegral $ ord c
|
||||
35
Task/Caesar-cipher/Kotlin/caesar-cipher.kotlin
Normal file
35
Task/Caesar-cipher/Kotlin/caesar-cipher.kotlin
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
// version 1.0.5-2
|
||||
|
||||
object Caesar {
|
||||
fun encrypt(s: String, key: Int): String {
|
||||
val offset = key % 26
|
||||
if (offset == 0) return s
|
||||
var d: Char
|
||||
val chars = CharArray(s.length)
|
||||
for ((index, c) in s.withIndex()) {
|
||||
if (c in 'A'..'Z') {
|
||||
d = c + offset
|
||||
if (d > 'Z') d -= 26
|
||||
}
|
||||
else if (c in 'a'..'z') {
|
||||
d = c + offset
|
||||
if (d > 'z') d -= 26
|
||||
}
|
||||
else
|
||||
d = c
|
||||
chars[index] = d
|
||||
}
|
||||
return chars.joinToString("")
|
||||
}
|
||||
|
||||
fun decrypt(s: String, key: Int): String {
|
||||
return encrypt(s, 26 - key)
|
||||
}
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val encoded = Caesar.encrypt("Bright vixens jump; dozy fowl quack.", 8)
|
||||
println(encoded)
|
||||
val decoded = Caesar.decrypt(encoded, 8)
|
||||
println(decoded)
|
||||
}
|
||||
|
|
@ -1,19 +0,0 @@
|
|||
#lang racket
|
||||
|
||||
(define A (char->integer #\A))
|
||||
(define Z (char->integer #\Z))
|
||||
(define a (char->integer #\a))
|
||||
(define z (char->integer #\z))
|
||||
|
||||
(define (rotate c n)
|
||||
(define cnum (char->integer c))
|
||||
(define (shift base) (integer->char (+ base (modulo (+ n (- cnum base)) 26))))
|
||||
(cond [(<= A cnum Z) (shift A)]
|
||||
[(<= a cnum z) (shift a)]
|
||||
[else c]))
|
||||
|
||||
(define (caesar s n)
|
||||
(list->string (for/list ([c (in-string s)]) (rotate c n))))
|
||||
|
||||
(define (encrypt s) (caesar s 1))
|
||||
(define (decrypt s) (caesar s -1))
|
||||
|
|
@ -1,5 +0,0 @@
|
|||
> (define s (encrypt "The five boxing wizards jump quickly."))
|
||||
> s
|
||||
"Uif gjwf cpyjoh xjabset kvnq rvjdlmz."
|
||||
> (decrypt s)
|
||||
"The five boxing wizards jump quickly."
|
||||
|
|
@ -1,27 +1,12 @@
|
|||
module CaesarCipher
|
||||
AtoZ = (0..25).each_with_object({}) do |key,h|
|
||||
str = [*"A".."Z"].rotate(key).join
|
||||
h[key] = str + str.downcase
|
||||
class String
|
||||
ALFABET = ("A".."Z").to_a
|
||||
|
||||
def caesar_cipher(num)
|
||||
self.tr(ALFABET.join, ALFABET.rotate(num).join)
|
||||
end
|
||||
|
||||
def encrypt(key, plaintext)
|
||||
(1..25) === key or raise ArgumentError, "key not in 1..25"
|
||||
plaintext.tr(AtoZ[0], AtoZ[key])
|
||||
end
|
||||
|
||||
def decrypt(key, ciphertext)
|
||||
(1..25) === key or raise ArgumentError, "key not in 1..25"
|
||||
ciphertext.tr(AtoZ[key], AtoZ[0])
|
||||
end
|
||||
end
|
||||
|
||||
include CaesarCipher
|
||||
|
||||
original = "THEYBROKEOURCIPHEREVERYONECANREADTHIS"
|
||||
en = encrypt(3, original)
|
||||
de = decrypt(3, en)
|
||||
|
||||
[original, en, de].each {|e| puts e}
|
||||
|
||||
puts 'OK' if
|
||||
(1..25).all? {|k| original == decrypt(k, encrypt(k, original))}
|
||||
#demo:
|
||||
encypted = "THEYBROKEOURCIPHEREVERYONECANREADTHIS".caesar_cipher(3)
|
||||
decrypted = encypted.caesar_cipher(-3)
|
||||
|
|
|
|||
37
Task/Caesar-cipher/Sed/caesar-cipher.sed
Normal file
37
Task/Caesar-cipher/Sed/caesar-cipher.sed
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
#!/bin/sed -rf
|
||||
# Input: <number 0..25>\ntext to encode
|
||||
|
||||
/^[0-9]+$/ {
|
||||
# validate a number and translate it to analog form
|
||||
s/$/;9876543210dddddddddd/
|
||||
s/([0-9]);.*\1.{10}(.?)/\2/
|
||||
s/2/11/
|
||||
s/1/dddddddddd/g
|
||||
/[3-9]|d{25}d+/ {
|
||||
s/.*/Error: Key must be <= 25/
|
||||
q
|
||||
}
|
||||
# append from-table
|
||||
s/$/\nabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ/
|
||||
# .. and to-table
|
||||
s/$/abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ/
|
||||
# rotate to-table, lower and uppercase independently, removing one `d' at a time
|
||||
: rotate
|
||||
s/^d(.*\n[^Z]+Z)(.)(.{25})(.)(.{25})/\1\3\2\5\4/
|
||||
t rotate
|
||||
s/\n//
|
||||
h
|
||||
d
|
||||
}
|
||||
|
||||
# use \n to mark character to convert
|
||||
s/^/\n/
|
||||
# append conversion table to pattern space
|
||||
G
|
||||
: loop
|
||||
# look up converted character and place it instead of old one
|
||||
s/\n(.)(.*\n.*\1.{51}(.))/\n\3\2/
|
||||
# advance \n even if prev. command fails, thus skip non-alphabetical characters
|
||||
/\n\n/! s/\n([^\n])/\1\n/
|
||||
t loop
|
||||
s/\n\n.*//
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
10 INPUT KEY
|
||||
20 INPUT T$
|
||||
30 LET C$=""
|
||||
40 FOR I=1 TO LEN T$
|
||||
50 LET L$=T$(I)
|
||||
60 IF L$<"A" OR L$>"Z" THEN GOTO 100
|
||||
70 LET L$=CHR$ (CODE L$+KEY)
|
||||
80 IF L$>"Z" THEN LET L$=CHR$ (CODE L$-26)
|
||||
90 IF L$<"A" THEN LET L$=CHR$ (CODE L$+26)
|
||||
100 LET C$=C$+L$
|
||||
110 NEXT I
|
||||
120 PRINT C$
|
||||
65
Task/Caesar-cipher/Swift/caesar-cipher.swift
Normal file
65
Task/Caesar-cipher/Swift/caesar-cipher.swift
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
var arr:[Character]=["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"]
|
||||
|
||||
func res(st:String,ar:[Character],x:Int,ro:String)->String{
|
||||
|
||||
var str2:[Character]=[]
|
||||
|
||||
for i in st.characters
|
||||
{
|
||||
|
||||
for j in 0...25
|
||||
{
|
||||
|
||||
|
||||
if i==ar[j]
|
||||
{
|
||||
|
||||
switch ro
|
||||
{
|
||||
case "right":
|
||||
|
||||
if(j+x<=25)
|
||||
{
|
||||
|
||||
str2.append(ar[j+x])
|
||||
break
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
str2.append(ar[j+x-26])
|
||||
break
|
||||
}
|
||||
|
||||
case "left":
|
||||
|
||||
if(j-x>=0)
|
||||
{
|
||||
|
||||
str2.append(ar[j-x])
|
||||
break
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
str2.append(ar[j-x+26])
|
||||
break
|
||||
}
|
||||
default:
|
||||
print("incorrect input for rotation direction")
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return String(str2)
|
||||
}
|
||||
|
||||
var mssg:String="hi"
|
||||
var x1:Int=5
|
||||
var rot:String="right"
|
||||
var rotstr:String=res(st:mssg,ar:arr,x:x1,ro:rot)
|
||||
print(rotstr)
|
||||
35
Task/Caesar-cipher/VBScript/caesar-cipher.vb
Normal file
35
Task/Caesar-cipher/VBScript/caesar-cipher.vb
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
str = "IT WAS THE BEST OF TIMES, IT WAS THE WORST OF TIMES."
|
||||
|
||||
Wscript.Echo str
|
||||
Wscript.Echo Rotate(str,5)
|
||||
Wscript.Echo Rotate(Rotate(str,5),-5)
|
||||
|
||||
'Rotate (Caesar encrypt/decrypt) test <numpos> positions.
|
||||
' numpos < 0 - rotate left
|
||||
' numpos > 0 - rotate right
|
||||
'Left rotation is converted to equivalent right rotation
|
||||
|
||||
Function Rotate (text, numpos)
|
||||
|
||||
dim dic: set dic = CreateObject("Scripting.Dictionary")
|
||||
dim ltr: ltr = Split("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")
|
||||
dim rot: rot = (26 + numpos Mod 26) Mod 26 'convert all to right rotation
|
||||
dim ch
|
||||
dim i
|
||||
|
||||
for i = 0 to ubound(ltr)
|
||||
dic(ltr(i)) = ltr((rot+i) Mod 26)
|
||||
next
|
||||
|
||||
Rotate = ""
|
||||
|
||||
for i = 1 to Len(text)
|
||||
ch = Mid(text,i,1)
|
||||
if dic.Exists(ch) Then
|
||||
Rotate = Rotate & dic(ch)
|
||||
else
|
||||
Rotate = Rotate & ch
|
||||
end if
|
||||
next
|
||||
|
||||
End Function
|
||||
33
Task/Caesar-cipher/Vala/caesar-cipher.vala
Normal file
33
Task/Caesar-cipher/Vala/caesar-cipher.vala
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
static void println(string str) {
|
||||
stdout.printf("%s\r\n", str);
|
||||
}
|
||||
|
||||
static unichar encrypt_char(unichar ch, int code) {
|
||||
if (!ch.isalpha()) return ch;
|
||||
|
||||
unichar offset = ch.isupper() ? 'A' : 'a';
|
||||
return (unichar)((ch + code - offset) % 26 + offset);
|
||||
}
|
||||
|
||||
static string encrypt(string input, int code) {
|
||||
var builder = new StringBuilder();
|
||||
|
||||
unichar c;
|
||||
for (int i = 0; input.get_next_char(ref i, out c);) {
|
||||
builder.append_unichar(encrypt_char(c, code));
|
||||
}
|
||||
|
||||
return builder.str;
|
||||
}
|
||||
|
||||
static string decrypt(string input, int code) {
|
||||
return encrypt(input, 26 - code);
|
||||
}
|
||||
|
||||
const string test_case = "The quick brown fox jumped over the lazy dog";
|
||||
|
||||
void main() {
|
||||
println(test_case);
|
||||
println(encrypt(test_case, -1));
|
||||
println(decrypt(encrypt(test_case, -1), -1));
|
||||
}
|
||||
7
Task/Caesar-cipher/Zkl/caesar-cipher-1.zkl
Normal file
7
Task/Caesar-cipher/Zkl/caesar-cipher-1.zkl
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
fcn caesarCodec(str,n,encode=True){
|
||||
var [const] letters=["a".."z"].chain(["A".."Z"]).pump(String); // static
|
||||
if(not encode) n=26 - n;
|
||||
m,sz := n + 26, 26 - n;
|
||||
ltrs:=String(letters[n,sz],letters[0,n],letters[m,sz],letters[26,n]);
|
||||
str.translate(letters,ltrs)
|
||||
}
|
||||
6
Task/Caesar-cipher/Zkl/caesar-cipher-2.zkl
Normal file
6
Task/Caesar-cipher/Zkl/caesar-cipher-2.zkl
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
text:="The five boxing wizards jump quickly";
|
||||
N:=3;
|
||||
code:=caesarCodec(text,N);
|
||||
println("text = ",text);
|
||||
println("encoded(%d) = %s".fmt(N,code));
|
||||
println("decoded = ",caesarCodec(code,N,False));
|
||||
Loading…
Add table
Add a link
Reference in a new issue