in reply to Date Array Convolution

Here's my crack at this interesting and deceptively difficult problem:

#! perl -slw use strict; use Data::Dump qw[ pp ]; sub dt2int { my( $d, $h, $m ) = unpack '(A2)*', $_[0]; return ( ( $d - 1 ) * 24 + $h ) * 60 + $m; } sub int2dt { sprintf "%02d%02d%02d", int($_[0]/1440)+1, int($_[0]/60)%24, $_[0] +%60; } sub adjacentPairs (&@) { my $code = shift; map { $code->( shift(), $_[0] ); } 1 .. @_; } my @listone = (['010000','010010',2],['010200','010210',5],['012359','020001',3] +); my @listtwo = (['010005','010015',1],['010207','010211',4]); my @res = adjacentPairs{ defined( $_[1] ) && $_[0][4] > $_[1][3] ? [ $_[0][0], int2dt( $_[1][3] - 1 ), $_[0][2] ] : [ @{ $_[0] }[ 0 .. 2 ] ] } sort { $a->[ 3 ] <=> $b->[ 3 ] } map { my $s = dt2int( $_->[ 0 ] ); my $e = dt2int( $_->[ 1 ] ); my @out; while( int( $s / 1440 ) != int( $e / 1440 ) ) { my $newe = ( $s + 1440 ) % 1440; push @out, [ int2dt( $s ), int2dt( $newe ), $_->[2], $s, $newe + ]; $s = $newe +1; } ( @out, [ int2dt( $s ), $_->[1], $_->[2], $s, $e ] ); } @listone, @listtwo; pp \@res; __END__ c:\test>junk33 [ ["010000", "010004", 2], ["010005", "010015", 1], ["010200", "010206", 5], ["010207", "010211", 4], ["012359", "012359", 3], ["020000", "020001", 3], ]

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^2: Date Array Convolution
by alanonymous (Sexton) on Nov 04, 2011 at 01:02 UTC
    This is super close to being exactly what I need! I tweaked the input arrays to test the borders of feasibility of inputs and am breaking things now. It is possible for a window to fall entirely within another, and depending on the V value, can change the behavior a little. If the input arrays are, for example:
    my @listone = (['010000','010110',6],['010200','010210',5],['012350',' +020012',3]); my @listtwo = (['010005','010015',1],['010207','010211',4],['012355',' +020003',1]);
    Also, I'm still digesting your code and trying to figure out how it works. Still kinda new to perl :) Thank you for the help!!!
      It is possible for a window to fall entirely within another, and depending on the V value, can change the behavior a little.

      1) T'would've been nice if the example data reflected that; 2) you don't define how the behaviour changes?

      These two input ranges become?:

      2350......0000......0010... 3333333333333333333333 11111111 option 1 - smaller range disappears and the larger becomes 2 ranges 3333333333 333333333333 option 2 - they becomes 4 ranges 3333 11111 1111 333333333 option 3 - other?

      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
        I'm sorry for the ambiguity in the initial example! The behavior doesn't 'change' per say, and the initial statement is still true, but the example didn't reflect all of the possible examples. For example, if two windows have an overlapping period, it is possible for the smaller duration one to have a lower value. This would result in 3 separate listed windows, a piece of the longer, the shorter, then the longer again. Words are difficult to describe it, so:

        Example 1:
        Window 1: 66666666666666666666666666
        Window 2:           111111
        Result would be:
        Window 3: 66666111111666666666666666

        And at any point within those window could be a 24 hour daybreak, so if there was a day break directly in the middle of the 1's, the final window would be:

        66666   111   111   666666666666666

        Sorry if the spaces don't line up properly ... it's my ghetto & nbsp at work. Also, thank you again for all of the help and understanding!

        -Alan

        *******Edit:
        I forgot to explicitly say option 2!