Chapter 3 Daily 05 HW
Some longer example tests are available. Additional example showing how to read a bunch of lyrics from a file (for a cool thing to encrypt in problem 8).
-
How many numbers are between two given binary numbers, including the start and end number?
bcount :: [Int] -> [Int] -> Int bcount [1,0,0,1,1] [1,1,0,1,0] == 8
-
(
ensureLen :: Int -> [Int] -> [Int]
). The callensureLen 6 xs
add leading zeros toxs
if necessary to ensure it has a length of at least 6.ensureLen 4 [1] == [0,0,0,1] ensureLen 3 [1,0,1,0,1] == [1,0,1,0,1]
-
(
and' :: Int -> Int -> Int
). Helper function. Give 1 if both inputs are 1, otherwise 0. -
(
bitwiseAnd
) Given two binary integersa
andb
(represented as lists of 0 and 1), produce the binary number given by lining up the ones places and the then “and”-ing the digits ofa
andb
together. The shorter number should be padded with zeros to be the same length as the long number. -
(
bitwise
) Given a binary operation (op :: Int -> Int -> Int
) and two binary integers, create a number by performing the operation bitwise as in the previous exercise. The shorter number should be padded with zeros to be the same length as the long number.The type signature for
op
in the bigbitwise
signature is(Int->Int->Int)
. Example:xor' :: Int -> Int -> Int xor' a b = if a == b then 0 else 1 bitwise xor' [1,0,1,0] [0,1,1,0] = [1,1,0,0]
-
txtToBin :: String -> [[Int]]
. Translate a string into a list of binary numbers using the location each character is found in the (abbreviated) list below. If a character is not found, produce the empty list to represent it.[space]A-Za-z0-9.
-
binToMsg :: [[Int]] -> [Int]
. Make sure each binary number is 6 digits, and then concatenate all of the binary numbers into one long list of integers. -
msgDecode :: [Int] -> String
. Now decode the big binary number, changing it back into text. -
encrypt :: String -> String -> String
. The encrypt function takes in a message string and a key string. The encryption lines up the letters in the message and the string, changes the characters into binary numbers, joins the binary numbers withxor
, and then changes the resulting number back into a string. The string will be nonsense.encrypt "Key" "Secret"
The key is repeated and lined up with the secret message, then the values of the binary numbers are combined with
xor
."KeyKeyK" "Secrets" "X tm cl"
Note: this function is likely to require helper functions!
-
decrypt :: String -> String -> String
. Given an encrypted message and a key, reverse the encryption process. This is kind of tricky without experimentation; try just re-encrypting the message and see if you can figure out a pattern.