Concept

How can file input/output programs process text, binary data, and word searches?

ComputerScienceOne / Exercises

"There are additional performance issues when reading/writing the data and converting binary numbers to their string representations. With binary data no such parsing is necessary. As long as the data does not need to be human-readable, binary formats should be preferred.\n\nExercise 9.1. Write a function that takes a string representing a file name and opens and processes the file, returning all of its contents as a single string.\n\nExercise 9.2. Consider an irregular, 2-D simple polygon with n points, ( x 1 , y 1 ) , ( x 2 , y 2 ) , . . . , ( x n , y n ) The area A of the polygon can be computed as A = 1 2 n − 1 ∑ i =0 ( x i y i +1 − x i +1 y i ) Note, that the initial and end point will be the same, ( x 0 , y 0 ) = ( x n , y n ). An example polygon for n = 5 can be found in Figure 9.2. Figure 9.2.: An example polygon for n = 5 Write a program to open and process a text file containing n coordinates. In particular, the first line is a single integer n that indicates how many points should be read in. Each line after that has the x, y coordinates of each point separated by a single space. 4 1.0 0.0 13.2 1.25 20.5 18.4 16.37 24.54 188\n\nAfter reading the file in, it will compute the area of the polygon according to the formula above and output it to the user. For example, the output for the above file may be something like Area of the polygon: 197.9135\n\nExercise 9.3. Write a program that processes an input text file and scrubs it of any HTML characters that need to be escaped (see Exercise 8.20 for details). It should produce a new output file with all special characters escaped.\n\nExercise 9.4. Write a program that spell checks a plain text file. The program will open a text file and process each word separately, checking for proper spelling against a standard dictionary. You may assume that each word is separated by some whitespace (you may assume that there are no multi-line hyphenated words). However, you should ignore all punctuation (periods, question marks, etc.). Use a standard American dictionary provided on your unix system which stores words one per line. Your output should include all misspelled or unrecognized words (words not contained in the dictionary file).\n\nExercise 9.5. A standard word search consists of an n × n grid in which there are a number of words hidden, some intersecting, with dummy letters filling in the blanks. An example is provided in Figure 9.3. Figure 9.3.: A Word Search Write a program to solve a word search. Your program will read in an input file with the following format: the first line will contain a single integer n which is followed by n lines with n characters (not including the end line character) corresponding to the word search. Once you read in the word search, you will iterate through all possible words running down, right, or diagonally down-right. You will attempt to match each possibility against a standard English dictionary. If the word matches a word in the dictionary, output it to the standard output, otherwise ignore it. To simplify, you may restrict your attention to words that have a length between 3 and 8 (inclusive)."

Related Ideas

How can file input/output programs process text, binary data, and word searches? | ComputerScienceOne | Bifalgorithm | Bifalgorithm