Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Yet Another Rosetta Code Problem (Perl, Ruby, Python, Haskell, ...)

by Limbic~Region (Chancellor)
on Aug 14, 2007 at 00:33 UTC ( #632352=note: print w/ replies, xml ) Need Help??


in reply to Yet Another Rosetta Code Problem (Perl, Ruby, Python, Haskell, ...)

eyepopslikeamosquito,
BTW, does anyone know of a CPAN module that can solve this problem directly?

It is hard to imagine why this specific problem would have its own solution.

The problem can be generalized into something worth solving. Assume we want to divide a list on break points. I will present an iterator solution.

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; sub gen_divider { my %opt = @_; my $is_break = $opt{detect_break}; my ($curr, @list); return sub { for (@_) { if (! defined $curr || $is_break->($_, $curr)) { push @list, [$_]; $curr = $_; } else { push @{$list[-1]}, $_; } } return \@list; }; } my $break_point = sub { my ($item_to_test, $prev_break_point) = @_; return $item_to_test ne $prev_break_point; }; my $divide = gen_divider(detect_break => $break_point); for (split //, "ZBBBCZZ") { $divide->($_); } my $list = $divide->(); print Dumper($list);
This leaves a lot to be desired but the idea jumped into my head and so I thought I would share.

Cheers - L~R


Comment on Re: Yet Another Rosetta Code Problem (Perl, Ruby, Python, Haskell, ...)
Download Code
Re^2: Yet Another Rosetta Code Problem (Perl, Ruby, Python, Haskell, ...)
by Anonymous Monk on Sep 11, 2007 at 00:54 UTC
    Ruby solution
    x = [] "ZBBBCZZ".scan(/((.)\2*)/){ x << [$~[0]]; x.flatten!}
    .flatten is needed else you get
    # => [["Z"], ["BBB"], ["C"], ["ZZ"]]
    as result.

    But this is quite an ugly solution anyway... in fact, perl looks almost as readable in this example ;)

    not sure how to solve this any easier though, hmm i wonder if .each could be used and then another grouping way... I really dont like the regex magic of the ruby solution, cant you guys think of another solution :-)

      Oh and btw this comment box sucks! [[ newlines should become html br tags! :(
        Oh and btw this comment box sucks! [[ newlines should become html br tags! :(
        The comment box accepts a limited subset of HTML (with some added Perlmonks-specific markup).
        Newlines (and other whitespace) don't behave in HTML as your describe.

        This isn't your bff's blog or your mum's guestbook. :-)

        If you were logged in, you could update your post to look a little nicer.

        -David

      I really dont like the regex magic of the ruby solution, cant you guys think of another solution :-)
      How about some inject magic?
      x = s.split(//).inject([]) {|a,e| (a.last && a.last[e] ? a.last : a) < +< e; a}

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://632352]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (9)
As of 2014-07-31 21:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (253 votes), past polls