Permalink
Browse files

added coding challenge

  • Loading branch information...
1 parent 51fcd0b commit 87fd9fcafc3e3675bc07284d37f1cf5bdcc65e9c @epsilonhalbe committed Jun 21, 2012
Showing with 112 additions and 1 deletion.
  1. +68 −0 CodingChallenges/001-CodingChallenge.hs
  2. +44 −1 README.md
@@ -0,0 +1,68 @@
+-- from http://stackoverflow.com/questions/11144393
+import Prelude hiding (lookup)
+import Data.Char ( isAlpha
+ , toUpper)
+import Data.List ( group
+ , sort
+ , sortBy)
+import Data.Function (on)
+import Data.Map ( fromList
+ , lookup
+ , Map)
+import Data.Maybe (mapMaybe)
+
+codedMsg :: String
+{-
+codedMsg = "V'Z GELVAT GB GRNPU GUR PNIRZRA GB CYNL FPENOOYR." ++
+ "VG'F HCUVYY JBEX. GUR BAYL JBEQ GURL XABJ VF 'HAU'," ++
+ "NAQ GURL QBA'G XABJ UBJ GB FCRYY VG."
+-}
+codedMsg = "LIVITCSWPIYVEWHEVSRIQMXLEYVEOIEWHRXEXIPFEMVEWHKVSTYLXZIXLIKIIXPIJVS"
+ ++ "ZEYPERRGERIMWQLMGLMXQERIWGPSRIHMXQEREKIETXMJTPRGEVEKEITREWHEXXLEXXM"
+ ++ "ZITWAWSQWXSWEXTVEPMRXRSJGSTVRIEYVIEXCVMUIMWERGMIWXMJMGCSMWXSJOMIQXL"
+ ++ "IVIQIVIXQSVSTWHKPEGARCSXRWIEVSWIIBXVIZMXFSJXLIKEGAEWHEPSWYSWIWIEVXL"
+ ++ "ISXLIVXLIRGEPIRQIVIIBGIIHMWYPFLEVHEWHYPSRRFQMXLEPPXLIECCIEVEWGISJKT"
+ ++ "VWMRLIHYSPHXLIQIMYLXSJXLIMWRIGXQEROIVFVIZEVAEKPIEWHXEAMWYEPPXLMWYRM"
+ ++ "WXSGSWRMHIVEXMSWMGSTPHLEVHPFKPEZINTCMXIVJSVLMRSCMWMSWVIRCIGXMWYMX"
+
+freqTable_EN :: [Char]
+freqTable_EN = ['E', 'T', 'A', 'O', 'I', 'N', 'S', 'H', 'R', 'D', 'L', 'C', 'U'
+ ,'M', 'W', 'F', 'G', 'Y', 'P', 'B', 'V', 'K', 'X', 'J', 'Q', 'Z']
+
+-- weed out non alphabetical characters from the list
+filterAlpha :: String -> String
+filterAlpha = filter isAlpha
+
+-- sort the list by length
+sortByLength :: [[a]] -> [[a]]
+sortByLength = sortBy (compare `on` length)
+
+-- sort the list into most frequent character first
+sortByFreq :: [[a]] -> [[a]]
+sortByFreq = reverse . sortByLength
+
+-- change the list into one instance of each character
+reduceGroups :: [[a]] -> [a]
+reduceGroups = map head
+
+-- Pairing letters
+pairs :: [(Char, Char)]
+pairs = nonAlphaPairs ++ zip freqSortedMsg freqTable_EN
+ where cleanedMsg = (filterAlpha . map toUpper) codedMsg
+ freqSortedMsg = (reduceGroups . sortByFreq . group . sort) cleanedMsg
+ nonAlphaPairs = map (\x ->(x,x)) $ filter (not . isAlpha) codedMsg
+
+-- and creating a map of tuples containing frequency related characters
+cipher :: Map Char Char
+cipher = fromList pairs
+
+-- replace coded list with analyzed list
+decipher :: String -> String
+decipher = mapMaybe (uplook cipher)
+ where uplook = flip lookup
+
+result :: String
+result = decipher codedMsg
+
+main :: IO ()
+main = print result
View
@@ -1,5 +1,5 @@
# Here will be all of ε/2's code for lambdaheads meeting
----
+
## 2012-02-19: Crop:
from the [ccc2010](http://catcoder.catalyst.cc): the task to solve is to
@@ -11,3 +11,46 @@ get numbers of an array according to a path.
* [level5](http://www.unet.univie.ac.at/~a0307893/downloads/description-level5.pdf)
I tried to solve this in Haskell - by generating a path and turning the array
and always collecting the first row.
+
+## 99-haskell-problems
+are just some finger exercises for not loosing grasp of haskell
+
+## Coding Challenges
+inspired by hop I want to introduce coding challenges to Lambdaheads
+
+### Cryptoanalysis 101:
+Inspired by a question I answered on stackoverflow.com:
+One knows the easiest of cryptographic ciphers to break is the "Ceasar-Cipher"
+it works just by transposition of letters. The way to break it is to analyze the
+number of occurences of a letter in a text and compare it to a frequency table
+of the guessed language the text is written in.
+**write a function to decipher the following text by use of frequency analysis**
+
+ * the following text is written in english:
+<pre>
+LIVITCSWPIYVEWHEVSRIQMXLEYVEOIEWHRXEXIPFEMVEWHKVSTYLXZIXLIKI
+IXPIJVSZEYPERRGERIMWQLMGLMXQERIWGPSRIHMXQEREKIETXMJTPRGEVEKE
+ITREWHEXXLEXXMZITWAWSQWXSWEXTVEPMRXRSJGSTVRIEYVIEXCVMUIMWERG
+MIWXMJMGCSMWXSJOMIQXLIVIQIVIXQSVSTWHKPEGARCSXRWIEVSWIIBXVIZM
+XFSJXLIKEGAEWHEPSWYSWIWIEVXLISXLIVXLIRGEPIRQIVIIBGIIHMWYPFLE
+VHEWHYPSRRFQMXLEPPXLIECCIEVEWGISJKTVWMRLIHYSPHXLIQIMYLXSJXLI
+MWRIGXQEROIVFVIZEVAEKPIEWHXEAMWYEPPXLMWYRMWXSGSWRMHIVEXMSWMG
+STPHLEVHPFKPEZINTCMXIVJSVLMRSCMWMSWVIRCIGXMWYMX
+</pre>
+ * the frequency table for the english language is given by:
+<pre>
+['E', 'T', 'A', 'O', 'I', 'N', 'S', 'H', 'R', 'D', 'L', 'C', 'U'
+,'M', 'W', 'F', 'G', 'Y', 'P', 'B', 'V', 'K', 'X', 'J', 'Q', 'Z']
+</pre>
+ * the result should be - as frequency analysis is not a real good tool:
+<pre>
+RENEMYSODEUNTOLTNSHEWIARTUNTKETOLHATAEDBTINTOLFNSMURAPEAREFE
+EADEGNSPTUDTHHCTHEIO WRICRIAWTHEOCDSHELIAWTHTFETMAIGMDHCTNTF
+TEMHTOLTAARTAAIPEMOVOSWOASOTAMNTDIHAHSGCSMNHETUNETAYNIJEIOTH
+CIEOAIGICYSIOASGKIEWARENEWENEAWSNSMOLFDTCVHYSAHOETNSOEEXANEP
+IABSGAREFTCVTOLTDSOUSOEOETNARESARENAREHCTDEHWENEEXCEELIOUDBR
+TNLTOLUDSHHBWIARTDDARETYYETNTOCESGFMNOIHRELUSDLAREWEIURASGAR
+EIOHECAWTHKENBNEPTNVTFDETOLATVIOUTDDARIOUHIOASCSOHILENTAISOI
+CSMDLRTNLDBFDTPEQMYIAENGSNRIHSYIOISONEHYECAIOUIA
+</pre>
+* the real result is to be deciphered by rot13!

0 comments on commit 87fd9fc

Please sign in to comment.