Supplemental Problems For Computer Science 151

Supplemental Problems

Supplemental Problems

Problems: 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 , 17

Supplemental Problems extend the range of problems considered in the course and help sharpen problem-solving skills. Problems numbered 9 or higher 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:
    • Within an hpterm window (before running scheme), begin a recording session with the statement

      submit filename

      where filename is the name of the file in which you want the session stored.
    • Print a copy of your Scheme definitions with the command

      cat Scheme-file.ss

      where Scheme-file.ss is the name of the file containing your Scheme program. If your work involves several files, list all of them with the cat command.
    • Run scheme as usual, with the scheme command.
    • Load file Scheme-file.ss with the load command.
    • Run appropriate test cases to check the correctness of your program.
    • Stop scheme with <Ctrl/D> as before.
    • Stop recording by typing <Ctrl/D> one more time.
    • Print the record of your session by typing

      print filename

  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.''

Student Data within an Expert System

  1. At the beginning of each academic year, the Department of Mathematics and Computer Science suggests a tentative placement in mathematics and computer science courses, based upon a review of high school transcripts and standardized test scores. The full process of analyzing this data uses techniques of expert systems -- an area of research within the field artificial intelligence. Some details of this system are presented later in this semester in an extra-credit lab. Recent research also is exploring the use of neural networks -- another area within artificial intelligence -- to help in the placement process.

    While the current expert system involves a reasonably long and complex LISP program, the data structure follows a fairly simple list format. The following define statements illustrate lists for three hypothetical students (Donald Duck, Minnie Mouse, and Mickey Mouse):

    
    (define student1 
            '( "Donald Duck" 
               ( (SAT 257) (SemOfEng 8)(SemOfMath 8)(Grades 2.50)(SemOfPCalc 1)(PCalcGrades 3.00) (TSemUnits 35) )
               (Campus-Box Y-6 )
               (Adviser "H Walker") ) )
    
    (define student2
            '( "Minnie Mouse"
               ( (SAT 710) (APAB 5) (SemOfEng 8)(SemOfMath 8)(Grades 3.38)(SemOfCalc 2)(CalcGrades 4.00)(SemOfCS 1)(CSGrades 4.00) (TSemUnits 41) )
               (Campus-Box Y-2 )
               (Adviser "S Rebelsky") ) )
    
    (define student3
            '( "Mickey Mouse"
               ( (SAT 650) (SemOfEng 8)(SemOfMath 6) (Grades 3.83) (SemOfPCalc 2)(PCalcGrades 4.00) (TSemUnits 40) )
               (Campus-Box Y-3 )
               (Adviser "J Stone") ) )
    
    The structure for each student is a list with four elements:
    1. The name comes first.
    2. A list of test results and transcript entries comes next.
      • Each test result or transcript entry is designated as a list of two items, such as (SAT 710).
      • The first element of the pair identifies the nature of the information. For example, SAT specifies a math SAT score; APAB specifies a score on the Advanced Placement Calculus AB Examination; SemOfEng, SemOfMath, SemOfPCalc, SemOfCalc and SemOfCS specify the number of semesters of English, mathematics, pre-calculus, calculus, and computer science the student took in high school respectively; Grades, PCalcGrades, CalcGrades, and CSGrades give the students average grades in mathematics, precalculus, calculus, and computer science, respectively; and TSemUnits specifyies the number of semester units earned in all subjects during high school.
      • The second element of the pair indicates the students score or average grades.
      • For example, (SAT 710) indicates that the student scored a 710 on the SAT mathematics test.
  2. The third element on the main list is the student's mailbox.
  3. The last element on the main list is the name of the student's advisor.

Given such a list structure, it is common to write simple procedures to extract various pieces of data or to rearrange the order of data on the list. For example, the following procedure retrieves the student's mailbox:


(define student-mailbox
   (lambda (student)
        (car (cdr (car (cdr (cdr student)))))
   )
)
With this definition, the procedure student-mailbox produces the following output with the above definitions:

