in reply to Runs and Sequences

Here's a kinda fun solution. Can you see how it works?

use warnings; use strict; my $flips = 'TTHHHHTHHHHHHTHT'; my $flops = substr $flips, 1; my ($maxT, $maxH, $side) = (0, 0, substr $flops, 0, 1); my $toggles = ($flips ^ $flops); $toggles =~ tr/\0\x1c/01/; my @runs = $toggles =~ /(0*1)/g; for my $run (@runs) { $maxT = length $run if $side eq 'T' && length $run > $maxT; $maxH = length $run if $side eq 'H' && length $run > $maxH; $side = $side eq 'T' ? 'H' : 'T'; } print "Longest heads run is $maxH\n"; print "Longest tails run is $maxT\n";

Prints:

Longest heads run is 6 Longest tails run is 2

Perl is Huffman encoded by design.

Replies are listed 'Best First'.
Re^2: Runs and Sequences
by twofish (Initiate) on Sep 09, 2005 at 13:06 UTC
    Neat, that works to. Though of course it doesn't actually generate the data, you'd have to save the output of the code i posted to a file.txt and then read that in. I'm not sure if you could cache a spetillion chars... ?

      Hey, we don't have to give you the whole answer in one hit here :)

      Exercise for the student:

      1. Add the data generation code for some given finite number of tosses
      2. Adapt the resulting code to deal with very long series

      You should reply to this with your solution. For bonus marks you should compare the execution time for this technique with other techniques that have, or may be, posted. Have fun.


      Perl is Huffman encoded by design.