Add tasks for all the new languages
This commit is contained in:
parent
9dc3c2bb62
commit
bba7bfd280
13208 changed files with 134745 additions and 0 deletions
29
Task/Caesar-cipher/ERRE/caesar-cipher.erre
Normal file
29
Task/Caesar-cipher/ERRE/caesar-cipher.erre
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
PROGRAM CAESAR
|
||||
|
||||
!$INCLUDE="PC.LIB"
|
||||
|
||||
PROCEDURE CAESAR(TEXT$,KY%->CY$)
|
||||
LOCAL I%,C%
|
||||
FOR I%=1 TO LEN(TEXT$) DO
|
||||
C%=ASC(MID$(TEXT$,I%))
|
||||
IF (C% AND $1F)>=1 AND (C% AND $1F)<=26 THEN
|
||||
C%=(C% AND $E0) OR (((C% AND $1F)+KY%-1) MOD 26+1)
|
||||
CHANGE(TEXT$,I%,CHR$(C%)->TEXT$)
|
||||
END IF
|
||||
END FOR
|
||||
CY$=TEXT$
|
||||
END PROCEDURE
|
||||
|
||||
BEGIN
|
||||
RANDOMIZE(TIMER)
|
||||
PLAINTEXT$="Pack my box with five dozen liquor jugs"
|
||||
PRINT(PLAINTEXT$)
|
||||
|
||||
KY%=1+INT(25*RND(1)) ! generates random between 1 and 25
|
||||
CAESAR(PLAINTEXT$,KY%->CYPHERTEXT$)
|
||||
PRINT(CYPHERTEXT$)
|
||||
|
||||
CAESAR(CYPHERTEXT$,26-KY%->DECYPHERED$)
|
||||
PRINT(DECYPHERED$)
|
||||
|
||||
END PROGRAM
|
||||
41
Task/Caesar-cipher/FreeBASIC/caesar-cipher.freebasic
Normal file
41
Task/Caesar-cipher/FreeBASIC/caesar-cipher.freebasic
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
' FB 1.05.0 Win64
|
||||
|
||||
Sub Encrypt(s As String, key As Integer)
|
||||
Dim c As Integer
|
||||
For i As Integer = 0 To Len(s)
|
||||
Select Case As Const s[i]
|
||||
Case 65 To 90
|
||||
c = s[i] + key
|
||||
If c > 90 Then c -= 26
|
||||
s[i] = c
|
||||
Case 97 To 122
|
||||
c = s[i] + key
|
||||
If c > 122 Then c -= 26
|
||||
s[i] = c
|
||||
End Select
|
||||
Next
|
||||
End Sub
|
||||
|
||||
Sub Decrypt(s As String, key As Integer)
|
||||
Dim c As Integer
|
||||
For i As Integer = 0 To Len(s)
|
||||
Select Case As Const s[i]
|
||||
Case 65 To 90
|
||||
c = s[i] - key
|
||||
If c < 65 Then c += 26
|
||||
s[i] = c
|
||||
Case 97 To 122
|
||||
c = s[i] - key
|
||||
If c < 97 Then c += 26
|
||||
s[i] = c
|
||||
End Select
|
||||
Next
|
||||
End Sub
|
||||
|
||||
Dim As String s = "Bright vixens jump; dozy fowl quack."
|
||||
Print "Plain text : "; s
|
||||
Encrypt s, 8
|
||||
Print "Encrypted : "; s
|
||||
Decrypt s, 8
|
||||
Print "Decrypted : "; s
|
||||
Sleep
|
||||
36
Task/Caesar-cipher/GFA-Basic/caesar-cipher.gfa
Normal file
36
Task/Caesar-cipher/GFA-Basic/caesar-cipher.gfa
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
'
|
||||
' Caesar cypher
|
||||
'
|
||||
OPENW 1 ! Creates a window for handling input/output
|
||||
CLEARW 1
|
||||
INPUT "string to encrypt ";text$
|
||||
INPUT "encryption key ";key%
|
||||
encrypted$=@encrypt$(UPPER$(text$),key%)
|
||||
PRINT "Encrypted: ";encrypted$
|
||||
PRINT "Decrypted: ";@decrypt$(encrypted$,key%)
|
||||
'
|
||||
PRINT "(Press any key to end program.)"
|
||||
~INP(2)
|
||||
CLOSEW 1
|
||||
'
|
||||
FUNCTION encrypt$(text$,key%)
|
||||
LOCAL result$,i%,c%
|
||||
result$=""
|
||||
FOR i%=1 TO LEN(text$)
|
||||
c%=ASC(MID$(text$,i%))
|
||||
IF c%<ASC("A") OR c%>ASC("Z") ! don't encrypt non A-Z
|
||||
result$=result$+CHR$(c%)
|
||||
ELSE
|
||||
c%=c%+key%
|
||||
IF c%>ASC("Z")
|
||||
c%=c%-26
|
||||
ENDIF
|
||||
result$=result$+CHR$(c%)
|
||||
ENDIF
|
||||
NEXT i%
|
||||
RETURN result$
|
||||
ENDFUNC
|
||||
'
|
||||
FUNCTION decrypt$(text$,key%)
|
||||
RETURN @encrypt$(text$,26-key%)
|
||||
ENDFUNC
|
||||
16
Task/Caesar-cipher/LiveCode/caesar-cipher.livecode
Normal file
16
Task/Caesar-cipher/LiveCode/caesar-cipher.livecode
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
function caesarCipher rot phrase
|
||||
local rotPhrase, lowerLetters, upperLetters
|
||||
put "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz" into lowerLetters
|
||||
put "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ" into upperLetters
|
||||
repeat for each char letter in phrase
|
||||
get charTonum(letter)
|
||||
if it >= 65 and it <= 90 then
|
||||
put char ((it + rot) - 64) of upperLetters after rotPhrase
|
||||
else if it >= 97 and it <= 122 then
|
||||
put char ((it + rot) - 96) of lowerLetters after rotPhrase
|
||||
else
|
||||
put letter after rotPhrase
|
||||
end if
|
||||
end repeat
|
||||
return rotPhrase
|
||||
end caesarCipher
|
||||
14
Task/Caesar-cipher/Nim/caesar-cipher.nim
Normal file
14
Task/Caesar-cipher/Nim/caesar-cipher.nim
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
import strutils
|
||||
|
||||
proc caesar(s: string, k: int, decode = false): string =
|
||||
var k = if decode: 26 - k else: k
|
||||
result = ""
|
||||
for i in toUpper(s):
|
||||
if ord(i) >= 65 and ord(i) <= 90:
|
||||
result.add(chr((ord(i) - 65 + k) mod 26 + 65))
|
||||
|
||||
let msg = "The quick brown fox jumped over the lazy dogs"
|
||||
echo msg
|
||||
let enc = caesar(msg, 11)
|
||||
echo enc
|
||||
echo caesar(enc, 11, decode = true)
|
||||
6
Task/Caesar-cipher/Oforth/caesar-cipher.oforth
Normal file
6
Task/Caesar-cipher/Oforth/caesar-cipher.oforth
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
: ceasar(c, key)
|
||||
c dup isLetter ifFalse: [ return ]
|
||||
isUpper ifTrue: [ 'A' ] else: [ 'a' ] c key + over - 26 mod + ;
|
||||
|
||||
: cipherE(s, key) s map(#[ key ceasar ]) charsAsString ;
|
||||
: cipherD(s, key) cipherE(s, 26 key - ) ;
|
||||
19
Task/Caesar-cipher/Phix/caesar-cipher.phix
Normal file
19
Task/Caesar-cipher/Phix/caesar-cipher.phix
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
sequence alpha_b = repeat(0,255)
|
||||
alpha_b['A'..'Z'] = 'A'
|
||||
alpha_b['a'..'z'] = 'a'
|
||||
|
||||
function caesar(string s, integer key)
|
||||
integer ch, base
|
||||
for i=1 to length(s) do
|
||||
ch = s[i]
|
||||
base = alpha_b[ch]
|
||||
if base then
|
||||
s[i] = base+remainder(ch-base+key,26)
|
||||
end if
|
||||
end for
|
||||
return s
|
||||
end function
|
||||
string s = "One fine day in the middle of the night, two dead men got up to fight. \n"&
|
||||
"Back to back they faced each other, drew their swords and shot each other. %^&*()[",
|
||||
e = caesar(s,5),
|
||||
r = caesar(e,26-5) ?e ?r
|
||||
19
Task/Caesar-cipher/SSEM/caesar-cipher.ssem
Normal file
19
Task/Caesar-cipher/SSEM/caesar-cipher.ssem
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
00101000000000100000000000000000 0. -20 to c
|
||||
11001000000000010000000000000000 1. Sub. 19
|
||||
10101000000001100000000000000000 2. c to 21
|
||||
10101000000000100000000000000000 3. -21 to c
|
||||
00000000000000110000000000000000 4. Test
|
||||
10001000000000000000000000000000 5. 17 to CI
|
||||
10101000000001100000000000000000 6. c to 21
|
||||
10101000000000100000000000000000 7. -21 to c
|
||||
01001000000000010000000000000000 8. Sub. 18
|
||||
10101000000001100000000000000000 9. c to 21
|
||||
10101000000000100000000000000000 10. -21 to c
|
||||
00000000000001110000000000000000 11. Stop
|
||||
01001000000000010000000000000000 12. Sub. 18
|
||||
00000000000000110000000000000000 13. Test
|
||||
00000000000001110000000000000000 14. Stop
|
||||
10101000000000100000000000000000 15. -21 to c
|
||||
00000000000001110000000000000000 16. Stop
|
||||
11010000000000000000000000000000 17. 11
|
||||
01011000000000000000000000000000 18. 26
|
||||
32
Task/Caesar-cipher/SequenceL/caesar-cipher.sequencel
Normal file
32
Task/Caesar-cipher/SequenceL/caesar-cipher.sequencel
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
import <Utilities/Sequence.sl>;
|
||||
import <Utilities/Conversion.sl>;
|
||||
|
||||
lowerAlphabet := "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
upperAlphabet := "abcdefghijklmnopqrstuvwxyz";
|
||||
|
||||
caesarEncrypt(ch, key) :=
|
||||
let
|
||||
correctAlphabet :=
|
||||
lowerAlphabet when some(ch = lowerAlphabet)
|
||||
else
|
||||
upperAlphabet;
|
||||
|
||||
index := Sequence::firstIndexOf(correctAlphabet, ch);
|
||||
|
||||
newIndex := (index + key - 1) mod 26 + 1;
|
||||
in
|
||||
ch when not(some(ch = lowerAlphabet) or some(ch = upperAlphabet))
|
||||
else
|
||||
correctAlphabet[newIndex];
|
||||
|
||||
caesarDecrypt(ch, key) := caesarEncrypt(ch, 26 - key);
|
||||
|
||||
main(args(2)) :=
|
||||
let
|
||||
key := Conversion::stringToInt(args[2]);
|
||||
encrypted := caesarEncrypt(args[1], key);
|
||||
decrypted := caesarDecrypt(encrypted, key);
|
||||
in
|
||||
"Input: \t" ++ args[1] ++ "\n" ++
|
||||
"Encrypted:\t" ++ encrypted ++ "\n" ++
|
||||
"Decrypted:\t" ++ decrypted;
|
||||
13
Task/Caesar-cipher/Sidef/caesar-cipher.sidef
Normal file
13
Task/Caesar-cipher/Sidef/caesar-cipher.sidef
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
func caesar(msg, key, decode=false) {
|
||||
decode && (key = (26 - key));
|
||||
msg.gsub(/([A-Z])/i, {|c| ((c.uc.ord - 65 + key) % 26) + 65 -> chr});
|
||||
};
|
||||
|
||||
var msg = 'THE FIVE BOXING WIZARDS JUMP QUICKLY';
|
||||
|
||||
var enc = caesar(msg, 10);
|
||||
var dec = caesar(enc, 10, true);
|
||||
|
||||
say "msg: #{msg}";
|
||||
say "enc: #{enc}";
|
||||
say "dec: #{dec}";
|
||||
14
Task/Caesar-cipher/TypeScript/caesar-cipher.type
Normal file
14
Task/Caesar-cipher/TypeScript/caesar-cipher.type
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
function replace(input: string, key: number) : string {
|
||||
return input.replace(/([a-z])/g,
|
||||
($1) => String.fromCharCode(($1.charCodeAt(0) + key + 26 - 97) % 26 + 97)
|
||||
).replace(/([A-Z])/g,
|
||||
($1) => String.fromCharCode(($1.charCodeAt(0) + key + 26 - 65) % 26 + 65));
|
||||
}
|
||||
|
||||
// test
|
||||
var str = 'The five boxing wizards jump quickly';
|
||||
var encoded = replace(str, 3);
|
||||
var decoded = replace(encoded, -3);
|
||||
|
||||
console.log('Enciphered: ' + encoded);
|
||||
console.log('Deciphered: ' + decoded);
|
||||
28
Task/Caesar-cipher/Ursa/caesar-cipher.ursa
Normal file
28
Task/Caesar-cipher/Ursa/caesar-cipher.ursa
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
decl string mode
|
||||
while (not (or (= mode "encode") (= mode "decode")))
|
||||
out "encode/decode: " console
|
||||
set mode (lower (in string console))
|
||||
end while
|
||||
|
||||
decl string message
|
||||
out "message: " console
|
||||
set message (upper (in string console))
|
||||
|
||||
decl int key
|
||||
out "key: " console
|
||||
set key (in int console)
|
||||
if (or (> key 26) (< key 0))
|
||||
out endl "invalid key" endl console
|
||||
stop
|
||||
end if
|
||||
|
||||
if (= mode "decode")
|
||||
set key (int (- 26 key))
|
||||
end if
|
||||
|
||||
for (decl int i) (< i (size message)) (inc i)
|
||||
if (and (> (ord message<i>) 64) (< (ord message<i>) 91))
|
||||
out (chr (int (+ (mod (int (+ (- (ord message<i>) 65) key)) 26) 65))) console
|
||||
end if
|
||||
end for
|
||||
out endl console
|
||||
18
Task/Caesar-cipher/Wortel/caesar-cipher.wortel
Normal file
18
Task/Caesar-cipher/Wortel/caesar-cipher.wortel
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
@let {
|
||||
; this function only replaces letters and keeps case
|
||||
ceasar &[s n] !!s.replace &"[a-z]"gi &[x] [
|
||||
@vars {
|
||||
t x.charCodeAt.
|
||||
l ?{
|
||||
&& > t 96 < t 123 97
|
||||
&& > t 64 < t 91 65
|
||||
0
|
||||
}
|
||||
}
|
||||
!String.fromCharCode ?{
|
||||
l +l ~% 26 -+ n t l
|
||||
t
|
||||
}
|
||||
]
|
||||
!!ceasar "abc $%^ ABC" 10
|
||||
}
|
||||
16
Task/Caesar-cipher/XLISP/caesar-cipher-1.xlisp
Normal file
16
Task/Caesar-cipher/XLISP/caesar-cipher-1.xlisp
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
(defun caesar-encode (text key)
|
||||
(defun encode (ascii-code)
|
||||
(defun rotate (character alphabet)
|
||||
(define code (+ character key))
|
||||
(cond
|
||||
((> code (+ alphabet 25)) (- code 26))
|
||||
((< code alphabet) (+ code 26))
|
||||
(t code)))
|
||||
(cond
|
||||
((and (>= ascii-code 65) (<= ascii-code 90)) (rotate ascii-code 65))
|
||||
((and (>= ascii-code 97) (<= ascii-code 122)) (rotate ascii-code 97))
|
||||
(t ascii-code)))
|
||||
(list->string (mapcar integer->char (mapcar encode (mapcar char->integer (string->list text))))))
|
||||
|
||||
(defun caesar-decode (text key)
|
||||
(caesar-encode text (- 26 key)))
|
||||
9
Task/Caesar-cipher/XLISP/caesar-cipher-2.xlisp
Normal file
9
Task/Caesar-cipher/XLISP/caesar-cipher-2.xlisp
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
[1] (define caesar-test (caesar-encode "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))
|
||||
|
||||
CAESAR-TEST
|
||||
[2] caesar-test
|
||||
|
||||
"QOSGOF: Kvc wg wh wb hvs dfsgg hvoh qozzg cb as? W vsof o hcbuis, gvfwzzsf hvob ozz hvs aigwq, Qfm 'Qosgof!' Gdsoy; Qosgof wg hifb'r hc vsof."
|
||||
[3] (caesar-decode caesar-test 14)
|
||||
|
||||
"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."
|
||||
Loading…
Add table
Add a link
Reference in a new issue