CS1003/1004 Homework #2
Due by Tuesday, March 2, at 11:00am
(you can submit it by Thursday, February 26, at 5:00pm for 3 pts. extra credit)

There are two parts to this homework assignment: a theory portion, worth 15 points, and a programming portion, worth 10 points. Please be sure to review the submission instructions in advance.

Theory questions

  1. (5 points; variation on Brookshear 4.3.3) You're given the following "encrypted" message: Pdeo eo pda yknnayp wjosan.
    1. (2 points) Decode the message, and explain (in a few sentences) how you "got your foot in the door".
    2. (2 points) Describe an algorithm, in either English or psuedocode, as to how to encode an arbitrary English phrase into this "secret" code.
    3. (1 point) What's the running time of this algorithm, and (one sentence) why?
  2. (5 points) You're given the following list of numbers in order: 5, 36, 167, 229, 412, 1039, 3556, 4843, 9876, 10329, 15443
    1. (1 point) State the number of steps it would take to find 9876 using a sequential search.
    2. (2 points) State the number of steps it would take to find 9876 using a binary search, and list them (i.e., the numbers that are compared until 9876 is found).
    3. (2 points) Is binary search always faster than linear search? If so, explain in a few sentences why. Otherwise, provide a counterexample and demonstrate (by stating the number of steps) how long it would take for each search.
  3. (5 points) One common challenge in mathematics is to find the next large prime number -- as you get into larger and larger integers, the number of primes decreases substantially. We won't worry about finding the largest primes, but we'll look at a smaller variant of the problem.
    1. (3 points) Devise an algorithm, and a corresponding procedure, to determine whether or not a positive integer is prime (i.e., a procedure called isPrime that takes one integer and returns a true/false answer). You can use psuedocode, C, or Java; it doesn't have to be syntactically precise, but it should be semantically precise.
    2. (2 points) What's the running time of this algorithm, and why?

Programming assignment: palindrome checking

A palindrome is a string that is symmetric, i.e., you get the same result when looking at it forwards or backwards (ignoring case and punctuation). Famous palindromes include "Madam, I'm Adam", "Able was I ere I saw Elba", and "radar" (want more?). In this exercise, we'll write a program that will be able to tell if a string is a palindrome.

  1. (5 points) Write a procedure called isPalindrome that takes one parameter (a string) and returns a boolean (Java) or a int (C) indicating whether or not the supplied String is a palindrome. (For C, return a 0 if it's not, or a 1 if it is.)

    In order to accomplish this, you'll need to employ a loop construct to process the entire string. Set up two variables -- a "left" one at position 0 and a "right" one at position n-1, and work towards the middle, character-by-character, checking to see if they're equal. You'll stop in one of two cases:
    1. The "current" characters don't match. In this case, we don't have a palindrome;
    2. Left is greater or equal to than right. This means that either the markers are pointing to the same character, or have crossed over (i.e., right is pointing at the character immediately before left). In either case, we've successfully found a palindrome.
    Before you go onto the next step, test this code with a simple palindrome (like "radar"). Note that the above will not help in the case where you've got uneven spaces or punctuation; the subsequent parts will handle this.
  2. (2 points) Modify the palindrome checking procedure so that it's case-insensitive. There are several approaches to doing this, but the simplest might be to always convert the two characters that are being compared to lowercase before you actually compare them. In C, there's a function called tolower that takes a character and returns the lowercase equivalent. In Java, the Character.toLowerCase method does the exact same thing. Note that both operate on a char.
  3. (2 points) Modify the palindrome checking procedure so that it ignores whitespace and punctuation. In particular, handle spaces ( ), periods (.), commas (,), and apostrophes (') so that when they're encountered, you skip over it. In other words, you'll have to modify your while loop so that it only compares if both are characters; otherwise it will skip over one of them and try again.
  4. (1 point) Finally, write a main method that takes the first command-line parameter, feeds it to isPalindrome, and uses the result to say either "<phrase> is a palindrome" or "<phrase> is not a palindrome". Note that to test palindromes with spaces, you must enclose the whole argument in a pair of quote marks; otherwise, the words will be separated as separate arguments. So, to run your final program, you might type something like

One last important consideration: now that your programs are starting to get more complex, style and commenting become an issue. We won't be draconian about it, but we expect some effort at designing your code and commenting appropriately. Tips on this will be discussed in detail in class.