Haskell : Caesar Cipher Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

chart2int c = ord c - ord ‘a’

A

returns 2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

int2char i = chr (i + ord ‘a’)

A

z

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

shift c offset =
let
converted = char2int c
is_lower = converted >= 0 && converted < 26
in
if is_lower
then int2char ((converted + offset) ‘mod’ 26)
else c

A

shift ‘a’ 3
Shifts a by three
Mod implements wrap around

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

caesar_enc [] - = []
caesar_enc (x:xs) offset = shift x offset
: caesar_enc xs offset

A

caesar_enc “Hello there” 3
“khoor wkhuh”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

caesar_dec [] _ = []
caesar_dec (x:xs) offset = shift x (-offset)
: caesar_dec xs offset

A

caesar_enc “khoor wkhuh” 3
“Hello there”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Chi-squared score

A

(freq-english)^2/english
Lower the score, closer to english

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

count _ [] = 0
count c (x:xs)
|c==x = 1+rest
|otherwise = rest
where rest = count c xs

A

count ‘a’ “aabaa”
4

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

freq c list = fromIntegral (count c list) / fromIntegral (length list)

A

freq ‘a’ “aabaa”
0.8

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

get_freqs _ 26 = []
get_freqs string c = freq (int2char c) string : get_freqs string (c+1)

A

get_freqs “abc” 0
[“0.3333334”, “0.3333334”, “0.3333334”, 0.0, 0.0…]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

chi_squared [] [] = 0
chi_squared (x:xs) (y:ys) =
(x-y)**2/y + chi_squared xs ys

A

chi_squared [0.1, 0.9] [0.8, 0.2]
3.0624998

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

eng_freqs = […]

A

list of all English character frequencies

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

chi_squared_string string =
let
string_freqs = get_freqs string 0
in
chi_squared string_freqs eng_freqs

A

chi_squared_string “hello there”
1.5819808

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

chi_squared_list _ 26 = []
chi_squared_list string i =
let
decoded = caesar_dec string i
score = chi_squared_string decoded
in
(score, decoded) : chi_squared_list string (i+1)

A

chi_squared_list “ifmmp” 0
[(9.637143,”ifmmp”),(4.4730797,”hello”),(22.258533,”gdkkn”),…

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

get_best [(socre,string)] = (score = string)
get_best((score, string):xs) =
let
(tail_score, tail_string) = get_best xs
in
if score < tail_score
then (score, string)
else(tail_score,tail_string)

A

get_best (chi_squared_list “ifmmp” 0)
(4.4730797, “hello”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

caesar_crack string =
let
score = chi_squared_list string 0
(score, best) = get_best scores
in
best

A

caesar_crack “ifmmp”
“hello”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly