Skip to content

Instantly share code, notes, and snippets.

@mohamedhayibor
Created August 29, 2017 11:57
Show Gist options
  • Save mohamedhayibor/45423dfe60c51a8f22a80ab056726541 to your computer and use it in GitHub Desktop.
Save mohamedhayibor/45423dfe60c51a8f22a80ab056726541 to your computer and use it in GitHub Desktop.
Debugging stackoverflow question
(ns cipherize.core
(:require [reagent.core :as reagent :refer [atom]] ))
(def alphabet-chars
(mapv char (range 97 (+ 97 26))))
(def alphabet
(mapv str alphabet-chars))
(defn shift-by-one
"takes the last and insert at the begginning shifting everything."
[coll]
(->> (first coll)
(vector)
(concat (rest coll))
(vec)))
(defn alphabet-cipher
"Takes alphabet then returns the 2d array representation of cipher"
[coll]
(->> (iterate shift-by-one coll)
(take 26)
(vec)))
(defn seed-msg-letter-set
"Takes a seed (keyword) and a msg, sequential inputs for
encryption and decryption.
Note: the letter seed is always first."
[seed msg]
;; Todo: will be more specific about the string length later,
;; works for now
(if (> (count seed) (count msg))
(mapv str seed (clojure.string/join (repeat (count seed) msg)))
(mapv str (clojure.string/join (repeat (count msg) seed)) msg)))
(defn char-encoding
"Takes a letter seed and message and returns char encoding"
[char-s char-m]
(get-in (alphabet-cipher alphabet) [(- (int char-m) 97) (- (int char-s) 97)]))
(defn encode
"encrypts letter by letter using keyword and message"
[seed msg]
(->> (seed-msg-letter-set seed msg)
(map seq)
(map #(char-encoding (first %) (second %)))
(apply str)))
(enable-console-print!)
;; Testing
(prn "testing encode:" (encode (str "testing") (str "test")))
(prn "testing encode:" (type (encode "testing" "test")))
(prn "testing encode:" (type encode ))
(defn result-state []
(fn []
[:div
[:h1 "Shippin up to Boston"]
]))
(reagent/render-component [ result-state ]
(. js/document (getElementById "app")))
(defn on-js-reload []
;; optionally touch your app-state to force rerendering depending on
;; your application
;; (swap! app-state update-in [:__figwheel_counter] inc)
)
@mohamedhayibor
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment