Einzelnen Beitrag anzeigen
Alt 06.04.09, 18:43   #42 (permalink)
Ook!
 
Registriert seit: 21.04.08
Ook! Leistung: Facit NTK
Likes: 0
Standard

und nochmal in Haskell ...

Code:
import Char
import List

caesar :: String -> (Int, Int) -> String
caesar [] (_, _) = []
caesar (x:xs) (shift, dec) = enc : caesar xs (shift, dec)
  where
    enc = [ 'A'..'Z'] !! (mod (ord x - 65 + (dec * shift)) 26)

vigenere :: String -> String -> (Int, Int) -> String
vigenere [] _ (_, _) = []
vigenere (x:xs) ks (idx, dec) = enc : vigenere xs ks (newIdx, dec)
  where
    alph = [ 'A'..'Z']
    newIdx = mod (idx + 1) (length ks)
    enc = alph !! (mod (ord x - 65 + (dec * (shift (ks !! idx, 0)))) 26)
    shift (k, i) = if i > 25 then 1 else if alph !! i == k then i else shift (k, i+1)

main :: IO()
main = do
  putStrLn $ "FELIX -> " ++ caesar "FELIX" (3, 1)
  putStrLn $ "IHOLA -> " ++ caesar "IHOLA" (3, -1)
  putStrLn $ "GEHEIMNIS -> " ++ vigenere "GEHEIMNIS" "AKEY" (0, 1)
  putStrLn $ "GOLCIWRGS -> " ++ vigenere "GOLCIWRGS" "AKEY" (0, -1)
Gruß
Felix
Ook! ist offline   Mit Zitat antworten
 

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61