Skip to content

Instantly share code, notes, and snippets.

@ex
Created September 21, 2013 23:54
Show Gist options
  • Save ex/6655358 to your computer and use it in GitHub Desktop.
Save ex/6655358 to your computer and use it in GitHub Desktop.
let text = "Uid nx, aex jcdjipx iu wzux zp, ta wxtpa jtdaws, ai etkx vis
Dcos zyexdzaxr aex Jxdw jezwipijes iu etkzyg nidx aety iyx hts
ai ri aex ptnx aezyg. Z zyexdzaxr aeta jezwipijes udin Wtdds Htww,
hei zp ns exdi tqactwws. Z htya ai ntfx Dcos cpxdp udxx. Z htya ai
gzkx aexn aex udxxrin ai qeiipx. Jxijwx tdx rzuuxdxya. Jxijwx qeiipx
rzuuxdxya qdzaxdzt. Oca zu aexdx zp t oxaaxd hts tniyg ntys
twaxdytazkxp, Z htya ai xyqicdtgx aeta hts os ntfzyg za qinuidatowx.
Pi aeta'p heta Z'kx adzxr ai ri.
Z htya ai piwkx jdiowxnp Z nxxa zy aex rtzws wzux os cpzyg qinjcaxdp,
pi Z yxxr ai hdzax jdigdtnp. Os cpzyg Dcos, Z htya ai qiyqxyadtax aex
aezygp Z ri, yia aex ntgzqtw dcwxp iu aex wtygctgx, wzfx patdazyg hzae
jcowzq kizr pinxaezyg pinxaezyg pinxaezyg ai pts, \"jdzya exwwi hidwr.\"
Z vcpa htya ai pts, \"jdzya aezp!\" Z riy'a htya tww aex pcddicyrzyg
ntgzq fxshidrp. Z vcpa htya ai qiyqxyadtax iy aex atpf. Aeta'p aex otpzq
zrxt. Pi Z etkx adzxr ai ntfx Dcos qirx qiyqzpx tyr pcqqzyqa.
Scfzezdi Ntapcniai. (hhh.tdaznt.qin/zyak/dcos)\n"
let freqLang = "TEOIARNSHLMYUCWDGPFBVKJ"
(* Count the number of repetitions per character *)
let len = String.length text
let repetitions = Hashtbl.create 128;;
for k = 0 to len - 1 do
let c = Char.uppercase (String.get text k) in
if c >= 'A' && c <= 'Z' then
if Hashtbl.mem repetitions c then
Hashtbl.replace repetitions c (Hashtbl.find repetitions c + 1)
else
Hashtbl.replace repetitions c 1
done;;
let dic = Hashtbl.create 128
let freqText = ref ""
let index = ref 0
(** Sort in descending order **)
let getKeys hash = Hashtbl.fold (fun key _ list -> key :: list) hash [];;
let comparator key1 key2 = compare (Hashtbl.find repetitions key2) (Hashtbl.find repetitions key1)
let keys = List.sort comparator (getKeys repetitions);;
List.iter (fun key ->
(* print_char key; print_string ": "; print_int (Hashtbl.find frequency key); print_string "\n"; *)
freqText := (!freqText ^ (String.make 1 key));
Hashtbl.replace dic key (String.get freqLang !index);
index := !index + 1
) keys;;
print_endline freqLang;;
print_endline !freqText
(** Translate **)
let decrypted = ref "";;
for k = 0 to len - 1 do
let upper = ref false in
let c = ref (String.get text k) in
if !c >= 'A' && !c <= 'Z' then
upper := true
else
c := Char.uppercase !c;
if Hashtbl.mem dic !c then
if !upper then
decrypted := !decrypted ^ (String.make 1 (Hashtbl.find dic !c))
else
decrypted := !decrypted ^ (String.make 1 (Char.lowercase (Hashtbl.find dic !c)))
else
decrypted := !decrypted ^ (String.make 1 !c)
done;;
print_endline !decrypted
(*
#load "graphics.cma";;
open Graphics;;
open_graph "640x480";;
for i = 12 downto 1 do
let radius = i * 20 in
set_color (if (i mod 2) = 0 then red else yellow);
fill_circle 320 240 radius
done;;
read_line ();;
*)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment