Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: (Golf) Fibonacci Strings

by abstracts (Hermit)
on Jul 20, 2001 at 00:35 UTC ( [id://98224]=note: print w/replies, xml ) Need Help??


in reply to (Golf) Fibonacci Strings

Hello,

Do I need to explain how it works?

Aziz,,,

#!/usr/bin/perl -w
use strict;
print `cat $0`;
print "\nOUTPUT:\n";
while(<DATA>){
   chomp;
   print "$_\n" if is_fibo($_);
}
sub is_fibo {
   my ($str,$r,$s) = (shift, -1, 0);
   ($r,$s) = ($s,$r+$s+1)
      while $str =~ s#^((.)\2{$s})## and $str !~ /^\Q$2\E/;
   return not length $str;
}
__DATA__
Pass
abaabbbaaaaabbbbbbbbaaaaaaaaaaaaa
a aa   aaaaa
@!%%+++*****
Isee
Fail
aaaaaaaaaaaa
abbcccddddd
ab

OUTPUT:
Pass
abaabbbaaaaabbbbbbbbaaaaaaaaaaaaa
a aa   aaaaa
@!%%+++*****
Isee
ab

Replies are listed 'Best First'.
Re: Re: (Golf) Fibonacci Strings
by dragonchild (Archbishop) on Jul 20, 2001 at 01:43 UTC
    This is just a golfing of abstract's neat concept. It comes in at 80 chars. Unfortunately, it allows 'ab' to pass, which it shouldn't. I tried to fix it, but couldn't find a way around the s/^((.)\2{$s})// quotemeta thing.

    ($_,$r,$s)=(pop,-1,0);($r,$s)=($s,$r+$s+1)while s#^((.)\2{$s})##&&!/^\Q$2\E/;!$_

      This will fail for strings like: abaa0. That's why the length test was needed.
         MeowChow                                   
                     s aamecha.s a..a\u$&owag.print

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (2)
As of 2024-04-26 04:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found