CSCI 3333
Data Structures
Summer 2008
Homework #3 Possible Solution
The following is a possible solution in Perl, since I do not have time to develop Java/C++ solutions.
#!/opt/gnu/bin/perl
# Create Jumbo games: part #1.
# Create files of words made of unique set of characters.
# All unique words of length 5 or below go to the file word_set1.txt
# All unique words of length 5 or above go to the file word_set2.txt
# All non-unique words go to the file other_word_set.txt
open(DICT, "words.txt") || die "Can't open words.txt";
while ($word = <DICT>)
{ chop($word);
# remove single-character words, capitalized words
# and words with a '.
next if ($word =~ /^.$/ || $word =~ /^[0-9A-Z]/ || $word =~ /'/);
$key = join(//,sort(split(//, $word)));
if ($chset{$key})
{ if ($chset{$key} != -1) {
# word is not unique. Store the first word with the same key in @nonuniqueSet.
push(@nonuniqueSet, $chset{$key});
$chset{$key} = -1;
}
# Store the current word with the same key in @nonuniqueSet.
push(@nonuniqueSet, $word);
}
else
{ $chset{$key} = $word;
}
}
close(DICT);
foreach $key (keys %chset)
{ if ($chset{$key} == -1) {
next;
}
if (length($key) >= 5)
{ push(@word_set2, $chset{$key});
}
else
{ push(@word_set1, $chset{$key});
}
}
@nonuniqueSet = sort(@nonuniqueSet);
open(OTHER_WORD_SET, ">other_word_set.txt") || die "Can't open other_word_set.txt" ;
while (@chset)
{ $_ = shift(@nonuniqueSet);
print OTHER_WORD_SET "$_\n";
}
close(OTHER_WORD_SET);
@word_set1 = sort(@word_set1);
open(WORD_SET1, ">word_set1.txt") || die "Can't open word_set1.txt" ;
while (@word_set1)
{ $_ = shift(@word_set1);
print WORD_SET1 "$_\n";
}
close(WORD_SET1);
@word_set2 = sort(@word_set2);
open(WORD_SET2, ">word_set2.txt") || die "Can't open word_set2.txt" ;
while (@word_set2)
{ $_ = shift(@word_set2);
print WORD_SET2 "$_\n";
}
close(WORD_SET2);
exit 0;