Add tasks for all the new languages

This commit is contained in:
Tina Müller 2016-12-05 23:44:36 +01:00
parent 9dc3c2bb62
commit bba7bfd280
13208 changed files with 134745 additions and 0 deletions

View file

@ -0,0 +1,28 @@
import strutils
proc isAlpha(c): bool = c in 'a'..'z' or c in 'A'..'Z'
proc encrypt(msg, key): string =
result = ""
var pos = 0
for c in msg:
if isAlpha c:
result.add chr(((ord(key[pos]) + ord(toUpper c)) mod 26) + ord('A'))
pos = (pos + 1) mod key.len
proc decrypt(msg, key): string =
result = ""
var pos = 0
for c in msg:
result.add chr(((26 + ord(c) - ord(key[pos])) mod 26) + ord('A'))
pos = (pos + 1) mod key.len
const text = "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!"
const key = "VIGENERECIPHER"
let encr = encrypt(text, key)
let decr = decrypt(encr, key)
echo text
echo encr
echo decr

View file

@ -0,0 +1,12 @@
func s2v(s) { s.uc.scan(/[A-Z]/)»ord»() »-» 65 };
func v2s(v) { (v »%» 26 »+» 65)»chr»().join };
func blacken (red, key) { v2s(s2v(red) »+« s2v(key)) };
func redden (blk, key) { v2s(s2v(blk) »-« s2v(key)) };
var red = "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!";
var key = "Vigenere Cipher!!!";
say red;
say (var black = blacken(red, key));
say redden(black, key);

View file

@ -0,0 +1,47 @@
class Vigenere {
key: string
/** Create new cipher based on key */
constructor(key: string) {
this.key = Vigenere.formatText(key)
}
/** Enrypt a given text using key */
encrypt(plainText: string): string {
return Array.prototype.map.call(Vigenere.formatText(plainText), (letter: string, index: number): string => {
return String.fromCharCode((letter.charCodeAt(0) + this.key.charCodeAt(index % this.key.length) - 130) % 26 + 65)
}).join('')
}
/** Decrypt ciphertext based on key */
decrypt(cipherText: string): string {
return Array.prototype.map.call(Vigenere.formatText(cipherText), (letter: string, index: number): string => {
return String.fromCharCode((letter.charCodeAt(0) - this.key.charCodeAt(index % this.key.length) + 26) % 26 + 65)
}).join('')
}
/** Converts to uppercase and removes non characters */
private static formatText(text: string): string {
return text.toUpperCase().replace(/[^A-Z]/g, "")
}
}
/** Example usage */
(() => {
let original: string = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book."
console.log(`Original: ${original}`)
let vig: Vigenere = new Vigenere("vigenere")
let encoded: string = vig.encrypt(original)
console.log(`After encryption: ${encoded}`)
let back: string = vig.decrypt(encoded)
console.log(`After decryption: ${back}`)
})()