Bifalgorithm post · Computer science one · Exercises

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

How can file input/output programs process text, binary data, and word searches? (Computer science one · Exercises) — "There are additional performance issues…

Ideas in this flowchart

  • Binary data performance and file-processing exercises
  • Reading and writing data can create performance overhead.
  • Converting binary numbers to strings adds further overhead.
  • Binary data avoids unnecessary parsing.
  • Binary formats should be preferred when human readability is unnecessary.
  • File-processing programming exercises
  • Exercise 9.1: Open a named file and return all its contents as one string.
  • Exercise 9.2: Read polygon coordinates from a text file and compute its area.
  • The polygon file begins with the number of points, followed by one coordinate pair per line.
  • The polygon area is computed with the given coordinate-sum formula.
  • Exercise 9.3: Escape HTML characters in an input file and write a new output file.
  • Exercise 9.4: Spell-check each word in a plain text file against a standard dictionary.
  • Punctuation is ignored, and misspelled or unrecognized words are output.
  • Exercise 9.5: Solve a word search using an input grid and a standard dictionary.
  • Search candidates run down, right, or diagonally down-right and must contain 3 to 8 letters.
Feed/

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

No cover

Channel

Computer science one

1 page

"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)."

Flowchart preview
👍
0
0 comments0 shares

Sign in to view and post comments.