Supplemental Problems For Computer Science 153

Supplemental Problems

Supplemental Problems

Supplemental Problems extend the range of problems considered in the course and help sharpen problem-solving skills. Starred problems may be turned in for extra credit.

Format: In turning in any programs for the course, please follow these directions:

  1. The first three lines of any Scheme program should be comments containing your name, your mailbox number, and an identification of assignment being solved. For example:
        ;;; Henry M. Walker
        ;;; Box Y-06
        ;;; Supplemental Problem 2
    Also, a comment is needed for every procedure, stating in English what that procedure is supposed to do.

  2. Obtain a listing of your program and a record of relevant test runs using the submit command:

  3. Either write on your printout or include a separate statement that argues why your program is correct, based upon the evidence from your test runs.
List Processing: A common processing pattern in Scheme involves operating on pairs of adjacent elements in a list rather than on single elements. For instance, we might want a procedure adjacent-sums that takes a non-empty list of numbers and returns a list of the sums of adjacent pairs, thus:

(adjacent-sums '(1 2 3 4 5 6 7)) ===> (3 5 7 9 11 13)
(adjacent-sums '(-5 12 13 0 -8)) ===> (7 25 13 -8)
(adjacent-sums '(7/3 -1/2 8/5 9/4)) ===> (11/6 11/10 77/20)
(adjacent-sums '(4 7)) ===> (11)
(adjacent-sums '(16)) ===> ()
Here's how we'd write it:

(define adjacent-sums
  (lambda (ls)
    (if (null? (cdr ls))
        (cons (+ (car ls) (cadr ls)) (adjacent-sums (cdr ls)))
  1. Give your own rendition of this procedure ``in English.''

Adjacent Sums
  1. Write and test a Scheme procedure adjacent-elements that takes any list of 2 or more numbers and returns a list of the sum of adjacent sums from the original list. That is, each element on the returned list will be the sum of two adjacent elements in the original list.
    (adjacent-sums '(3 1 4 1 5 9 2)) ===> (4 5 5 6 14 11)
    (adjacent-sums '(5 12 12 0)) ===> (17 24 12)

Lists of Descending Numbers

  1. Write and test a Scheme predicate (a procedure that always returns #t or #f) that takes any non-empty list of real numbers and determines whether they are in descending numerical order, in the sense that each one is strictly greater than the one that follows it.

    (descending? '(150 100 50 0 -50)) ===> #t
    (descending? '(5.0 4.1 3.8)) ===> #t
    (descending? '(8 7 6 4 5 3 2 1)) ===> #f
    (descending? '(0 0 0 0)) ===> #f
    (descending? '(-3 -2)) ===> #f
    (descending? '(17)) ===> #t

Another Exercise

  1. A baby sitter charges $1.50 per hour until 9:00 pm (while the kids are still up), $1.00 per hour between 9:00 pm and midnight, and $1.25 per hour after midnight (since late night baby sitting interferes with morning classes).

    Write a procedure that takes as arguments the sitter's starting time in hours and minutes and the ending time in hours and minutes and then computes the sitter's fee. Assume all times are between 6:00 pm and 6:00 am.

    Programming Note: You may NOT use any if statements, loops, or recursion in your procedure(s) for this program. Rather, you ARE encouraged to use cond statements to handle the parallel conditions that arise in this problem.

Grading Passwords

  1. Since many modern computer systems use passwords as a means to provide protection and security for users, a major issue can be the identification of appropriate passwords. The main point should be to choose passwords that are not easily guessed, but which the user has a chance of remembering. For example, passwords related to birthdays, anniversaries, family names, or common words are all easily guessed and should be avoided.

    Some common guidelines suggest that a password should contain at least 6 characters and include characters from at least three of the following categories:

    Other guidelines indicate that elements of passwords should be pronounceable. One simple measure of this guideline suggests that any group of letters in a password should contain both vowels and consonants.

    In Scheme, some procedures are already built-in to test some of these conditions. Specifically, the Scheme standard identifies the procedures char-alphabetic?, char-numeric?, char-whitespace?, char-upper-case?, and char-lower-case?. See the section on characters in the Scheme standard for more details.

    1. The first part of this supplemental problem is to write additional procedures for other categories of characters. (Some of these procedures were part of the lab on characters and may be reused.)

      • Procedure vowel? has a single parameter and returns true if the parameter's value is a vowel and false otherwise.
      • Procedure consonant? has a single parameter and returns true if the parameter's value is a consonant and false otherwise.
      • Procedure punctuation? has a simple parameter and returns true if the paramter's value is a punctuation mark (i.e., not a letter or digit) and false otherwise.

      Note: While you are free to use built-in Scheme predicates for these procedures, you may not use type conversion procedures. Thus, you are not allowed to convert characters to integers or other data types.

    2. The second part of this problem is to write a tail-recursive procedure that checks whether a string contains a character meeting a particular test. Specifically, write procedure
         (contains? str pred?)
      which applies the predicate pred? to each character in string str and which returns 1 if some character meets this predicate test and 0 otherwise. For example, contains? should return the following results:
      (contains "Walker" char-upper-case?) ===> 1
      (contains "Walker" char-lower-case?) ===> 1
      (contains "Walker" char-numeric?)    ===> 0
      (contains "Walker" punctuation?)     ===> 0

      Note: To meet the specifications below, contains? must return 1 or 0, not #t, #f, or another number.

      Also note: If you can, this procedure should handle strings as strings, rather than convering a string to another data type such as a list.

    3. The third part of this problem is to write procedure password-grade which takes a string as parameter and which gives a grade to that password according to the following grading scale:
      • Assign 1 point for each of the following elements in the password:
        • password contains at least 6 characters
        • password contains at least 1 vowel
        • password contains at least 1 consonant
        • password contains at least 1 upper-case letter
        • password contains at least 1 lower-case letter
        • password contains at least 1 numeric character
        • password contains at least 1 punctuation mark

      • Assign a letter grade to the password by applying the sum of the points above to the following.
        • 6 or 7 points: A
        • 5 points: B
        • 4 points: C
        • 3 points: D
        • 0 or 1 or 2 points: F

    According to this scale, "Walker" would receive a "B" grade, with points for string length, vowel, consonant, upper-case letter, and lower-case letter.

    After writing and debugging your procedures, use the submit process described at the beginning of these supplemental problems to turn in your code. In considering the correctness of your code, be sure to write out (in a paragraph) what cases each procedure might encounter, identify test cases to cover each of those cases, show the testing in your submit file, and comment on the extent to which your code handles those cases correctly.

Reading Test Data

  1. (*)File in directory ~walker/151s/labs contains information on test results for a certain class. Each line of the file contains a students first and last name and the results of three hour tests. Write a program that computes the average test score for each student, the maximum and the minimum scores for each test, and prints the results in a nicely formatted table. For example, the table might have the following form:
         Name                        Test
    First        Last        1       2       3     Average
    Egbert       Bacon      88      85      92      88.33   
    Maximum                 --      --      --
    Minimum                 --      --      --
Information on the 1997-1998 Iowa Senate

  1. File /home/walker/151s/labs/ia-senate contains information about the members of the 1997-1998 Iowa Senate. After a title line and a blank line, a typical line has the following form:
    Angelo          Jeff        44      Creston           IA 50801
    Kramer          Mary        37      West Des Moines   IA 50265
    Lundby          Mary        26      Marion            IA 52302-0563
    Thus, a typical line gives the last name, the first name, the district number, the town of residence, the state (always IA), and the town's zip code. The information in these lines is arranged in columns.

    Design and write a Scheme program that reads in data from this file and creates two output files, senators-by-district and senators-by-zip-code, in the current working directory. The senators-by-district file should contain the same data as the source file, in the same format, but with the lines arranged by senate district (column 3). The other file, senators-by-zip-code, should contain a list of all senators in the following format

    Jeff Angelo
    Creston, IA 50801
    A blank line should appear after each senator and city address. In this format, the name appears on a first line (first name, then last), and the city, a comma, the state, and zip code is on the next line -- separated by single spaces in the format shown. Note that a variation of this format (with a street address, if available) might be used for a mailing label.

  1. To be determined
  1. To be determined

Any of the following problems may be done for extra credit. As noted in the course syllabus, however, a student's overall problems' average may not exceed 120%.

Unusual Canceling

  1. The fraction 64/16 has the unusual property that its reduced value of 4 may be obtained by "canceling" the 6 in the numerator with that in the denominator. Write a program to find the other fractions whose numerators and denominators are two-digit numbers and whose values remain unchanged after "canceling."

    Of course, some fractions trivially have this property. For example, when numerator and denominator are multiples of 10, such as 20/30, one can always "cancel" the zeroes. Similarly, cancelation is always possible when the numerator and denominator are equal, as in 22/22. Your program should omit these obvious cases.

Roman Numerals
  1. Write a procedure that reads an integerN between 1 and 1000 from the keyboard and prints the Roman numerals between 1 and N in alphabetical order.

City Data

  1. (*)The file ~walker/151p/labs/lab26.dat contains several items of information about large American cities. More specifically, in ~walker/151p/labs/lab26.dat , each entry consists of the name of the city (line 1), the county or counties (line 2) and the state (line 3) in which it is situated, the year in which it was incorporated (line 4), its population as determined by the census of 1980 (line 5), its area in square kilometers (line 6), an estimate of the number of telephones in the city (line 7), and the number of radio stations (line 8) and television stations (line 9) serving the city. Thus a typical entry reads as follows:
    New Mexico
    A blank line follows each entry, including the last.

    Write a procedure which has a filename as parameter and which answers the following questions about the cities represented in the data files.

    1. Which of those cities has the highest population density (population divided by area)?
    2. Which of these cities has over one million telephones?
    3. Which city has the lowest per capita number of radio and television stations (together)?
    The answers to each of these questions should be printed neatly and clearly by the procedure.

File Analysis

  1. (*)Write a procedure file-analysis that takes the name of a file as its argument, opens the file, reads through it to determine the number of words in each sentence, displays the total number of words and sentences, and computes the average number of words per sentence. The results should be printed in a table (at standard output), such as shown below:
         This program counts words and sentences in file "comp.text ".
         Sentence:  1    Words: 29
         Sentence:  2    Words: 41
         Sentence:  3    Words: 16
         Sentence:  4    Words: 22
         Sentence:  5    Words: 44
         Sentence:  6    Words: 14
         Sentence:  7    Words: 32
         File "comp.text" contains 198 words words in 7 sentences
         for an average of 28.3 words per sentence.
    In this program, you should count a word as any contiguous sequence of letters, and apostrophies should be ignored. Thus, "word", "sentence", "O'Henry", "government's", and "friends'" should each be considered as one word.

    Also in the program, you should think of a sentence as any sequence of words that ends with a period, exclamation point, or question mark.
    Exception: A period after a single capital letter (e.g., an initial) or embedded within digits (e.g., a real number) should not be counted as being the end of a sentence.
    White space, digits, and other punctuation should be ignored.

This document is available on the World Wide Web as

created February 6, 1997
last revised February 10, 2001 Henry Walker (