|
[SPOILER] Perl 'Easy' Quiz of the Week #2005-1: msg#00036lang.perl.qotw.discuss
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) |
|
| <Prev in Thread] | Current Thread | [Next in Thread> |
|---|---|---|
| Previous by Date: | [SPOILER] Easy QOTW 2005-1 - Test Suite: 00036, Shlomi Fish |
|---|---|
| Next by Date: | [SPOILER] My Solutions to the Problem: 00036, Shlomi Fish |
| Previous by Thread: | Re: [SPOILER] Perl 'Easy' Quiz of the Week #2005-1i: 00036, Roger Burton West |
| Next by Thread: | [SPOILER] Perl 'Easy' Quiz of the Week #2005-1: 00036, Daniel Martin |
| Indexes: | [Date] [Thread] [Top] [All Lists] |
| News | FAQ | advertise |