Skip to content

Instantly share code, notes, and snippets.

@maruks
Created February 17, 2020 10:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maruks/14371e685773472d46ef0ba2c33796a6 to your computer and use it in GitHub Desktop.
Save maruks/14371e685773472d46ef0ba2c33796a6 to your computer and use it in GitHub Desktop.
(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