(student-mailbox student1)   ===> y-6
(student-mailbox student2)   ===> y-2
(student-mailbox student3)   ===> y-3

Write the following procedures, and test these procedures with the data for the three students given above.

  1. Procedure student-name returns the name of the student's name.
  2. Procedure student-advisor returns the advisor of the student.
  3. Procedure student-SAT produces the student's SAT score, assuming that the SAT pair is the first element of the test-result/transcript-entry list.
  4. Procedure reformat-entry produces a new list, with the student's mailbox at the head of the list, the name of the student's advisor's name next, the test-result/transcript-entry list third, and the student's name last. That is, the new list largely contains the original data in reverse order, except that the mailbox and advisor lists are replaced by the data themselves.

While you will want to test each of your procedures thoroughly, you should include the following tests as part of your submitted work:


(student-name student1)         ===> "Donald Duck"
(student-name student2)         ===> "Minnie Mouse"
(student-advisor student1)      ===> "H Walker"
(student-advisor student2)      ===> "S Rebelsky"
(student-SAT student1)          ===> 257
(student-SAT student3)          ===> 650
(reformat-entry student2)       ===> (y-2
                                      "S Rebelsky"
                                      ((sat 710)
                                       (apab 5)
                                       (semofeng 8)
                                       (semofmath 8)
                                       (grades 3.38)
                                       (semofcalc 2)
                                       (calcgrades 4.0)
                                       (semofcs 1)
                                       (csgrades 4.0)
                                       (tsemunits 41))
                                      "Minnie Mouse")
(reformat-entry student3)       ===> (y-3
                                      "J Stone"
                                      ((sat 650)
                                       (semofeng 8)
                                       (semofmath 6)
                                       (grades 3.83)
                                       (semofpcalc 2)
                                       (pcalcgrades 4.0)
                                       (tsemunits 40))
                                      "Mickey Mouse")
A Gambling Simulation
  1. In a private game, a gambler sets the following personal limits. The gambler starts the evening with $20; he bets $2 on each game and stops when he either runs out of money or has a total of $50. To be more specific, for a specifc game, the gambler bets $2. If the gambler loses the bet, then $2 is deducted from his account. If the gambler wins the bet, then the gambler wins a payoff amount, and the gambler's new balance is increased by that payoff -- the $2 is not deducted. For example, if the payoff is $5 and if the gambler starts the game with $20, then the gambler's new balance would be $18 if the gambler loses a bet and $25 if the gambler wins the bet.

    The following problems will allow you to investigate the likelihood of the gambler winning by simulating games each evening over a period of 1000 nights of gambling. To accomplish this simulation, you are to proceed in three steps:

    1. Write a procedure game which simulates a single bet. Thus, game should have parameters for the gambler's purse amount before the bet, the payoff from a $2 bet, and the probability of the gambler winning the single bet. Then game should return the amount the gambler has after that single bet, using a random number generator and the probability to determine if the gambler won. For example,
      
         (game  37  5  0.7)
      
      should represent the result of a bet, when the gambler starts with $37, when the return on a $2 bet is $5, and when the gambler has a 70% chance of winning the bet. With these values, game should return 35 if the gambler loses the bet and 42 if the gambler wins.

      Note that one way to simulate a bet is to compare (random 1.0) with the probability 0.7:

      
         (cond ((< (random 1.0) 0.7) ;winning part
                    )
               (else ;losing part
                    )
          )
      
    2. Modify game to obtain a procedure evening as follows:
      • the gambler always begins an evening with a $20 purse.
      • if the gambler is cleaned out (the balance is less than $2, the amount required for another bet), evening should return "lost";
      • if the gambler's balance is $50 or more, evening should return "won";
      • if the gambler is still playing and wins the current bet, the gambler continues playing, with the winnings added to his purse; and
      • if the gambler is still playing and loses a bet, then gambler continues playing, with the $2 bet deducted.

      For example, the call

      
         (evening  5  0.7)
      
      should simulate an evening of gambling, where the balance begins at $20, the payoff amount is $5, and the probability of winning a game is 0.7 . The result of this call will be the conclusion "won" or "lost".

    3. Using evening, write a procedure simulate-evenings which takes the payoff from a $2 bet, the probability of winning a single bet, and a number of evenings to be played as parameters and returns the number of evenings won. For example, if the gambler consistently plays (fair) games which have a $2 payoff and a 50% chance of winning, and if the gambler plays 1000 evenings of gambling, then an appropriate procedure call would be:
      
         (simulate-evenings  2  0.5  1000)
      
      Similarly, if the gambler consistently plays (fair) games which have a $4 payoff and a 1/3 change of winning, and if the gambler plays 500 eveings of gambling, then an appropriate procedure call would be:
      
         (simulate-evenings  4  1/3  500)
      
      Hint: Since the number of evenings to be played is the new element distinguishing simulate-evenings from evening, you might focus upon that parameter as the basis for processing in simulate-evenings. An appropriate call to the evening procedure already will handle other details of betting for an evening.

    4. After you have written your procedures, be sure to test them in several cases. What test cases and results can help you determine that your code might be correct?

