Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
All,
My 3 solutions are below. The first two were not created with the memory challenge in mind. In the first, I was trying to show ikegami that if you turned the problem on its side, a merge algorithm would work.

This uses the general purpose merge algorithm iterator I created at Merge Algorithm (Combining Sorted Lists). I discovered several bugs 3 years later but they are fixed in the version below.

#!/usr/bin/perl use strict; use warnings; use List::Util 'first'; sub gen_merger { my ($list, $fetch, $compare, $finish) = @_; my @item = map $fetch->($_), @$list; my $done; return sub { return $finish if $done; my $idx = first {$item[$_] ne $finish} 0 .. $#item; my $next = $item[$idx]; for ($idx + 1 .. $#item) { next if $item[$_] eq $finish; my $result = $compare->($next, $item[$_]); if ($result == 1) { $next = $item[$_]; $idx = $_; } } $item[$idx] = $fetch->($list->[$idx]); $done = 1 if ! first {$_ ne $finish} @item; return $next; }; } sub gen_sum_iter { my ($num, $list) = @_; my $idx = 0; return sub { return undef if $idx > $#$list; return $num + $list->[$idx++]; }; }; my @list1 = (1 .. 5); my @list2 = (1 .. 5); my $finish = 'A val that is guaranteed not to be present in any list'; my @merge_list = map gen_sum_iter($_, \@list2), @list1; # Map over sma +ller list if possible my $fetch = sub {my $sum = $_[0]->(); return defined $sum ? $sum : $fi +nish;}; my $compare = sub {$_[0] <=> $_[1]}; my $next = gen_merger(\@merge_list, $fetch, $compare, $finish); while (1) { my $item = $next->(); last if defined $item && $item eq $finish; print "$item\n"; }

In this version, I realized that the iterators were not needed. They really didn't reduce complexity and there is obviously the performance penalty of invoking subs repeatedly.

#!/usr/bin/perl use constant VAL => 0; use constant IDX => 1; use constant SUM => 2; use strict; use warnings; my @list1 = (1 .. 5); my @list2 = (1 .. 5); my @merge_list = map {[$_, 0, $_ + $list2[0]]} @list1; # Map over smal +ler list if known while (@merge_list) { my ($min, $index) = ($merge_list[0][SUM], 0); for (1 .. $#merge_list) { ($min, $index) = ($merge_list[$_][SUM], $_) if $merge_list[$_] +[SUM] < $min; } print "$min\n"; $merge_list[$index][IDX]++; if ($merge_list[$index][IDX] > $#list2) { splice(@merge_list, $index, 1); } else { $merge_list[$index][SUM] = $merge_list[$index][VAL] + $list2[$ +merge_list[$index][IDX]]; } }

This is where I realized it could be turned into a challenge since only 1 of the 3 tracking values was necessary. Caching the sum improves performance but what are a few cycles amongst friends. I also realized that keeping track of the value in @list1 was also just a caching technique. With it gone, I was left with 2N + M along with about 4 constant scalars.

#!/usr/bin/perl use strict; use warnings; my @list1 = 1 .. 5; my @list2 = 1 .. 5; my @merge_list = ((0) x scalar @list1); # use smaller of 2 lists while (1) { my ($min, $idx); for (my $i = 0; $i < @merge_list; ++$i) { next if $merge_list[$i] > $#list2; my $sum = $list1[$i] + $list2[$merge_list[$i]]; ($min, $idx) = ($sum, $i) if ! defined $min || $sum < $min; } last if ! defined $min; print "$min\n"; $merge_list[$idx]++; }

Cheers - L~R


In reply to Re: Challenge: Sorting Sums Of Sorted Series by Limbic~Region
in thread Challenge: Sorting Sums Of Sorted Series by Limbic~Region

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2024-04-18 22:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found