CS 1323 Section 30, Spring 1997

Individual Project 3
Text Layout Functions
Due Monday, February 17, 3:30pm
- papers submitted after 3:30pm Feb 17 and before 3:30pm Feb 19 suffer a penalty of one-grade per day -
- papers not accepted after 3:30pm Feb 19 -
Note: Start today! This is more than twice as hard as your last project.
Anticipate a definition or collection of defintions comprising one or two dozen lines.

What to Hand In (see also: example project solution)

The Function You Must Define

Four projects are described below. Different team members do different projects. Team members listed first or fifth on their team's roster must do Project 3A; those listed second or sixth, Project 3B; third, Project 3C; fourth, Project 3D.

Project 3A - packaging lines
Define a function called linePackets that splits a sequence of strings (words) supplied in the second argument into a sequence of sequences of strings (line packets) that fit within a length limit supplied in the first argument, but would not fit if they contained any additional strings.

The delivered sequence of packets must satisfy the following properties:

Examples

  linePackets 12 ["The", "purpose", "of", "computing"]
    = [["The", "purpose"], ["of computing"]]
  linePackets 6 ["is", "insight,", "not", "numbers."]
    = [["is"], ["insight,"], ["not"], ["numbers."]]
  linePackets 14 (repeat "blah") = repeat["blah", "blah", "blah"]
Show that the function you define has the following property:
If k::Int and ws::[String] and
(1) ps = linePackets k ws and
(2) p is an element of ps and
(3) sum[length w | w <- p] + length(p) - 1 > k,
then length(p) = 1.
Project 3B - fully justifying lines
Define a function called justify that, given a line length (first argument) and a sequence of strings (words) (second argument), delivers a string (line) of exactly the given length containing the words equally spaced, as nearly as possible, across the line. The line must be constructed so that the first character of the first word is the first character of the line and the last character of the last word, if there is more than one word, is the last character of the line and so that difference between the greatest and least number of spaces between adjacent words is either zero or one.
Exception: if the length of the string formed by concatenating the given words with one space between each pair of adjacent words exceeds the given length, then that string should be delivered as the value of the function.

Examples:

  justify 12 ["The", "purpose"] = "The  purpose"
  justify 12 ["of", "computing"] = "of computing"
  justify 6 ["is"] = "is    "
  justify 6 ["insight,"] = "insight,"
  justify 52 ["The", "purpose", "of", "computing",
              "is", "insight,", "not", "numbers."] =
           "The  purpose  of  computing is insight, not numbers."
Show that the function you define has the following property:
If ws::[String] and length(ws) > 1 and length(unwords ws) <= n,
then length(justify n ws) = n

Project 3C - arranging words in columns
Define a function called columns that, given a number indicating how many columns are desired and a sequence of strings (words), delivers a sequence of sequences (columns) of words such that the first column contains the initial segment of the given sequence of words, the second column contains the next segment, and so on. All columns must have exactly the same length; if the number of words is not a multiple of the number of columns, then pad the bottom of some of the columns with an empty string.

Note: Each column must consist entirely of words from the argument, except that the last element of the column may be a pad; the number of padded columns must be less than the number of columns.

Examples

   columns 3 ["Bool", "Char", "Int", "Float",
              "Double", "Integer", "String"] =
                                [["Bool", "Char", "Int"],
                                 ["Float", "Double", ""],
                                 ["Integer", "String", ""]]
   columns 3 ["Bool", "Char", "Int", "Float", "Double", "Integer"] =
                                [["Bool", "Char"],
                                 ["Int", "Float"],
                                 ["Double", "Integer"]]
   columns 3 ["Bool", "Char", "Int", "Float", "Double"] =
                                [["Bool", "Char"],
                                 ["Int", "Float"],
                                 ["Double", ""]]
Show that the function you define has the following property:
If n::Int, n >= 1, and ws::[String],
then ws is a subsequence of concat(columns n ws)

Project 3D - making a table
Define a function called table that, given a number (the first argument) indicating how many spaces to put between columns and a sequence of sequences of strings (columns of words), all columns having exactly the same length, delivers a sequence of strings (rows) such that each row is formed by concatenating one word from each column in such a way that the words from the first column occur at the beginnings of the rows, the words from the second column follow the words from the first column in their respective rows, then the words from the third column and so on.

The rows must be arranged so that the columns "line up" and so that the minimum gap between each adjacent pair of columns is the number supplied as the first argument. That is, if the second word in the first row begins at the k-th character in the string representing that row, then the second word in all the other rows must begin at the k-th character of the row. Furthermore, number of spaces between the end of the first and second words on the row must be exactly equal to the second argument in the invocation of the table function. Other words in the rows must line up in an analogous way and follow the same constraint on the number of spaces between words in one column and words in the next column. All of the rows must contain exactly the same number of characters. Use spaces to separate the words of a row and to pad out the rows to make them all the same length.

Examples

  table 3 [["Char", "String", "Bool"],
           ["Int", "Integer", "Rational"],
           ["Float", "Complex Float", "Double"]] =
      ["Char     Int        Float        ",
       "String   Integer    Complex Float",
       "Bool     Rational   Double       "]
  table 1 [["Char", "String", "Bool"],
           ["Int", "Integer", "Rational"],
           ["Float", "Complex Float", "Double"]] =
      ["Char   Int      Float        ",
       "String Integer  Complex Float",
       "Bool   Rational Double       "]
  table 0 [["Char", "String", "Bool"],
           ["Int", "Integer", "Rational"],
           ["Float", "Complex Float", "Double"]] =
      ["Char  Int     Float        ",
       "StringInteger Complex Float",
       "Bool  RationalDouble       "]
Show that the function you define has the following property:
All the rows of a table are exactly the same length. That is,
If n::Int, n >= 0, cs::[[String]],
and r::String and s::String are any two elements of table n cs
(that is, [..., r, ..., s, ...] = table n cs)
then length(r) = length(s)

Ground Rules


CS 1323 Section 30 - Fundamentals of Computer Programming - Spring 1997
Instructor: Rex Page (Email: page@ou.edu)
Up to: Welcome Page ~~~ Up to: University of Oklahoma ~~~ Go to: Next Semester ~~~ Go to: Previous Semester