Data Tabulation and File Processing
  1. A common application of computing involves the tabulation of experimental or observed data. This problem illustrates a simple application of this type.

    Many conferences involve sessions where professionals discuss their work. Typically, a Program Committee first issues a Call for Participation, in which professionals are invited to submit papers describing their work. These papers then are read by several referees, and each referee ranks the paper in such categories as technical content, writing and organization, originality, and significance. The referee also provides an overall rating. A Program Committe then averages the scores and uses these averages as part of the paper selection process to determine what sessions will be scheduled during the conference.

    As many papers may be submitted to a conference, the process of collecting ratings and generating averages often is automated. Ratings are collected in a file, and a program then reports the various averages. For example, file

    /home/walker/151s/labs/paper-ratings-sample

    contains the ratings for two such papers. In this file, the first line for a paper gives the paper number and other headings. Subsequent lines give the ratings from each referee in each of several categories. The data for one paper follow the data for the previous, using the same format. Thus, the above file appears as follows:

    
    Paper 1   Technical  Organization   Originality  Significance  Overall
    
                  4            3             4            4           4
                  3            3             2            2           2
                  3            1             3            2           2
                  3            3             3            3           3
                  3            2             3            2           2
    
    Paper 2   Technical  Organization   Originality  Significance  Overall
    
                  3          2               4            4           3
                  3          4               5            5           5
                  4          5               4            6           5
                  3          3               2            2           3
    

    Since paper acceptance is related to ratings, some research has been done to determine the variability of ratings among different referees. As part of a recent study, each of 10 papers were given to 80 to 120 referees, and file

    /home/walker/151s/labs/paper-ratings-study

    contains some results.

    In this problem, you are to write a procedure that reads the data for the papers in a file and computes the average ratings for each of the areas (technical content, writing and paper organization, originality, significance, and overall). These results should be printed paper by paper. Thus, the results from the sample file above could be given as follows:

    
    Paper 1
         Number of referees:  5 
         Average of technical content score:  3.2
         Average of organization score:  2.4
         Average of originality score:  3
         Average of significance score:  2.6
         Average of overall score:  2.6
    
    Paper 2
         Number of referees:  4 
         Average of technical content score:  3.25
         Average of organization score:  3.5
         Average of originality score:  3.75
         Average of significance score:  4.25
         Average of overall score:  4
    

    While you may present the output in any format you feel appropriate, your program should clearly identify each paper and properly label both the number of referees reporting and the average of their ratings in each category.

    Notes:

    1. You may assume that data for each paper starts with the headings shown above, where the second element in the title is the paper number.
    2. You may assume that all entries are complete. You need not account for incomplete data.
    3. You may assume that each paper has been reviewed by at least one referee.
    4. Your main procedure should have a single parameter -- the name of the file containing the data.
    5. In testing your procedure, you should show runs for both the sample and study files mentioned above.
    6. In writing your program, you might want to write one procedure to process data for a single paper. This procedure might be considered an inner kernel procedure.
    7. The paper-processing procedure may be called repeatedly until all papers in the file have been processed. Altogether, the husk kernel could open the file, call the kernel procedure until all papers have been processed, and close the original file.
    8. Remember to use the submit process to record the program listing and test runs. (This process is described in more detail at the beginning of this lab.)

    Very Important Reminder: The course syllabus states, "...since a primary goal of the course is to enable students to develop their own programs, collaboration is not allowed on homework assignments, supplemental problems, or tests. In addition, students should note the department's policy regarding the role of user-consultants for Computer Science 151 ."

    Students with any questions about this policy should talk to their instructor prior to beginning this problem. Accordingly, following faculty legislation, the CSC 151 instructor will have to turn over to the Academic Standing Committee any evidence of collaboration found on any Supplemental Problems.

