Guest User

Untitled

a guest
Jan 21st, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Informatics 1 - Functional Programming
  2. -- Tutorial 2
  3. --
  4. -- Week 4 - due: 13/14 Oct.
  5.  
  6. import Data.Char
  7. import Data.List
  8. import Test.QuickCheck
  9.  
  10.  
  11. -- 1.
  12. rotate :: Int -> [Char] -> [Char]
  13. rotate n []                          = error "Cannot rotate empty list"
  14. rotate n xs |n < length xs && n > 0 = drop n xs ++ take n xs
  15.             |n <= 0                  = error "You cannot rotate a string by 0 or by a negative number."
  16.             |n >= length xs           = error "n cannot be bigger than your string."
  17.  
  18.  
  19.  
  20. -- 2.
  21. -- (b) It takes the modulus of the number by the length of the string
  22. -- if it is too large or negative it will return 0, which will end
  23. -- in doing rotate l str == str which is true.
  24.  
  25. prop_rotate :: Int -> String -> Bool
  26. prop_rotate k str = rotate (l - m) (rotate m str) == str
  27.                         where l = length str
  28.                               m = if l == 0 then 0 else k `mod` l
  29.  
  30. -- 3.
  31.  
  32.  
  33. makeKey :: Int -> [(Char, Char)]
  34. makeKey n  = zip ['A'..'Z'] (rotate n ['A'..'Z'])
  35.            
  36.  
  37. -- 4.
  38. -- unzip the 2 lists, try if k is equal to the first term of my list
  39. -- if not drop the first
  40. lookUp :: Char -> [(Char, Char)] -> Char
  41. lookUp c []                 = c
  42. lookUp c  ((x,y):xs) | c==x = y
  43.                      | c/=x = lookUp c xs
  44.                          
  45.  
  46. -- 5.
  47. encipher :: Int -> Char -> Char
  48. encipher n c = lookUp c (makeKey n)
  49.  
  50. -- 6.
  51. normalize :: String -> String
  52. normalize (xs)  = [toUpper x | x <- xs, isAlpha x || isDigit x]
  53.  
  54. -- 7
  55. encipherStr :: Int -> String -> String
  56. encipherStr n [] = []
  57. encipherStr n (xs) = [encipher n x | x <- (normalize (xs))]
  58.  
  59.  
  60. -- 8.
  61. reverseKey :: [(Char, Char)] -> [(Char, Char)]
  62. reverseKey [] = []
  63. reverseKey ((x,y):xs) = (y,x) : reverseKey (xs)
  64.  
  65. -- 9.
  66. decipher :: Int -> Char -> Char
  67. decipher n c = lookUp c (reverseKey(makeKey n))
  68.  
  69. decipherStr :: Int -> String -> String
  70. decipherStr n [] = []
  71. decipherStr n (x:xs) = decipher n x : decipherStr n xs
  72.  
  73. -- 10.
  74. prop_cipher :: Int -> String -> Property
  75. prop_cipher n xs = (n <= length xs && n > 0) ==> (normalize xs == decipherStr n(encipherStr n xs))
  76.  
  77. -- 11.
  78. contains :: String -> String -> Bool
  79. contains xs [] = False
  80. contains [] ys = False
  81. contains (x:xs) ys |take (length ys) (x:xs) == ys = True
  82.                    |otherwise = contains xs ys
  83. -- 12.
  84.  
  85.  
  86. candidates :: String -> [(Int, String)]
  87. candidates xs = [(n, decipherStr n xs) | n <- [1..25], contains (decipherStr n xs) "THE" || contains (decipherStr n xs) "AND"]
Add Comment
Please, Sign In to add comment