For all of these solutions, you should realize there are often many ways to solve a problem, and my solution is only one of those ways. Your solution is just as good, as long as it also produces the right answer.

  3. Remember that good form in type-checking requires you to put a caret (^) at the beginning of the regular expression and a dollar sign ($) at the end of the regular expression so that your program can be sure that there is not extra junk in the data.
    1. /^[mf]$/
    2. /^\d{5}$/
    3. /^(\d{1,2}-\d{1,2}-(19|20)\d{2})$/
    4. /^(\d{10}|\d{3}-\d{3}-\d{4}|\(\d{3}\) ?\d{3}-\d{4})$/
    5. /^\w+@\w+(.\w+)*$/

    1. Reads the input as a stream of pixels, negates each one by subtracting it from 255, and prints it out. The first four "pixels" are actually header information that I just echo out.
    2. Reads the input as a stream of pixels. The first four "pixels" are actually header information that I just echo out. I then unshift a column's worth of pixels into an array and print the array using the join command. Unshifting is like pushing the items onto a stack, which reverses them. I repeat this procedure for each row.
    3. To show you that there are many ways to solve the same problem in Perl, I have included Kristy Van Horweder's solution. It is line-oriented solution that reads the input a line at a time. It uses the specific format of the first three lines of the pgm file to extract the header information and print it out. It then reads the remaining lines into a huge array and indexes into the array in order to extract the last row, then the next to last row, etc.
  6. My solution puts the command line arguments into a hash table and initializes their word counts to 0. For each word in the file we check whether or not the word is in the hash table, and if so, increment that word's count. I have to do some manipulation to clean up the words, including using the lc function to lowercase the words and a regular expression to extract the leading, alphanumeric portion of a word.