CSCI 3333
Data Structures
Summer 2008
Homework #1 Possible Solution
The following is a possible solution in Perl:
use strict;
#
# Homework #1, CSCI 3333, Summer 2008
# by Bun Yue
#
# See http://dcm.uhcl.edu/courses/csci3333/Summer2008/hw/h1.html for
# problem specification.
#
# Open dictionary file
my $dictionary = "words.txt";
open (DICT, "<$dictionary") || die "Can't open dictionary file $dictionary.\n";
# Get input characters for checking words and store them in the character array @ch
@ARGV >= 1 || die "usage: h1.pl ch1 ch2 ...\n";
my @ch = @ARGV;
# Check each array element to contain only one character each.
foreach (@ch) {
die "Arguments must be single characters.\n" if (length $_ > 1);
}
my $result = "";
my $resultCount = 0;
my $word = "";
WORD_LOOP:
while ($word = <DICT>) {
# Check each word in the dictionary.
# Remove the last character if it is a blank.
chomp $word;
foreach (@ch) {
# For each input character, if it is not found in the word,
# skip the word by going to the next iteration of the
# External loop.
next WORD_LOOP if $word !~ /$_/i;
}
# $word contains all characters. Add it to the result.
$resultCount++;
$result .= " $resultCount: $word\n";
}
close DICT;
if ($resultCount) {
print "There are $resultCount words containing all supplied characters:\n$result\n";
}
else {
print "There are no words containing all supplied characters.\n";
}
exit 0;
The code above contains documentation that is a little redundant. I include them so you may understand the Perl code better. A more plausible version:
use strict;
#
# Homework #1, CSCI 3333, Summer 2008
# by Bun Yue
#
# See http://dcm.uhcl.edu/courses/csci3333/Summer2008/hw/h1.html for
# problem specification.
#
# Open dictionary file
my $dictionary = "words.txt";
open (DICT, "<$dictionary") || die "Can't open dictionary file $dictionary.\n";
@ARGV >= 1 || die "usage: h1.pl ch1 ch2 ...\n";
my @ch = @ARGV;
foreach (@ch) {
die "Arguments must be single characters.\n" if (length $_ > 1);
}
my $result = "";
my $resultCount = 0;
my $word = "";
WORD_LOOP:
while ($word = <DICT>) {
# Check each word in the dictionary.
chomp $word;
foreach (@ch) {
next WORD_LOOP if $word !~ /$_/i;
}
# $word contains all characters. Add it to the result.
$resultCount++;
$result .= " $resultCount: $word\n";
}
close DICT;
if ($resultCount) {
print "There are $resultCount words containing all supplied characters:\n$result\n";
}
else {
print "There are no words containing all supplied characters.\n";
}
exit 0;
I do not expect you to know Perl or to understand the code above. The points I would like to make are two-folds:
Output for the test cases are:
>h1.pl a b c d e f
There are 3 words containing all supplied characters:
1: barefaced
2: boldface
3: feedback
>h1.pl a e i o u q
There are 14 words containing all supplied characters:
1: consequential
2: equatorial
3: equinoctial
4: equitation
5: equivocal
6: equivocate
7: grandiloquent
8: inconsequential
9: liquefaction
10: quasiorder
11: quasiperiodic
12: questionnaire
13: sequestration
14: Sequoia
>h1.pl a b c d e f g
There are no words containing all supplied characters.
>h1.pl w x y
There are 3 words containing all supplied characters:
1: expressway
2: taxiway
3: waxy
>
h1.pl x y w
There are 3 words containing all supplied characters:
1: expressway
2: taxiway
3: waxy
Additional test cases:
>h1.pl d a t s r u c e
There are 3 words containing all supplied characters:
1: disturbance
2: transconductance
3: transducer
>h1.pl q z
There are 10 words containing all supplied characters:
1: quartz
2: quartzite
3: quetzal
4: Quezon
5: quiz
6: quizzes
7: quizzical
8: squeeze
9: Vasquez
10: Velasquez
>h1.pl a s t r e g m
There are 12 words containing all supplied characters:
1: bremsstrahlung
2: ferromagnetism
3: gastronome
4: heterogamous
5: magisterial
6: magistrate
7: manslaughter
8: metallurgist
9: ringmaster
10: smattering
11: spectrogram
12: stratagem