Supplemental Problems extend the range of problems considered in the course and help sharpen problem-solving skills. Problems number 8 or higher may be turned in for extra credit.
Format: In turning in any programs for the course, please follow these directions:
;;; 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.
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)))
)
)
)
List Endings
Fibonacci Numbers
In mathematics, the Fibonacci sequence is a sequence of positive integers,
as follows.
the first two numbers of the sequence are both 1's.
endings that
takes any non-empty list ls and returns a list of all of the
non-empty ending lists of ls.
(endings '(a b c d e)) ===> ((a b c d e) (b c d e) (c d e) (d e) (e))
(endings '(5 12 12 0)) ===> ((5 12 12 0) (12 12 0) (12 0) (0))
(endings '(first second)) ===> ((first second) (second))
(endings '(only)) ===> ((only))
Thus, the first 10 elements of the Fibonacci sequence are: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55
Write and test a Scheme procedure Fibonacci which takes a parameter n and returns the nth Fibonacci element in the sequence. Also, write a test a procedure Fib-seq which takes a parameter n and returns a list containing the first n elements of the Fibonacci sequence.
(Fibonacci 10) ===> 55 (Fib-seq 10) ===> (55 34 21 13 8 5 3 2 1 1)Processing Dates
For this problem, dates will have three parts:
month day year,
where the month and day are integers and the
month is a symbol (e.g., january, february, etc.).
Write the following procedures:
year-end? which returns true if and only if a date ends a year (e.g., December 31, 1998)
Notes:
January, March, May, July, August, October, and December have 31 days.
Examples: These procedures should produce the following results:
(year-end? 'march 31 1999) ===> #f (year-end? 'december 30 1999) ===> #f (year-end? 'december 31 1999) ===> #t (month-end? 'january 8 1999) ===> #f (month-end? 'january 31 1999) ===> #t (month-end? 'february 28 1999) ===> #t (month-end? 'feburary 28 1999) ===> #f (month-end? 'february 28 2000) ===> #f
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:
(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
)
)
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".
(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.
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:
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).
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).
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.
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.
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.
Hardness often is measured on the (nonlinear) Mohs Scale, which associates a hardness number to each mineral, from 1 (softest) to 10 (hardest):
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/flynt/public_html/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 that will let you extract the names of gemstones of a certain color and at least a particular hardness.
If this program is invoked as:
(find-by-color-and-hardness 8 "RED" "/home/flynt/public_html/gems.txt")
it should return:
Diamond RED 10 3.52 2.42
Topaz (yellow) RED 8 3.53 1.63
Spinel RED 8 3.60 1.72
Spinel (synth) RED 8 3.63 1.73
Chrysoberyl RED 8.5 3.71 1.75
Corundum RED 9 3.99 1.77
Painite RED 8 4.01 1.80
Cubic Zirconia RED 8 5.75 2.15
Extra credit: Display only the name, color and hardness fields, and left justify the name, instead of right justifying it:
Diamond RED 10 Topaz (yellow) RED 8 Spinel RED 8 Spinel (synth) RED 8 Chrysoberyl RED 8.5 Corundum RED 9 Painite RED 8 Cubic Zirconia RED 8
In 1843 Gregor Mendel noticed that certain traits were inherited among the pea plants he was growing. Over the next 25 years he kept careful records of the appearance of parent plants and their offspring, and in 1866 published a paper describing how these traits were controlled by "factors" that could be inherited.
Mendel determined that a factor (what we now call a gene) has two components. A child gets one component from each of its parents.
A gene can either be "Dominant" in which case it will control how a trait is expressed, or "Recessive", in which case the expression of the trait may be controlled by the other gene.
For example, Gregor Mendel noticed that some pea plants are tall, and some are short. If he crossed two short plants, he always got short offspring. If he crossed two tall plants, he sometimes got all tall offspring, as you'd expect, but sometimes a short plant would appear among the offspring. Often, 3/4 of the offspring would be tall, while 1/4 were short.
Mendel determined that there was a gene for tallness, and that the grow-tall gene was dominant. If one of the genes in a plant was the dominant form of that gene, the plant grew tall. Only when both of the genes were for shortness would the plant be short.
We denote the dominant form of a gene with a capital letter, and the recessive form with a small letter. The possible combinations of Tallness genes are:
The combination of genes is an individual's genotype. The way a pair of genes is expressed controls an individual's physical appearance. We call the individual's appearance its phenotype.
If Gregor crossed one of the plants with two dominant genes with a plant with two recessive genes the offspring would each have one dominant Tall gene and one recessive short gene. So the phenotype for these children would be tall, even though the genotype was one Tall and one short gene.
We might write this cross like this:
TT x tt -> Tt - Tt - Tt - Tt
The genotype for the offspring is "Tt", while the phenotype is "Tall".
If we crossed one of these Tall offspring with a single Dominant gene for tallness with another plant with a single dominant gene and a single recessive, (two plants with a genotype of "Tt") the results would look like this:
Tt x Tt -> TT - Tt - tT - tt
Three out of 4 offspring have the "Tall" phenotype, and 1/4 would have the "Short" phenotype. One quarter of the offspring would have a genotype of two dominant genes, and one quarter of the offspring would have a genotype of two recessive genes, and one half of the offspring would have a genotype of one dominant and one recessive gene.
The Current Problem: (While the details of what follows are ficitious, the basic problem arises with remarkable frequency in practice in many business and research settings.)
In order to celebrate the 150th anniversary of the publishing of Mendel's paper, a group of researchers have been duplicating his work. They've planted several fields of peas, and counted the offspring in each field to start their records. They are counting whether or not the plants are tall or short, whether the seeds are round or wrinkled, and whether the seeds are yellow or green.
Following current modes of research, the researchers placed their records of the work within a computer database, and they systematically made a backup copy of each disk. For safety against water damage (from a sometimes leaky roof), they kept the backups in another room on a lower floor at the far end of the building. They also kept printouts of some of their data sets.
Unfortunately, a fire recently swept through much of the building, destroying both the computer system and the room with the backups. Thus, all electronic records were lost. Of course, an off-site storage location for the backups would have resolved this problem, but it is a bit late for second guessing at this point.
From paper records and from their memories, they have reconstructed the following:
While all of this raw data is available within the spreadsheet, the program they use doesn't have the capability of comparing all the experimental data with the expected ratios to find out which ones match. So, they've exported the data into two files: one with theoretical and one with experimental data. They need your help to figure out what the possible genotypes of the parents are.
Following a common format for exporting data from spreadsheets, the files have a single record on each line, and the fields are separated by commas. The file format is:
Line 1:
A header line that describes each of the phenotypes:
Short-Round-Yellow,Short-Round-Green,Short-Wrinkled-Yellow,Short-Wrinkled-Green,Tall-Round-Yellow,Tall-Round-Green,Tall-Wrinkled-Yellow,Tall-Wrinkled-Green
Line 2-N: An identifier, a value for Short-Round-Yellow, a value for Short-Round-Green, ...
The first few lines of the expected data file look like this:
Short-Round-Yellow,Short-Round-Green,Short-Wrinkled-Yellow,Short-Wrinkled-Green,Tall-Round-Yellow,Tall-Round-Green,Tall-Wrinkled-Yellow,Tall-Wrinkled-Green, tt rr yy x tt rr yy,0,0,0,1.0,0,0,0,0 tt rr yy x tt rr Yy,0,0,0.5,0.5,0,0,0,0 tt rr yy x tt rr YY,0,0,1.0,0,0,0,0,0 tt rr yY x tt rr YY,0,0,1.0,0,0,0,0,0 tt rr YY x tt rr YY,0,0,1.0,0,0,0,0,0 tt rr YY x tt rr Yy,0,0,1.0,0,0,0,0,0 tt rr YY x tt rr yy,0,0,1.0,0,0,0,0,0
For example, the first lineshows that if you have two parents who have all
recessive genes (tt rr yy x tt rr yy), all the offspring have
recessive phenotypes (Short with Wrinkled, Green seeds).
If one parent has only recessive genes for yellow, and the other has a dominant and a recessive gene, then half of the offspring will display the dominant trait, and half will display the recessive trait. (From the second line, half will be Short-Wrinkled-Yellow, and half will be Short-Wrinkled-Green.)
If one parent has two dominant genes, then the offspring will all be of the dominant phenotype. Notice that there are 5 combinations of parents that will produce offspring that all have yellow seeds.
The experimental data looks like this:
Short-Round-Yellow,Short-Round-Green,Short-Wrinkled-Yellow,Short-Wrinkled-Green,Tall-Round-Yellow,Tall-Round-Green,Tall-Wrinkled-Yellow,Tall-Wrinkled-Green, field1-0,64,60,72,48991,22,49,49,56 field2-1,29,44,24520,24518,62,33,49,7 field3-2,23,69,49023,35,27,5,48,24
It is relatively easy to convert one of these lines of data into a line of fractions. If we total up the numbers in each field (64, 60, 72, 48991, 22, 49, 49, 56) and divide each number by that total we get:
64/49363, 60/49363, 72/49363, 48991/49363, 22/49363, 49/49363, 49/49363, 56/49363
or (in decimal)
.0012, .0012, .0014, .9924, .0004, .0009, .0009, .0011
We can compare this set of fractions to the expected values by subtracting each field from the appropriate expected field and adding the absolute values of the differences. If we did it by hand, it would look like this if we compared this line to the first line of the expected data:
| 0 - .0012 | + | 0 - .0012 | + | 0 - .0014 | + | 1 - .9924 | + | 0 - .0004 | + | 0 - .0009 | + | 0 - .0011 |
This returns a value that represents the absolute difference between the expected data and the experimental data. The lower this value is, the closer the match between the experimental and theoretical data. (While there are several techniques for comparing sets of numbers, the absolute value is the simplest, and works well for this application.)
For this problem you should write a program that can accept a list of fractions, and compare that list to the lists of fractions in the theoretical data file and return the best match. If several theoretical ratios give the same best match, your program may return any of the most likely parents.
If your program is named find-parents, it might be invoked
like this:
(find-parents "/home/flynt/public_html/GeneticTheo" .0012 .0012 .0014 .9924 .0004 .0009 .0009 .0011)
Possible Parents for: (.0012 .0012 .0014 .9924 .0004 .0009 .0009 .0011)
tt rr yy x tt rr yy,0,0,0,1.0,0,0,0,0
Test your program with:
(find-parents "/home/flynt/public_html/GeneticTheo" 0.0008 0.0004 0.49 0.49 0.0007 0.0009 0.001 0.0002) (find-parents "/home/flynt/public_html/GeneticTheo" 0.0006 0.49 0.0003 0.49 0.0006 0.0004 0.0005 0.0007) (find-parents "/home/flynt/public_html/GeneticTheo" 0.002 0.0004 0.0004 0.49 0.001 0.001 0.001 0.49) (find-parents "/home/flynt/public_html/GeneticTheo" 0.001 .0004 0.99 0.001 0.0001 0.0001 0.0001 0.0014)
For extra credit, you may do one or both of the following:
A. Extend your program, so that it returns all such best candidates for parents.
B. Write an additional program that can:
Hints:
The experimental data file is in
/home/flynt/public_html/GeneticExpt, and the theoretical
ratios are in the file /home/flynt/public_html/GeneticTheo.
Angelo Jeff 44 Creston IA 50801 Kramer Mary 37 West Des Moines IA 50265 Lundby Mary 26 Marion IA 52302-0563Thus, 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 50801A 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, "...ince 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 ."
While this policy may not have been clear to all students for previous supplemental problems, students with any questions about this problem should talk to their instructors prior to beginning this problem. Accordingly, following faculty legislation, the CSC 151 instructors will have to turn over to the Academic Standing Committee any evidence of collaboration found on Supplemental Problem 8.
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
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:
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.
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" .
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.
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.
Write a procedure which has a filename as parameter and which answers the
following questions about the cities represented in the data files.
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.
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
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:
Within the fictional student file, it is worthwhile to note several
characteristics which add complexity to this placement problem:
Note that the list of attributes is a list of pairs, where each entry
(e.g.,
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
Multiplication of Three-Digit Integers
(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"
Unusual Canceling
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)
Roman Numerals
Anagrams
Game Simulation Exercise
City Data
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.)
File Analysis
Albuquerque
Bernalillo
New Mexico
1891
331767
247
323935
14
5
A blank line follows each entry, including the last.
The answers to each of these questions should be printed neatly and clearly
by the procedure.
Files for the Placement Program
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.
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.
~walker/261/labs/student.raw-data.
("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.
Such variations are common in applications involving expert systems.(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.
http://www.math.grin.edu/~walker/courses/151.sp99/suppl-prob.html
created February 6, 1997
last revised May 1, 1999