CS 170 Homework 6 - Deadline Wednesday October 30, 11:59pm

Transcription

CS 170 Homework 6 - Deadline Wednesday October 30, 11:59pm
CS 170
Homework 6 - Deadline Wednesday October 30, 11:59pm
Section 003
Fall 2013
• INSTRUCTIONS: This is the sixth homework for CS170 (Section 003) and covers
the following material: methods. Read the instructions carefully. In this homework
you will need to write several Java programs. It is not on paper homework. You will
need to write some Java programs, compile and test them and then you will need
to submit them the same way you submitted lab assignments.
Program style: Style of your programs will give you 10% of the score. If your program
is poorly written it won’t get perfect score even if it works correctly. By good style we
mean:
– Code is well commented, you don’t need to comment all statements, just some
major parts of the program.
– Variable naming reflects the meaning of the variable. For example, if a variable
stores html code, the name should reflect this.
– Indentation: statement inside blocks should have indentation (usually 4 spaces).
• HONOR CODE: Like all work for this class, the Emory Honor Code applies. The
assignment is an individual assignment and should be completed alone. For all programming assignments, you must write comment at the top of each file which include
the following information:
/*
THIS CODE IS MY OWN WORK. IT WAS WRITTEN WITHOUT CONSULTING CODE
WRITTEN BY OTHER STUDENTS OR MATERIALS OTHER THAN THIS SEMESTER’S
COURSE MATERIALS. _Your_Name_Here_
*/
If the honor code isnt present, the assignment will not be graded!
Homework submission
In your account on lab machines (you can connect and work on them remotely, please check
class website for the instructions) put all program files in the hw6 subdirectory of cs170
directory (/home/<your username>/cs170/hw6/). You can create such directory using the
following commands:
1. mkdir ∼/cs170/hw6
2. save your solution program files to this directory: /home/<your username>/cs170/hw6
3. To submit programs: cd ∼/cs170/hw6
4. /home/cs170003/turnin-hw DoublePalindromicPrimes.java hw6a
5. /home/cs170003/turnin-hw CrackCaesar.java hw6b
6. You can submit multiple times, only the last submission will be stored.
1
Homework 6 (cont.) - Deadline Wednesday October 30, 11:59pm
Fall 2013
Problem 1
1. (40 points) Double Palindromic primes
Name your program DoublePalindromicPrimes
Topics: methods, loops
In this problem you will need to write a Java program, that reads a positive integer
N from the keyboard and prints all numbers from 1 to N (inclusive) that are double
palindromic prime (definition follows).
A palindromic number is a number that remains the same when its digits are reversed.
Like 16461, for example, it is ”symmetrical”. Other examples are: 5, 11, 121, 5005, etc.
Note, a single digit number is considered palindromic.
A prime number is a number that only divides by 1 and itself (e.g. the first primes are:
2, 3, 5, 7, 11, 13, 17, ...). Note, that 1 is not considered as prime number.
A palindromic prime is a prime number that is also a palindromic number. For example,
the first palindromic primes are: 2, 3, 5, 7, 11, 101, ...
A double palindromic prime number is a palindromic number that has q digits and q is
also a palindromic number. For example, the first double palindromic number is 11 (11
is palindromic and prime, it has 2 digits, 2 is also palindromic and prime.). The next
example is 101, because it is prime, palindromic and has 3 digits, 3 is also palindromic
prime.
Example:
> java DoublePalindromicPrimes
Please enter a number: 10
There are no double palindromic prime numbers from 1 to 10.
> java DoublePalindromicPrimes
Please enter a number: 15
11 is double palindromic prime
> java DoublePalindromicPrimes
Please enter a number: 120
11 is double palindromic prime
101 is double palindromic prime
Hint: write down a high level algorithm (all steps in English) how to find all double
palindromic prime numbers. Look at the algorithm and identify which operations you
may want to implement as methods. For example, you will need to check is some number
is a prime. Could you write a method to test if a number is prime? What are other
operations? After you found all the compound parts of algorithm, follow top-down or
bottom-up approach as described on the lecture (read the book if you missed it).
Hint: it may be easier to check if a number is palindromic if you convert it to a string,
because you can traverse digits in both directions, which is not that easy with an integer.
2
Homework 6 (cont.) - Deadline Wednesday October 30, 11:59pm
Fall 2013
Problem 2
2. (60 points) Cracking Caesar’s Cipher
Name your program CrackCaesar
Topics: strings, loops, methods
In the last homework we studies and implemented Caesars cipher, which is one of the
simplest encryption algorithms. Just to remind you: each letter in the text is replaced by
a letter some fixed number of positions down the alphabet. The parameter (or the key)
of this encryption algorithm is a number, which represents by how many positions in the
alphabet each character need to be shifted. The shift operation is cyclic, which means
that if you shift z by 1 to the right you get a. Please refer to the previous homework for
more details (And you can reuse code from the previous assignment).
Unfortunately, it is not a very good cipher as it can be easily cracked. One can just
try all possible keys (it is enough to try keys from 0 to 25), try to decrypt the message
and check if the result looks like a good natural language text. There could be multiple
possible ways to do this. In the homework problem we will study and implement the
method, based on frequency analysis different characters
http://en.wikipedia.org/wiki/Frequency_analysis
Frequency analysis is based on the fact that, in any given stretch of written language,
certain letters and combinations of letters occur with varying frequencies. Moreover,
there is a characteristic distribution of letters that is roughly the same for almost all
samples of that language. For instance, given a section of English language, E, T, A and
O are the most common, while Z, Q and X are rare.
Given a frequency distribution of individual characters in a language, one can simply
calculate the probability of a message as a product of frequencies of each individual
letters.
a - 0.08167 b - 0.01492 c - 0.02782 d - 0.04253
e - 0.12702 f - 0.02228 g - 0.02015 h - 0.06094
i - 0.06966 j - 0.00153 k - 0.00772 l - 0.04025
m - 0.02406 n - 0.06749 o - 0.07507 p - 0.01929
q - 0.00095 r - 0.05987 s - 0.06327 t - 0.09056
u - 0.02758 v - 0.00978 w - 0.02360 x - 0.00150
y - 0.01974 z - 0.00074
As the product of such small numbers can be very very small and we can have underflow
problem, usually people calculate entropy. Entropy is a negative sum of the log of
frequencies:
Pstr.length()−1
E = − i=0
log(f req(str.charAt(i))), where freq(char) is the frequency of a
character char in the language as given above.
For example, let we have a message ”hello world”, it contains 10 letters (we will ignore
all non-alphabetic characters). The entropy for this message is the sum of logarithms of
frequencies of letters h, e, l, l, o, w, o, r, l, d.
3
Homework 6 (cont.) - Deadline Wednesday October 30, 11:59pm
Fall 2013
Entropy = −log(0.06094) − log(0.12702) − log(0.04025) − log(0.04025) − log(0.07507) −
log(0.02360) − log(0.07507) − log(0.05987) − log(0.04025) − log(0.04253).
Note: usually for entropy log2 is used, but you can use natural logarithm (Math.log()
method).
So, in this problem you need to write a program, that gets input and output filenames
as command line arguments, reads the input file and outputs the most likely decryption
of this text to the output file. The way it should do this is the following: your program
tries all possible keys, for each key it decodes a message using Caesar’s cipher and for
decoded message it calculates the entropy. The program should find a decoded message
with the lowest entropy. The message with the lowest entropy is our best guess for the
original text.
Note: We will assume the frequencies are the same for both lowercase and uppercase
letters. All other non-alphabetic characters should be ignored for the entropy calculation.
Hint: Table of frequencies can be stored in an array. We will start learning arrays next
week, but below I show you how you can use them for storing frequencies. Array is just
a sequence of variables of the same type. So, we can declare an array of double values
to store a sequence of numbers - frequencies of letters.
...
// d e c l a r e an a r r a y and i n i t i a l i z e i t w i t h a s e q u e n c e o f numbers
double [ ] f r e q = { 0 . 0 8 1 6 7 , 0 . 0 1 4 9 2 , 0 . 0 2 7 8 2 , 0 . 0 4 2 5 3 ,
0.12702 , 0.02228 ,0.02015 , 0.06094 , 0.06966 ,
0.00153 , 0.00772 , 0.04025 , 0.02406 , 0.06749 ,
0.07507 , 0.01929 , 0.00095 , 0.05987 , 0.06327 ,
0.09056 , 0.02758 , 0.00978 , 0.02360 , 0.00150 ,
0.01974 , 0.00074};
// t o a c c e s s i n d i v i d u a l i t e m s w i t h can use
// t h e f o l l o w i n g s y n t a x : f r e q [ i ] ,
// where i i s an i n d e x o f t h e e l e m e n t i n
// a r r a y .
// For example : t o g e t t h e f r e q u e n c y o f l e t t e r a :
double freqA = f r e q [ 0 ] ;
double f r e q B = f r e q [ 1 ] ;
...
double f r e q Z = f r e q [ 2 5 ] ;
You can test your program on 2 files given in the previous homework. Without knowledge
of the key your program should be able to crack the cipher and retrieve the original text
given encoded text as input.
Encoded text: http://mathcs.emory.edu/~dsavenk/courses/fall13/cs170/hw/output.
txt Original text: http://mathcs.emory.edu/~dsavenk/courses/fall13/cs170/hw/
input.txt
4
Homework 6 (cont.) - Deadline Wednesday October 30, 11:59pm
Fall 2013
Hint: for decoding a message you will need to shift a character left in the alphabet.
The following code gives a decoded character:
// i f ch i s our l o w e r c a s e c h a r a c t e r t o decode , t h e n :
char e n c o d e d c h a r = ( ch − ’ a ’ + 26 − key ) % 26 + ’ a ’ ;
Hint: Don’t panic! :) Write algorithm in English first. Try to define which methods you
can define. It seems reasonable to implement a method, that returns decoded message
given a string and a key as parameters. Also, calculating entropy for a message is a
good candidate for being a method. You can split the program deeper, e.g. implement a
method that decodes a character using the given key and use this method for decoding
string.
5