# fuzzy problem

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on
Hi all,
I have a string which contains binaries(1s and 0s). The objective is to find the maximum number of consecutive 1's/0's in the bitvector using perl. I tried couple of options to converting the string to list but it is returning wrong answer.

For exp: \$str = "1000000111111110101010100000000"

Does anyone have any clue?

Regards

## Re: fuzzy problem

bubunia2000ster@gmail.com wrote:

Please limit your line lenght ot ~75 characters as has been a proven and
time-honoured custom on Usenet for the past 3 decades.

Nice home work, but I bite:
split() the string at sequences of '1's to get all substrings of '0's.
split() the string at sequences of '0's to get all substrings of '1's.
Merge the two results, sort descending by length of the substrings, and
grab the first element.

jue

## Re: fuzzy problem

El 04/08/15 a las 17:48, jurgenex@hotmail.com escribió:

...

This approach likes me, but have the problem of the not simple split.

#!/usr/bin/perl -w

\$str = "1000000111111110101010100000000";

@zero = split /1+/, \$str;
@ones = split /0+/, \$str;

push @zero, @ones;

@merged = sort { length(\$b) <=> length(\$a) } @zero;

for \$i (@merged) {
print "\$i -> ", length(\$i), "\n";
}

__END__

00000000 -> 8
11111111 -> 8
000000 -> 6
0 -> 1
0 -> 1
0 -> 1
0 -> 1
1 -> 1
1 -> 1
1 -> 1
1 -> 1
1 -> 1
-> 0

# The problem is in the last line

Regards,

--
http://www.telecable.es/personales/gamo/
The generation of random numbers is too important to be left to chance

## Re: fuzzy problem

On 5/8/2015 23:02, gamo wrote:

this is 3 passes ! not good

## Re: fuzzy problem

You are wrong. It is 3n + log n passes.

But where did I miss the OPs requirement that the solution is time
critical?

jue

## Re: fuzzy problem

El 05/08/15 a las 22:39, George Mpouras escribió:

Yes, ok, but this is planet perl, where we orbit
far away and slower.

Seriosly, perl passes 10 cycles per the CPU where
others do it in one or two. I'm not too afraid for
problems which finish in less than 100 seconds. It
takes me much more to type that.

--
http://www.telecable.es/personales/gamo/
The generation of random numbers is too important to be left to chance

## Re: fuzzy problem

[...]

That's how split() works when it matches a pattern at the beginning/end
of the string.
However it's not a real problem because the length of 0 will always be
smaller than the lenght of any 1 or 0 sequence, therefore it will never
be returned as the result.

jue

## Re: fuzzy problem

El 06/08/15 a las 02:37, jurgenex@hotmail.com escribió:

It looks like a wrapper to filter undefs at beginning or the end is needed.

--
http://www.telecable.es/personales/gamo/
The generation of random numbers is too important to be left to chance

## Re: fuzzy problem

There are no undefs, please see the split() documentation.
There you will also find how to avoid the empty return string.

jue

## Re: fuzzy problem

El 06/08/15 a las 03:48, jurgenex@hotmail.com escribió:

Adding a space? Can't be done.

--
http://www.telecable.es/personales/gamo/
The generation of random numbers is too important to be left to chance

## Re: fuzzy problem

El 06/08/15 a las 03:48, jurgenex@hotmail.com escribió:

The confusion is not only mine.

\$ perl -E' say "ok" if ""==undef;'
ok
\$ perl -E' say "ok" if "" eq undef;'
ok

--
http://www.telecable.es/personales/gamo/
The generation of random numbers is too important to be left to chance

## Re: fuzzy problem

Why do you believe comparing something with undef will yield any
meaningful result?

See "perldoc -f defined", middle of second paragraph :-(

jue

## Re: fuzzy problem

Because it does: Just like it would handle a scalar with any other
value, Perl will also convert a scalar with no value to a suitable type
when an operation is to be performed with it. This will either result in
0 (the number) or an empty string.

NB: Checking for definedness of something is not going to work in this
way.

## Re: fuzzy problem

On 04.08.15 17:37, bubunia2000ster@gmail.com wrote:

I should think so. For help with homework---this does
look a bit like homework---please consider showing
your attempts at a solution. That's easier to follow.

## Re: fuzzy problem

On Tuesday, 4 August 2015 21:07:56 UTC+5:30, bubunia...@gmail.com  wrote:

One way would be:

perl -MList::Util=max -le '
\$_ = \$str = "1000000111111110101010100000000";
print max map { length } /11+|00+/g;
'

## Re: fuzzy problem

On 04/08/15 17:00, sharma__r@hotmail.com wrote:

Some people's minds just work in the right way for this lark.  I don't
think I could have thought of that in two days, let alone the 23 minutes
it took you.

--

Henry Law            Manchester, England

## Re: fuzzy problem

sharma__r@hotmail.com writes:

This doesn't work when the length is 1, eg, if the input string is '10'.
Using 1+ and 0+ instead does.

## Re: fuzzy problem

On Tuesday, 4 August 2015 21:42:00 UTC+5:30, Rainer Weikusat  wrote:

This doesn't work because it's not supposed to when the length is 1 !

The OP was interested in finding "consecutive" 1s/0s.

## Re: fuzzy problem

sharma__r@hotmail.com writes:

This may be your interpretation of 'consecutive' but it's not mine.

## Re: fuzzy problem

On Tuesday, 4 August 2015 22:46:44 UTC+5:30, Rainer Weikusat  wrote:

> This may be your interpretation of 'consecutive' but it's not mine.

This is from the Merriam-Webster dictionary:
definition of CONSECUTIVE
: following one after the other in a series
: following each other without interruption

Now please find a consecutive 1 in "10"?