Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
28
Task/Caesar-cipher/Janet/caesar-cipher.janet
Normal file
28
Task/Caesar-cipher/Janet/caesar-cipher.janet
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
(def alphabet "abcdefghijklmnopqrstuvwxyz")
|
||||
|
||||
(defn rotate
|
||||
"shift bytes given x"
|
||||
[str x]
|
||||
(let [r (% x (length alphabet))
|
||||
b (string/bytes str)
|
||||
abyte (get (string/bytes "a") 0)
|
||||
zbyte (get (string/bytes "z") 0)]
|
||||
(var result @[])
|
||||
(for i 0 (length b)
|
||||
(let [ch (bor (get b i) 0x20)] # convert uppercase to lowercase
|
||||
(when (and (<= abyte ch) (<= ch zbyte))
|
||||
(array/push result (get alphabet (% (+ (+ (+ (- zbyte abyte) 1) (- ch abyte)) r) (length alphabet)))))))
|
||||
(string/from-bytes ;result)))
|
||||
|
||||
(defn code
|
||||
"encodes and decodes str given argument type"
|
||||
[str rot type]
|
||||
(case type
|
||||
:encode (rotate str rot)
|
||||
:decode (rotate str (* rot -1))))
|
||||
|
||||
(defn main [& args]
|
||||
(let [cipher (code "The quick brown fox jumps over the lazy dog" 23 :encode)
|
||||
str (code cipher 23 :decode)]
|
||||
(print cipher)
|
||||
(print str)))
|
||||
Loading…
Add table
Add a link
Reference in a new issue