This node compares approaches in the range ( O(N) .. O(N2) ).and I thought WTF? It's a statement that doesn't make sense.
You do not seem to realize that expressing a complexity in "big-Oh" notation doesn't give you more information than an asymptotic upper bound. Stating an algorithm has an O(N2) run time complexity doesn't mean the algorithm actually grows quadratically. Searching in an unsorted array, for instance, has a run-time complexity of O(N2). It also has a run-time complexity of O(N).
So, I read your post more carefully. You're using a lot of woolly words, but it doesn't seem to be more than a logical expansion of how the FAQ tells you how to intersect arrays. Your memory requirements seem to be Ω(dN), where N is the number of people involved, and d the number of days to be considered. No naïve algorithm would use more. What I don't understand is why you need to rescan the file a second time - you do store all the names anyway.
For the amount of text and code, I'm not really impressed. Here's a program that does essentially the same (it doesn't print a bounding box). About half of the code has to do with formatting, not with actual calculations. Half of the remaining half is used for reading the data.
use 5.010; use warnings; use strict; my (@book, @people); while (<DATA>) { chomp; next if /^#/ || !/\S/; my ($name, $days) = split; my @days = split /,/, $days; foreach my $day (@days) { push @{$book[$day]}, $name; } push @people, [$name, \@days]; } foreach my $info (@people) { my ($name, $days) = @$info; my %matches; foreach my $day (@$days) { my @match = grep {$_ ne $name} @{$book[$day]} or next; $matches{$day} = \@match if @match; } show $name, $days, \%matches; } sub show { my ($name, $days, $matches) = @_; printf "%-20s %s\n", $name, join ", ", @$days; unless (%$matches) { print " Sorry! No matches.\n"; } else { foreach my $day (@$days) { foreach my $name (@{$$matches{$day} || []}) { print " $name can meet you on the $day"; given ($day) { when (/1[0-9]$/) {print "th"} when (/1$/) {print "st"} when (/2$/) {print "nd"} when (/3$/) {print "rd"} default {print "th"} } print ".\n"; } } } print "\n"; } __DATA__ # name days Al 1,2 Bob 2,13 Chuck 12,4 Dick 3,7,30 Edgar 5,7 Fred 2,23 Greg 4,5,12 Harry 6 Ian 1,20 Jack 4,23
In reply to Re: Gay Dating; or, Thrifty Set Intersections with an Accumulator
by JavaFan
in thread Gay Dating; or, Thrifty Set Intersections with an Accumulator
by Xiong
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |