Charles Lu wrote:
> Hi
>
> I am trying to think of a quick and simple way to find out if a number
> appears only in consecutive fashion once. In other words, a function that
> will return true if a particular number in a list appears only consecutively
> once.
>
> For example: Does "2" appear only in consecutive fashion?
>
> (1,2,2,2,3) = True; (1,2,2,3,4,2) = False
>
> (1,2,2,3,4,2,2,) = False <-- consecutive but appears more than once
>
> (1,2,3,4,5,2,2,2,2,2) = False ( see previous reason)
>
> (1,2,3,4) = true <--- appearing only once is okay
>
> I would appreciate any help with this problem. thanks alot
>
> charles
Will the incoming list be sorted, is (1, 10, 10, 10, 3) true.
If this is the case this should work for you
#!/usr/bin/perl -w
use strict;
sub iscons {
my @check;
my $last = shift;
foreach (@_) {
return 0 if ($check[$_]);
$check[$last]++ if ($_ != $last);
$last = $_;
}
return 1;
}
print "iscons returned ", iscons (1,2,2,2,3), "\n";
print "iscons returned ", iscons (1,2,2,3,4,2), "\n";
print "iscons returned ", iscons (1,2,2,3,4,2,2), "\n";
print "iscons returned ", iscons (1,2,3,4,5,2,2,2,2,2), "\n";
print "iscons returned ", iscons (1,2,3,4), "\n";
print "iscons returned ", iscons (1, 10, 10, 10, 3), "\n";
HTH,
Sudarsan
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]