Processing Census Data
  1. The U. S. Census Bureau compiles a wide range of data beyond simple population counts. For example, file /home/walker/151s/labs/state-income contains the median annual income for a 4-person family for the United States, each of the states, and the District of Columbia for each of the years from 1979 to 1997 (the year with the most recently released data). More such information may be found at the Bureau's Web site at http://www.census.gov/, with various types of income data available from http://www.census.gov/hhes/www/income.html.

    This file begins as follows:

     
    
                             Median Income for 4-Person Families, by State, According to the U.S. Census Bureau 
                            Reported November 3, 1999 on Web site http://www.census.gov/hhes/income/4person.html
    
    Year                  1997  1996  1995  1994  1993  1992  1991  1990  1989  1988  1987  1986  1985  1984  1983  1982  1981  1980  1979
    
    United States        53350 51518 49687 47012 45161 44615 43056 41451 40763 39051 36812 34716 32777 31097 29184 27619 26274 24332 22395
    Alabama              48240 44879 42617 41730 37975 39659 37638 35937 34930 33022 31221 29799 28407 26595 25117 24181 22443 22026 18613
    Alaska               57474 62078 56045 53555 51181 49632 49721 51538 48411 47247 47106 41292 42897 44017 38238 31823 35834 32745 31037
    Arizona              47133 45032 44526 41599 39679 39900 39364 38799 38347 36892 35711 33477 32129 29431 27551 29835 25163 23832 23000
    Arkansas             38646 36828 38520 36510 32594 36682 34566 31913 31853 28665 27415 27157 26255 23075 21524 20710 20583 19448 18493
    
    

    As this shows, the first two lines contain header information. A blank line follows, followed by column titles. For the remainder of the table, the state name is left justified within the first 21 characters on the line. Median income figures start with character 22, and each income value is 5 characters long. Income numbers are separated by 1 space.

    This problem involves processing the data in this file to answer SOME of the following questions and statements.

    1. Which state had the lowest median income for 1994?
    2. Which state had the highest median income for 1995?
    3. Which states had a median income under 40000 in 1996?
    4. Which states had a median income above 60000 in 1995?
    5. Print a table of each state, with its median income figures for the years 1997 and 1990 only.
    6. Print a table showing the increase in income from 1990 to 1997 for each state.
    7. Print a table showing the percentage increase in income for each state from 1990 to 1997.
    8. Which state has the largest increse in median family income from 1985 to 1995?
    9. Are there any states which had a lower median family income one year than in the previous year? (If so, which one(s)?)
    10. Given a year, find the states with the maximum and minimum median family income for that year.
    11. List all states whose median income for a specified year are within $2000 of the median for the United States for that year.
    12. Given the name of a state and a year, determine the state's median income for that year.

    In addressing these questions or statements, the District of Columbia should be processed in the same way as each state. The data for the United States as a whole may or may not be included in the output, at your option.

    Assignment: Each of the above questions or statements may be addressed by a separate procedure. For parts a through i, no parameters are needed for the procedure. Parts j and k require a parameter for the year; and part l requires two parameters, a state name and a year.

    While all of these questions and statements are interesting, students are asked to write only three procedures, according to the following table.

    Start of Last Name 151.01 151.02
    A-Da, e, i b, e, j
    E-Gb, f, l d, g, i
    H-Rc, g, k a, h, l
    S-Zd, h, j c, f, k
    Thus, a person in Section 151.01 whose last name is Walker should write procedures for parts d, h, and j above.

    Following good programming style, comments should be included at the start of each procedure, indicating what that procedure does. Also, comments should be included within a procedure to indicate the main steps of processing.

    After writing your procedures, follow the usual format for submitting supplemental problems. Be sure your testing is adequate and include a paragraph indicating how your testing demonstrates that your output is correct.

    Very Important Reminder: The course syllabus states, "...since a primary goal of the course is to enable students to develop their own programs, collaboration is not allowed on homework assignments, supplemental problems, or tests. In addition, students should note the department's policy regarding the role of user-consultants for Computer Science 151 ."

    Students with any questions about this policy should talk to their instructor prior to beginning this problem. Accordingly, following faculty legislation, the CSC 151 instructor will have to turn over to the Academic Standing Committee any evidence of collaboration found on any Supplemental Problems.

