https://wiki.haskell.org/index.php?title=Haskell_Quiz/The_Solitaire_Cipher/Solution_Stoltze&feed=atom&action=history
Haskell Quiz/The Solitaire Cipher/Solution Stoltze - Revision history
2024-03-29T06:20:59Z
Revision history for this page on the wiki
MediaWiki 1.35.5
https://wiki.haskell.org/index.php?title=Haskell_Quiz/The_Solitaire_Cipher/Solution_Stoltze&diff=33757&oldid=prev
Newacct at 11:25, 21 February 2010
2010-02-21T11:25:10Z
<p></p>
<table class="diff diff-contentalign-left diff-editfont-monospace" data-mw="interface">
<col class="diff-marker" />
<col class="diff-content" />
<col class="diff-marker" />
<col class="diff-content" />
<tr class="diff-title" lang="en">
<td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">← Older revision</td>
<td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">Revision as of 11:25, 21 February 2010</td>
</tr><tr>
<td colspan="2" class="diff-lineno">Line 43:</td>
<td colspan="2" class="diff-lineno">Line 43:</td>
</tr>
<tr>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>-- Not really needed, but since they specified cutting it into chuncks of 5, I thought I'd comply</div></td>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>-- Not really needed, but since they specified cutting it into chuncks of 5, I thought I'd comply</div></td>
</tr>
<tr>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>splitInto :: Int -> [Char] -> [Char]</div></td>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>splitInto :: Int -> [Char] -> [Char]</div></td>
</tr>
<tr>
<td class="diff-marker">−</td>
<td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div>splitInto n list = <del class="diffchange diffchange-inline">concat $ intersperse " "</del> $ splitInto' n list where</div></td>
<td class="diff-marker">+</td>
<td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>splitInto n list = <ins class="diffchange diffchange-inline">unwords</ins> $ splitInto' n list where</div></td>
</tr>
<tr>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> splitInto' n list | null list = []</div></td>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> splitInto' n list | null list = []</div></td>
</tr>
<tr>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> | length list < n = [list ++ replicate (n - length list) 'X']</div></td>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> | length list < n = [list ++ replicate (n - length list) 'X']</div></td>
</tr>
</table>
Newacct
https://wiki.haskell.org/index.php?title=Haskell_Quiz/The_Solitaire_Cipher/Solution_Stoltze&diff=18051&oldid=prev
Stoltze at 22:15, 2 January 2008
2008-01-02T22:15:03Z
<p></p>
<table class="diff diff-contentalign-left diff-editfont-monospace" data-mw="interface">
<col class="diff-marker" />
<col class="diff-content" />
<col class="diff-marker" />
<col class="diff-content" />
<tr class="diff-title" lang="en">
<td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">← Older revision</td>
<td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">Revision as of 22:15, 2 January 2008</td>
</tr><tr>
<td colspan="2" class="diff-lineno">Line 1:</td>
<td colspan="2" class="diff-lineno">Line 1:</td>
</tr>
<tr>
<td colspan="2" class="diff-empty"> </td>
<td class="diff-marker">+</td>
<td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>[[Category:Haskell Quiz]]</div></td>
</tr>
<tr>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div><haskell></div></td>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div><haskell></div></td>
</tr>
<tr>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>module Main where</div></td>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>module Main where</div></td>
</tr>
</table>
Stoltze
https://wiki.haskell.org/index.php?title=Haskell_Quiz/The_Solitaire_Cipher/Solution_Stoltze&diff=17565&oldid=prev
Stoltze at 22:45, 15 December 2007
2007-12-15T22:45:15Z
<p></p>
<p><b>New page</b></p><div><haskell><br />
module Main where<br />
<br />
import Char<br />
import List<br />
<br />
data Card = Card Int<br />
| JokerA<br />
| JokerB<br />
deriving (Show, Eq)<br />
<br />
instance Enum Card where<br />
fromEnum JokerA = 53<br />
fromEnum JokerB = 54<br />
fromEnum (Card n) = n<br />
toEnum n | n == 53 = JokerA<br />
| n == 54 = JokerB<br />
| otherwise = Card n<br />
succ JokerB = Card 1<br />
<br />
<br />
getValue :: Card -> Int<br />
getValue (Card n) = n<br />
getValue JokerA = 53<br />
getValue JokerB = 53<br />
<br />
deck :: [Card]<br />
deck = [Card 1 .. JokerB]<br />
<br />
alphabet :: [Char]<br />
alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"<br />
<br />
letterToNumber :: Char -> Int<br />
letterToNumber ch = f' 1 ch where<br />
f' n ch = if alphabet !! (n-1) == ch then n else f' (n+1) ch<br />
<br />
numberToLetter :: Int -> Char<br />
numberToLetter n | n < 1 = numberToLetter (n + 26)<br />
| n > 26 = numberToLetter (n - 26)<br />
| otherwise = alphabet !! (n - 1)<br />
<br />
-- Not really needed, but since they specified cutting it into chuncks of 5, I thought I'd comply<br />
splitInto :: Int -> [Char] -> [Char]<br />
splitInto n list = concat $ intersperse " " $ splitInto' n list where<br />
splitInto' n list | null list = []<br />
| length list < n = [list ++ replicate (n - length list) 'X']<br />
| otherwise = [take n list] ++ splitInto' n (drop n list)<br />
<br />
-- Takes some words and a function for parsing. Since encode and decode were very similar, I thought this would be easier<br />
process :: [Char] -> (Int -> Int -> Int) -> [Char]<br />
process line fn = let noSpaces = map toUpper $ filter isAlpha line<br />
key = genKey $ length noSpaces<br />
keyvalues = map letterToNumber key<br />
linevalues = map letterToNumber noSpaces<br />
newline = map (\(x, y) -> numberToLetter (fn x y)) $ zip linevalues keyvalues<br />
in splitInto 5 newline<br />
<br />
encode :: [Char] -> [Char]<br />
encode line = process line (\line key -> line + key)<br />
<br />
decode :: [Char] -> [Char]<br />
decode line = process line (\line key -> line - key)<br />
<br />
-- Generates the keycode<br />
genKey :: Int -> [Char]<br />
genKey n = repl n deck where<br />
repl 0 _ = []<br />
repl n val = let res = convert $ move val<br />
in if res == ' ' then repl n (move val) else res : (repl (n-1) (move val))<br />
<br />
toLetter :: Card -> Char<br />
toLetter JokerA = ' '<br />
toLetter JokerB = ' '<br />
toLetter card = (cycle alphabet !!) $ getValue card - 1<br />
<br />
convert :: [Card] -> Char<br />
convert (x:xs) = toLetter $ (x:xs) !! getValue x<br />
<br />
<br />
-- Moves the deck one step ahead<br />
move :: [Card] -> [Card]<br />
move cards = stepFour $ stepThree $ stepTwo $ stepOne cards where<br />
findStart :: (Eq a) => a -> [a] -> [a]<br />
findStart n (x:xs) = if x == n<br />
then (x:xs)<br />
else findStart n (xs ++ [x])<br />
<br />
stepOne, stepTwo, stepThree, stepFour :: [Card] -> [Card]<br />
stepOne (JokerA:x:xs) = x:JokerA:xs<br />
stepOne (x:xs) = let change (x:y:xs) = if x == JokerA<br />
then (y:x:xs)<br />
else change ((y:xs) ++ [x])<br />
in findStart x $ change (x:xs)<br />
<br />
stepTwo (JokerB:x:y:xs) = x:y:JokerB:xs<br />
stepTwo (x:xs) = let change (x:y:z:xs) = if x == JokerB<br />
then (y:z:x:xs)<br />
else change ((y:z:xs) ++ [x])<br />
in findStart x $ change (x:xs)<br />
<br />
stepThree (x:xs) = let fn c = c /= JokerA && c /= JokerB<br />
toFirst = takeWhile fn (x:xs)<br />
toLast = reverse $ takeWhile fn $ reverse (x:xs)<br />
in toLast ++ (reverse (drop (length toLast) (reverse (drop (length toFirst) (x:xs))))) ++ toFirst<br />
<br />
stepFour cards = let l = last cards<br />
r = getValue l<br />
in drop r (init cards) ++ take r (cards) ++ [l]<br />
</haskell></div>
Stoltze