Created
February 17, 2020 10:50
-
-
Save maruks/14371e685773472d46ef0ba2c33796a6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defpackage :transposition | |
(:use :cl)) | |
(in-package :transposition) | |
(defparameter *text* "WEAREDISCOVEREDFLEEATONCE------") | |
(defun range (max &key (min 0) (step 1)) | |
(loop for n from min below max by step | |
collect n)) | |
(defun every-nth-0 (text idx n) | |
(when (< idx (length text)) | |
(cons (nth idx text) (every-nth-0 text (+ idx n) n)))) | |
(defun every-nth (text start n) | |
(let ((r (every-nth-0 (coerce text 'list) start n))) | |
(coerce r 'string))) | |
(defun encode (text cols) | |
(let ((ids (range cols))) | |
(mapcar (lambda (i) (every-nth text i cols)) ids))) | |
(defun decode (texts) | |
(let* ((lists (mapcar (lambda (x) (coerce x 'list)) texts)) | |
(r (apply #'mapcar #'list lists))) | |
(apply #'concatenate 'string | |
(mapcar (lambda (x) (coerce x 'string)) r)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment