[SPOILER] Perl 'Easy' Quiz of the Week #2005-1
"Brad Greenlee" <[email protected]> Mon, 24 Jan 2005 12:00:58 -0500 (EST)
| Newsgroups | gmane.comp.lang.perl.qotw.discuss |
|---|---|
| Message-ID | <[email protected]> |
My version's memory usage is limited to the size of all the entries for a
given prefix, rather than the size of a single line. I figured that since
there can only be a maximum of 26 lines with a given prefix (and judging
from the sample data, probably much fewer), it was better to make
something that was more straightforward than maximally miserly with
memory.
Handling the variable number of arguments was easy, so I threw that in
there.
My algorithm is simple:
- split each line into prefix and suffix (I initially used a regex,
but since the problem description seemed to indicate that the data was
clean, split is much faster)
- keep all the suffixes for a given prefix in a hash
- when the prefix changes, dump all the values in the suffix hash,
making sure 'M' is one of them
I wrote up a python version too, since I'm trying to learn it. If any
python people out there have any tips for improving what I have, I'd love
to hear them.
Brad
----------------------------------------------------------
#!/usr/bin/perl -w
use strict;
if ($ARGV[0]) {open(STDIN, "<$ARGV[0]") or die "$!"}
if ($ARGV[1]) {open(STDOUT, ">$ARGV[1]") or die "$!"}
my %suffix_hash = ();
my $cur_prefix = '';
while (<STDIN>) {
chomp;
my ($prefix,$suffix) = split /\./;
if ($prefix eq $cur_prefix) {
$suffix_hash{$suffix}++;
}
else {
$suffix_hash{M}++ if %suffix_hash;
print map {"$cur_prefix.$_\n"} sort keys %suffix_hash;
%suffix_hash = ($suffix => 1);
$cur_prefix = $prefix;
}
}
$suffix_hash{M}++ if %suffix_hash;
print map {"$cur_prefix.$_\n"} sort keys %suffix_hash;
----------------------------------------------------------
#!/usr/bin/python
import sys
import fileinput
if len(sys.argv) > 2:
sys.stdout = open(sys.argv[2],'w')
del sys.argv[2] # remove from argv so fileinput doesn't pick it up
suffix_dict = {}
cur_prefix = ''
for line in fileinput.input():
prefix, suffix = line.rstrip().split('.')
if prefix == cur_prefix:
suffix_dict[suffix] = 1
else:
if len(suffix_dict) > 0:
suffix_dict['M'] = 1
for s in suffix_dict.keys():
print "%s.%s" % (cur_prefix, s)
suffix_dict = {suffix:1}
cur_prefix = prefix
if len(suffix_dict) > 0:
suffix_dict['M'] = 1
for s in suffix_dict.keys():
print "%s.%s" % (cur_prefix, s)