in reply to Re: Runs and Sequences
in thread Runs and Sequences

I know it looks like homework ..as I came across some similar java assignments while searching online. It isn't, it's something i just felt like doing. I read a stat that said there was some kind of record for an individual sitting there and fliiping a coin over and over, apparently the longest recorded run was 16 heads. I was curious how long (and how many tries) it took this individual. Thanks,

Replies are listed 'Best First'.
Re^3: Runs and Sequences
by blazar (Canon) on Sep 09, 2005 at 12:00 UTC
    I read a stat that said there was some kind of record for an individual sitting there and fliiping a coin over and over, apparently the longest recorded run was 16 heads. I was curious how long (and how many tries) it took this individual.
    Then, if you're also generating the sequence with Perl, which is not clear from your first post, and if you used rand to obtain random values, which is probable in case your did, then be warned that they won't be truly random. Which is the reason why modules like Math::TrulyRandom exist.

    It's not entirely clear to me what it is that you want to get, but as a general rule I suspect you may need a pattern matching like this:

    /((.)\2*)/; # slightly more general than your requirements, but shoul +dn't do harm.
    Or some variation of it that best fits your needs.

    HTH

Re^3: Runs and Sequences
by Angharad (Pilgrim) on Sep 09, 2005 at 11:47 UTC
    Well ... I could be wrong (I'm no perl guru) but .. why not first cut up the string into H's and T's, place them into an array. Now create a flag $H=0 as an example and then create a simple for loop, where you go though the array and create the appropriate if else statements to get the info you need.
    Make sense?
      Hrm, I'll try to wrap my head around that, cutting them up and where to is kind of the issue. I was hoping to do something like:
      my $last; my $count; my $toss = headsortails(); # returns either 0 or 1 (H or T) if ($toss == $last) { $count++; } else { $last = $toss; $c{$count}++; } if you had a bunch of variables like $c1, $c2
      Thought the implementation didn't really work out.