Fun with DDT!

use Test::More; my $r1 = {}; my $r2 = []; my @tests = ( { in1 => [ 43, 43, 44 ], in2 => [ 43, 43 ], out => [ 44 ], }, { in1 => [ 1,1,1,1,1,2,2,2,3,3,4,5,6 ], in2 => [ 1,2,3,4,5,6 ], out => [ 1,1,1,1,2,2,3 ], }, { in1 => [ $r1, $r1, $r2 ], in2 => [ $r1, $r1 ], out => [ $r2 ], }, ); my %solutions = ( Skeeve => \&skeeve, moritz => \&moritz, moritz2 => \&moritz2, kyle => \&kyle, betterworld => \&betterworld, ); plan 'tests' => scalar keys( %solutions ) * scalar @tests; while ( my ( $name, $code ) = each %solutions ) { my $n = 0; foreach my $t ( @tests ) { $n++; is_deeply( $code->( $t->{in1}, $t->{in2} ), $t->{out}, "$name +$n" ); } } sub kyle { my ( $ref1, $ref2 ) = @_; my %h; $h{$_}++ for @{$ref1}; $h{$_}-- for @{$ref2}; my %x; return [ grep { $x{$_}++ < $h{$_} } @{$ref1} ]; } sub skeeve { my ($a, $b)= @_; my %d; ++$d{$_} foreach @$a; --$d{$_} foreach @$b; my $d= (); return [ map { ($_) x abs $d{$_} } keys %d ]; } sub moritz { my ( $ref1, $ref2 ) = @_; my @p = @{$ref1}; my @q = @{$ref2}; my ($px, $qx) = (0, 0); my @diff; while (1) { if ($qx >= @q){ push @diff, @p[$px .. @p-1]; last; } elsif ( $p[$px] == $q[$qx] ) { $px++; $qx++; } else { push @diff, $p[$px++]; } } return \@diff; } sub moritz2 { my ( $ref1, $ref2 ) = @_; my @p = @{$ref1}; my @q = @{$ref2}; my ($px, $qx) = (0, 0); my @diff; while ($qx < @q) { if ( $p[$px] == $q[$qx] ) { $px++; $qx++; } else { push @diff, $p[$px++]; } } push @diff, @p[$px .. @p-1]; return \@diff; } sub betterworld { my ( $ref1, $ref2 ) = @_; my @p = @{$ref1}; my @q = @{$ref2}; my %q; $q{$_}++ for @q; my @r = grep { --$q{$_} < 0; } @p; return \@r; }

My own solution pulled out of the <readmore>:

sub kyle { my ( $ref1, $ref2 ) = @_; my %h; $h{$_}++ for @{$ref1}; $h{$_}-- for @{$ref2}; my %x; return [ grep { $x{$_}++ < $h{$_} } @{$ref1} ]; }

I notice that Skeeve's has the bug mine had before I tested it.

I'm happily surprised at how many don't have the bug I was expecting when I wrote the [ $r1, ... ] test.

I, for one, wasn't shooting to optimize memory usage. I just wrote the first thing I thought of.


In reply to Re: Difference arrays. by kyle
in thread Difference arrays. by BrowserUk

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.