Filtering and Reporting Data
  1. Gemstones are attractive forms of rock crystal, commonly used for decoration and in jewelry. Gemstones also have interesting mineral properties. Gemstones may be classified in a variety of ways, including chemical composition, crystal structure, color, specific gravity, refractive index, and hardness:

    1. Chemical Composition: While some gemstones are primarily composed of atoms of one element (e.g., diamonds are mostly carbon, with coloring coming from traces of other elements), other gemstones are made up of atoms of several atoms (e.g., mica molecules include oxygen, hydrogen, silicon, aluminum, iron, and/or many others). On-line sources of information include general references (e.g., Common Mineral Groups) and references to specific minerals (e.g., micas).

    2. Color may be classified informally (e.g., red, yellow, etc.) or more formally by viewing thin slices of mineral crystals through the microscope, using polarized light (see, for example, Minerals under the Microscope).

    3. Specific Gravity is a measure of the density of a mineral. More precisely, specific gravity is the ratio of the weight of the mineral in air to its weight in an equal volume of water. More details are available from various on-line sources (see, for example, the Mineral and Gemstone Kingdom's glossary for specific gravity.

    4. Refractive Index provides a measure of how much light bends within a crystal. The higher the refractive index, the more bending and the more brilliant a crystal is likely to appear. For more information, see various on-line sources, such as Refractive Index.

    5. Crystal Structure: Crystals typically have one of several standard shapes or structures, including cubic, tetragonal, orthorhombic, hexagonal, monoclinic, and triclinic. While the details of such structures are beyond the scope of this problem, the World Wide Web contains many useful references, including crystal forms (at the macro-level) and the (atomic-level) representation of structures prepared as part of lecture series by S. J. Heyes.

    6. Hardness often is measured on the (nonlinear) Mohs Scale, which associates a hardness number to each mineral, from 1 (softest) to 10 (hardest):

      1. Talc
      2. Gypsum
      3. Calcite
      4. Fluorite
      5. Apatite
      6. Orthoclase
      7. Quartz
      8. Topaz
      9. Corundum
      10. Diamond

      As a comparison, a fingernail has hardness 2.5, glass has hardness 5.5, and a steel file has hardness 6.5. Minerals of the same hardness should not scratch each other, but a mineral of one hardness will scratch minerals with a lower hardness number.

    File /home/walker/151s/labs/gems.txt contains information on several gemstones, including color, hardness, specific gravity, and refractive index. Within the file, each line contains information about a specific gemstone.

    Here are a couple of sample lines, and a character 'ruler' to show how wide the fields are:

              11111111112222222222333333333344444444445555555555666666666677777
    012345678901234567890123456789012345678901234567890123456789012345678901234
    
                    Zircon        RED           7.5         4.50         1.95
                     Topaz     YELLOW             8         3.53         1.62
    

    To clarify, the names of the gemstones come first in a line and are right-justified in a column. The colors come next, followed by hardness (on a scale 1 to 10), then specific gravity, and finally refractive index (generally between 1.3 and 2.5).

    Write a program print-by-color that will let you select the gemstones of a certain color and print the information about those gemstones, where the resulting table is in alphabetical order by gemstone name and where the columns are labeled.

    For example, if this procedure is invoked with the statement

    
    (print-by-color "GREY")
    
    the procedure should return a table, such as the following:
    
                                                          Specific   Refractive
                  Gemstone       Color       Hardness      Gravity      Index
    
                 Alabaster       GREY             2         2.32         1.53
                  Bakelite       GREY           2.5         1.28         1.65
                   Calcite       GREY             3          2.7         2.71
                    Casein       GREY           2.5         1.33         1.55
                  Celluoid       GREY             2         1.35         1.50
                Chalcedony       GREY             7         2.62         1.53
                  Corundum       GREY             9         3.99         3.99
                   Diamond       GREY            10         3.52         3.52
                  Hematite       GREY             6         5.10         5.05
                     Ivory       GREY           2.5         1.80         1.54
                   Jadeite       GREY             7         3.34         3.33
               Labradorite       GREY             6          2.7         2.70
                    Marble       GREY             3         2.71         1.50
                Meerschaum       GREY             2         1.50         1.53
                  Nephrite       GREY           3.5         3.00         2.96
                      Opal       GREY             6         2.10         2.10
                    Quartz       GREY             7         2.65         1.55
                    Quartz       GREY             7         3.33         2.65
                      Talc       GREY             1         2.70         2.75
    
    Another possible format might be:
    
                                           Specific   Refractive
    Gemstone Name       Color    Hardness   Gravity      Index
    
    Alabaster            GREY       2         2.32        1.53
    Bakelite             GREY       2.5       1.28        1.65
    Calcite              GREY       3         2.70        2.71
    Casein               GREY       2.5       1.33        1.55
    Celluoid             GREY       2         1.35        1.50
    Chalcedony           GREY       7         2.62        1.53
    Corundum             GREY       9         3.99        3.99
    Diamond              GREY      10         3.52        3.52
    Hematite             GREY       6         5.10        5.05
    Ivory                GREY       2.5       1.80        1.54
    Jadeite              GREY       7         3.34        3.33
    Labradorite          GREY       6         2.70        2.70
    Marble               GREY       3         2.71        1.50
    Meerschaum           GREY       2         1.50        1.53
    Nephrite             GREY       3.5       3.00        2.96
    Opal                 GREY       6         2.10        2.10
    Quartz               GREY       7         2.65        1.55
    Quartz               GREY       7         3.33        2.65
    Talc                 GREY       1         2.70        2.75
    

    As shown in each example, the gemstone names and properties must appear in labeled columns. Gemstone names may be either left-justified or right-justified.

    Note that some gemstones, such as Quartz above, appear several times in the table, since variations of a gemstone may have different properties.

    Optional Extra Credit: Expand print-by-color to allow two additional and optional parameters, giving lower and upper bounds for the hardness of the gemstones to be printed.

    With this extra credit option, (print-by-color "GREY") generates the same table as above, but (print-by-color "GREY" 2 3.6) prints the following sublist where all gemstones have a hardness between 2 and 3.6, inclusive.

    
                                           Specific   Refractive
    Gemstone Name       Color    Hardness   Gravity      Index
    
    Alabaster            GREY       2         2.32        1.53
    Bakelite             GREY       2.5       1.28        1.65
    Calcite              GREY       3         2.70        2.71
    Casein               GREY       2.5       1.33        1.55
    Celluoid             GREY       2         1.35        1.50
    Ivory                GREY       2.5       1.80        1.54
    Marble               GREY       3         2.71        1.50
    Meerschaum           GREY       2         1.50        1.53
    Nephrite             GREY       3.5       3.00        2.96
    

    Very Important Reminder: The course syllabus states, "...since a primary goal of the course is to enable students to develop their own programs, collaboration is not allowed on homework assignments, supplemental problems, or tests. In addition, students should note the department's policy regarding the role of user-consultants for Computer Science 151 ."

    Students with any questions about this policy should talk to their instructor prior to beginning this problem. Accordingly, following faculty legislation, the CSC 151 instructor will have to turn over to the Academic Standing Committee any evidence of collaboration found on any Supplemental Problems.

More Files
  1. Problem not available

    Very Important Reminder: The course syllabus states, "...since a primary goal of the course is to enable students to develop their own programs, collaboration is not allowed on homework assignments, supplemental problems, or tests. In addition, students should note the department's policy regarding the role of user-consultants for Computer Science 151 ."

    Students with any questions about this policy should talk to their instructor prior to beginning this problem. Accordingly, following faculty legislation, the CSC 151 instructor will have to turn over to the Academic Standing Committee any evidence of collaboration found on any Supplemental Problems.

Information on the 1997-1998 Iowa Senate

  1. File /u2/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-city and senators-by-zip-code, in the current working directory. The senators-by-city file should contain the same data as the source file, in the same format (including capitalization), but with the lines arranged alphabetically by city (column 4). 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.

    Very Important Reminder: The course syllabus states, "..since a primary goal of the course is to enable students to develop their own programs, collaboration is not allowed on homework assignments, supplemental problems, or tests. In addition, students should note the department's policy regarding the role of user-consultants for Computer Science 151 ."

    Students with any questions about this policy should talk to their instructor prior to beginning this problem. Accordingly, following faculty legislation, the CSC 151 instructor will have to turn over to the Academic Standing Committee any evidence of collaboration found on any Supplemental Problem.


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%.

Students are reminded that collaboration is NOT allowed on the following problems, as stated in the course syllabus and reiterated in Problem 8 above.

Determination of the Following Date

  1. Programs commonly must determine what date comes after a given one. Write a procedure next-date which returns the date which follows the specified one (e.g., April 1, 1999 follows March 31, 1999). The date returned should be formatted as a list, as in supplemental problem 4.

    If next-date is given an invalid date as a parameter, it should return an error message rather than a date.

    The following examples illustrate how next-date shoud work:

    
    (next-date 'january 8 1999)     ===> (january 9 1999)
    (next-date 'february 28 1999)   ===> (march 1 1999)
    (next-date 'february 28 2000)   ===> (february 29 2000)
    (next-date 'february 29 1999)   ===> "invalid date"
    (next-date 'december 31 1999)   ===> (january 1 2000)
    (next-date 'henry 31 2000)      ===> "invalid date"
    
Multiplication of Three-Digit Integers
  1. Write a procedure show-multiplication that has two three-digit integers as parameters and then prints their product in the following format:
    
            749
        x   381
        -------
            749
          5992
         2247
        -------
         285369
    
    Use the following cases as part of your testing:
    
    (show-multiplication 749 381)
    (show-multiplication 381 749)
    (show-multiplication 599 100)
    (show-multiplication 120 102)
    (show-multiplication 102 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 integer between 1 and 1000 from the keyboard and prints the equivalent number in Roman numerals.

Anagrams

  1. Sometimes one can simplify a problem by removing the parts that don't matter, and then looking at what's left.

    For instance if you wanted to figure out if two collections of "stuff" were the same, you might remove matching items from each collection until you see if there are items left over. If you have leftover items, the collections were different, and if both collections become empty at the same time, they areidentical.

    Use this technique to write a program which will determine whether or not two strings are anagrams of each other.

    Test it by deciding whether or not "one plus twelve" is the same as "eleven plus two" .

Game Simulation Exercise
  1. This exercise involves the completion of one of the following problems:

  1. Racquetball: Racquetball is a game played by two players on an indoor, enclosed court. Scoring proceeds as follows:
    • The score starts at 0 - 0.
    • Player A starts serving.
      • When Player A wins a volley, she scores a point and is allowed to serve again.
      • When Player A loses a volley, she loses the serve but no points are scored.
    • Player B starts serving.
      • When Player B wins a volley, she scores a point and is allowed to serve again.
      • When Player B loses a volley, she loses the serve but no points are scored.

    A player can only score points while she has the serve. A player loses the serve when she loses a volley, but no points are scored on the change of serve. Play continues until either the score is 11-0, which is a shut-out, or one player scores 21 points. (The rules do not require a player to win by two points.)

    Write a program that reads the probability of Player A winning a volley and then simulates the playing of 500 games with Player A having the first serve on each game. Record the number of wins (including shut-outs) for each player and the percentage of wins. Also record the number of shut-outs for each player.

  2. Volleyball:Volleyball is a game played on a court by two teams, separated by a net. Scoring proceeds much the same way as in racquetball (as explained above). In particular, scoring starts at 0-0. A team can only score points while it serves. A team loses the serve when it loses a volley, but no points are scored on the change of serve. Play continues until one team scores 15 points, and a team must win by at least two points (if the score is 15-14, play must continue until one team leads by 2 points). There is no special rule for ending a game due to a shut-out.

    Write a procedure that has as parameter the probability of Team A winning a volley and then simulates the playing of 500 games with Team A having the first serve on each game. The procedure should print the number of wins for each team and the percentage of wins. The procedure also should print the number of shut-outs for each team.

    Hint: Since the flow of activity in this problem is a bit complex, you might write an initial outline describing the overall flow of work for the simulation and/or within a game. Then write main procedures which follow this overall structure and which call other procedures to handle specific details. For example, an overall simulation procedure for the entire 500 games might call an individual game procedure to find the result of each game. The overall simulation procedure then would only have to tabulate results -- the individual game procedure would handle details of a game.

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:
    
    Albuquerque
    Bernalillo
    New Mexico
    1891
    331767
    247
    323935
    14
    5
    
    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.

Files for the Placement Program

  1. The expert systems lab describes a program which provides a tentative placement of incoming college students for their first mathematics and computer science courses.

    When this system is used each year to make recommendations to incoming students, the Registrar's Office sends the Department of Mathematics and Computer Science a file of student transcript information. A fictional file of this type is available in ~walker/261/labs/student.raw-data.

    Since the expert system is written in LISP, which is list-oriented, the placement program takes input from a file containing data for each student as a separate list. For example, a typical entry might be:

    ("Person I M A" ( (ACT 28) (SemOfEng 8)(SemOfMath 8)
             (Grades 2.75)(SemOfPCalc 1)(PCalcGrades 3.00)
             (SemOfCS 2)(CSGrades 4.00) (TSemUnits 36) ) 
         (Campus-Box |20-01| ) (Adviser "My Friend") )
    
    The full file for the above fictional students is available in ~walker/261/labs/student.data. Here, the person's name is the first entry in the list. The second list component is a list of attributes. The person's mailbox and advisor, when known, are the remaining elements of the main list.

    Within the fictional student file, it is worthwhile to note several characteristics which add complexity to this placement problem:

    Such variations are common in applications involving expert systems.

    Note that the list of attributes is a list of pairs, where each entry (e.g., (ACT 28)) gives the type of information (e.g., ACT first, followed by the data). Such a list of pairs is called an association list, and such lists are a particularly common mechanism for storing data within expert systems. The last part of the lab on pairs describes one common Scheme procedures for processing data within an assocation lists.

    This example of files illustrates a common circumstance in computing, where data from one application (e.g., the Registrar's file) comes in one format, while data for another application (e.g., the placement program) requires a second format.

    Problem: Write a program which reads a data file in the Registrar's format and produces a corresponding file in the list-oriented format.


This document is available on the World Wide Web as

http://www.math.grin.edu/~walker/courses/151.sp99/suppl-prob.html

created February 6, 1997
last revised November 4, 1999