Beefy Boxes and Bandwidth Generously Provided by pair Networks httptech
Welcome to the Monastery
 
PerlMonks  

The Name Game and Golf

by Cody Fendant (Pilgrim)
on Apr 19, 2010 at 07:22 UTC ( #835399=perlquestion: print w/ replies, xml ) Need Help??
Cody Fendant has asked for the wisdom of the Perl Monks concerning the following question:

I was musing over The Name Game, which is a string-processing algorithm as well as a song.

According to the instructions, I have written this script:

$N = $ARGV[0]; $n = $N =~ /^[AEIOU]/ ? $N : substr( $N, 1 ); @a = qw( b f m ) if $N !~ /^[BFM]/; $n = lc( $n =~ /^[BFM]/ ? substr( $n, 1 ) : $n ); printf( "%s, %s, bo-%s%s, Banana-fana fo-%s%s, Fee-fi-mo-%s%s, %s!", ( $N, $N, $a[0], $n, $a[1], $n, $a[2], $n, $N ) );

I'm sure other Monks can improve on that?

Edit: based on the input below, this is a revised version:

$N = $ARGV[0]; $n = $N =~ /^[AEIOU]/ ? $N : substr( $N, 1 ); foreach (qw( b f m )) { push( @a, ( $N =~ /^[$_]/i ? '' : $_ ) ) } $n = lc( $n =~ /^[BFM]/ ? substr( $n, 1 ) : $n ); printf( "%s, %s, bo-%s%s, Banana-fana fo-%s%s, Fee-fi-mo-%s%s, %s!\n", ( $N, $N, $a[0], $n, $a[1], $n, $a[2], $n, $N ) );

Comment on The Name Game and Golf
Select or Download Code
Re: The Name Game and Golf
by ambrus (Abbot) on Apr 19, 2010 at 09:02 UTC

    Your implementation gives a different result from the example on the website you linked to for the input Micah. Please debug it.

      Actually, you're right.

      But it says "And if the name starts with a b, f, or m, that sound simply is not repeated. (For example: Billy becomes "Billy Billy bo-illy"; Fred becomes "banana fana fo-red"; Marsha becomes "fee fi mo-arsha".)" so, what have I not understood?
      Oh wait, I get it now.

      What it means is, if the name starts with a b, f, or m, that consonant is not added if it would be doubled, e.g. Billy becomes "Billy, Billy, bo-illy, Banana-fana fo-filly, Fee-fi-mo-milly, Billy!" -- the b is dropped only at the point where it would be prefaced with another b, but not at the point where it would be prefaced with an f or an m.

      Neither of the two solutions below follows this rule, right?
Re: The Name Game and Golf
by youlose (Scribe) on Apr 19, 2010 at 10:55 UTC
    I think this is good variant:
    use strict; my $name = 'Micah'; printf <<'SONG',$name,substr $name,1; %1$s, %1$s, bo-b%2$s Banana-fana fo-f%2$s Fee-fi-mo-m%2$s %1$s! SONG
Re: The Name Game and Golf
by youlose (Scribe) on Apr 19, 2010 at 11:01 UTC
    or that variant:
    my $name = $ARGV[0] || 'Micah'; $_ = <<SONG; <X>, <X>, bo-b<X-1> Banana-fana fo-f<X-1> Fee-fi-mo-m<X-1> <X>! SONG s/<X-1>/substr $name,1/eg; s/<X>/$name/g; print;
Re: The Name Game and Golf
by youlose (Scribe) on Apr 19, 2010 at 13:25 UTC
    sorry, i don't read all instructions first time, now my variant:
    $_ = $n = $ARGV[0] || 'Micah'; $k = (/^[AEIOUY]/i and $c = substr $n,0,1) || /^[^bfm]/i || ''; $_ = <<SONG; <X>, <X>, bo-b<X-1> Banana-fana fo-f<X-1> Fee-fi-mo-m<X-1> <X>! SONG s/(\w)<X-1>/($k && $1) . $c . substr $n,1/eg; s/<X>/$n/g; print;

      No, you have the same bug as the original poster in his first piece of code.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (7)
As of 2014-04-19 18:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (483 votes), past polls