Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

In All roads lead to Rome, I pointed out that different languages lead to different ways to looking at problems. One thing I didn't mention was that different types of languages can lead to different solutions. Ben Tilly's phenomenal post Why I like functional programming was a classic example of how a different type of programming background can lead to a radically different solution. In that node, he attached actions to different sections of text. That is certainly not the way I looked at the problem. Interestingly, I would no longer consider such a horrible solution because continued exposure to different ways of looking at problems leads me to learn better ways of dealing with them. Thus, this post...

Getting back to Rome, the example that I put forward was using nested loops to find all elements in one array that existed in another array. The following code snippets all assume that the two arrays being compared have unique elements.

#!/usr/bin/perl -w use strict; my @alphas = qw/ 9 4 3 2 22 13 7 140 95 278/; my @betas = qw/ 8 3 4 1 278 94 15 7 19 200/; my @results; foreach my $alpha ( @alphas ) { foreach my $beta ( @betas ) { if ( $alpha == $beta ) { push @results, $beta; } } } @results = sort { $a <=> $b } @results; print "Linear search: @results\n";

For small arrays, nested loops are fine, but they don't scale well. Two 10 element arrays require 100 iterations. Two 100 element arrays leads to 10,000 iterations. So, how do you solve the scalability issue? More directly, I am wondering how different types of languages would solve the problem of identifying elements in one array that exist in another. A natural solution in Perl might be to use a hash.

my @alphas = qw/ 9 4 3 2 22 13 7 140 95 278/; my @betas = qw/ 8 3 4 1 278 94 15 7 19 200/; my @results; my %alpha; @alpha{ @alphas } = undef; foreach my $beta ( @betas ) { if ( exists $alpha{ $beta } ) { push @results, $beta; } } @results = sort { $a <=> $b } @results; print "Hash lookup: @results\n";

A C programmer, however, might sort one list and perform a binary search. Here's how such a thing might appear if translated to Perl.

my @alphas = qw/ 9 4 3 2 22 13 7 140 95 278/; my @betas = qw/ 8 3 4 1 278 94 15 7 19 200/; my @results; my @sorted = sort { $a <=> $b } @alphas; foreach my $beta ( @betas ) { my $index = binary_search( \@sorted, $beta ); if ( defined $index ) { push @results, $beta; } } @results = sort { $a <=> $b } @results; print "Binary search: @results\n"; sub binary_search { my ( $array, $target ) = @_; my ( $low, $high ) = ( 0, scalar @$array ); while ( $low < $high ) { use integer; my $current = ( $low + $high ) / 2; if ( $array->[$current] < $target ) { $low = $current + 1; } else { $high = $current; } } if ( $low < @$array and $array->[$low] == $target ) { return $low; } else { return undef; } }

Any monks familiar with other languages and styles of programming care to offer other examples? I'd be interested in seeing those in their native language and, if possible, how you would translate those solutions to Perl.

Cheers,
Ovid

Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.


In reply to TIMTOWTDI and other languages by Ovid

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others making s'mores by the fire in the courtyard of the Monastery: (5)
    As of 2014-07-26 15:52 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My favorite superfluous repetitious redundant duplicative phrase is:









      Results (178 votes), past polls