CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Fun With Perl: Perl Golf:
Perl Golf to translate to Pig Latin

 



rGeoffrey
User

Oct 15, 2001, 3:23 PM

Post #1 of 8 (35309 views)
Perl Golf to translate to Pig Latin Can't Post

Recently a question was asked in one (or was it more) of the other sections about converting a bunch of text to Pig Latin. And as we have not had a quiz here for quite some time, it is a chance to get MShareef an answer and have some fun here.

-----------------------------------------------

The rules of Perl Golf:

To solve a problem in the fewest number of characters of code.

To try to keep things at least slightly readable you will not be penalized for new lines or spaces at the beginning of a line. So please take the opportunity to indent where appropriate.

To make a level playing filed we will work from the same data set and you may only change the code between the two comment lines. Though you may change the #!/perl... line if needed. And just accept that the data is coming from a <DATA> block and live with the 6 characters as everyone else must.

-----------------------------------------------

The rules of Pig Latin, as stolen from the original post:

If the word starts with a vowel than add way to the end Otherwise everything from the beginning of the word upto (but not including) the first vowel(including 'y') is removed and added on to the end + "ay"

examples:
apple --> appleway
yellow --> ellowyay
bye --> yebay

The original post included some code, so that is the code presented below. Although it does not seem to be giving me the answers I was expecting, apple is 'wayle' and bye is 'bay', which does not seem right.

-----------------------------------------------

The rest of the rules for this contest:

The original post seems to be designed to work on exactly one word per line. That is not much fun, so please make it work on any number of words on a line. When posting your solution you only need to post the part between the comments as we will all have the same stuff outside the loop.

The winner will be the solution that uses the fewest number of charcters and converts the test data to valid Pig Latin.

The starter code is 104 characters long.

-----------------------------------------------

Here is the code to work from for this quiz:


Code
#!/usr/local/bin/perl 

#Contest Starts Here-------------------------------
while(<DATA>){
chomp;
if(/^[aeiou]/i) {print"$_"."way\n"}
elsif(/^(?:(.+?)([aeiouy]))/i){print"$2$'$1ay\n"}
}
#Contest Ends Here---------------------------------
}

__DATA__

apple
yellow
bye
Four score and
seven years ago
our forefathers
brought forth...

-----------------------------------------------

Let the games begin.




mhx
Enthusiast / Moderator

Oct 15, 2001, 10:05 PM

Post #2 of 8 (35303 views)
Re: Perl Golf to translate to Pig Latin [In reply to] Can't Post

I really hope to see some fancy answers!!!
Anyway, I'd like to note some things. But first:

Don't look at the attachment if you want to think about a solution yourself.

I think limiting the solution to the provided skeleton disables the usage of (in this case) highly effective command line switches. (As you could see in the attachment if you were looking... Wink)

Posting the correct output would be quite helpful. I haven't heard of Pig Latin up to now (as it seems it's not too popular in germany), but I think the correct solution would be:

Code
appleway 
ellowyay
yebay
ourFay orescay andway
evensay earsyay agoway
ourway orefathersfay
oughtbray orthfay...

Please correct me immediately if I'm wrong. I found it quite hard in the beginning to match the description and the examples. (From the formal description, one [read: I] could think yellow would translate into yelloway. Note there's only one w.)

However, good luck to all and happy golfing!!

-- Marcus

P.S.: And as I noticed just now as I tried to make my solution conforming with the rules, there's a typo in the example code. Just delete the closing brace right before __DATA__ .


Code
s$$ab21b8d15c3d97bd6317286d$;$"=547269736;split'i',join$,,map{chr(($*+= 
($">>=1)&1?-hex:hex)+0140)}/./g;$"=chr$";s;.;\u$&;for@_[0,2];print"@_,"



MShareef
stranger

Oct 16, 2001, 10:23 AM

Post #3 of 8 (35295 views)
Re: Perl Golf to translate to Pig Latin [In reply to] Can't Post

Here is what I have so far:
_______________________________
while(<DATA>){
$b = $_;
foreach (split(/ +/, $b)){
s/^([aeiou])(.*)/$1$2way/i or s/^(?:(.+?)([aeiouy].*))/$2$1ay/i;
print"$_ ";
}
}
_________________________________
It gives the correct output for everything but forth... which it converts to orth...fay. But I think other than that it works perfectly
Also the last print line is there only to make it look pretty.
P.S. this code won the original contest in my Advanced Topics Computer science class, so keep posting and see if you can beat it :)



mhx
Enthusiast / Moderator

Oct 16, 2001, 12:08 PM

Post #4 of 8 (35294 views)
Re: Perl Golf to translate to Pig Latin [In reply to] Can't Post

Hi MShareef,

your output looks pretty good, except - as you stated yourself - there's a problem with the dots after the last word. So I'd say there's still room for improvements. Wink
However, this code reaches a golf score of 121 (I hope I counted correctly...). The code I've attached to my last post seems to handle all cases correctly (at least all that occur in the test data) and has a score of 78 (even 66 if you change the rules a bit). If you're interested, just have a look at it.

-- Marcus


Code
s$$ab21b8d15c3d97bd6317286d$;$"=547269736;split'i',join$,,map{chr(($*+= 
($">>=1)&1?-hex:hex)+0140)}/./g;$"=chr$";s;.;\u$&;for@_[0,2];print"@_,"



chengkai
stranger

Oct 17, 2001, 9:25 AM

Post #5 of 8 (35280 views)
Re: Perl Golf to translate to Pig Latin [In reply to] Can't Post

It may not be the perfect one, but here the solution that comes out from my head

first we split out the words by using split and put them into an array, then we loop through the array by using
"for" statement. With the for loop, we apply the regex for checking the rule. So if the word start with any of the vowel [aeiou] we append the "way" after this word; if the word is not start with vowel [^aeiou] the we strip the first character and append the "ay" after it.

There should have other way to do it but I can't figure it out. At least, this one keeps the minmun code but without hurting eys too much!

while (<DATA>) {
@F = split /\s+/;
for (@F) {
$way = $1 . 'way ' if /\b([aeiou]\w+)\b/s;
$ay = $1 . 'ay ' if /\b[^aeiou](\w+)\b/s;
$words .= $way . $ay;
print $words;
($way, $ay, $words) = ('', '', '');
}
print "\n";
}

__DATA__
apple
yellow
bye
Four score and
seven years ago
our forefathers
brought forth


-------- output ------
appleway
elloway
yeay
ouray coreay andway
evenay earsay agoway
ourway orefathersay
roughtay orthay



chengkai
stranger

Oct 17, 2001, 10:11 AM

Post #6 of 8 (35278 views)
Re: Perl Golf to translate to Pig Latin [In reply to] Can't Post

As put a little thought with the previous regex, I came out another way to do the same job. This time, it will be more compact and hard to read: So I added comments in each line to make everybody to read it more easily.


while (<DATA>) {
# we need to use 1 while ( ... ) format; otherwise the regex can apply to all words in a line
#
1 while (
/\s*([aeiou]\w+)\s*(?{print $1, 'way '}) # if word start with vowel then add "way" after it
| # or
\s*[^aeiou](\w+)\s*(?{print $2, 'ay '}) # if the word that is not start with vowel then add "ay" after it
/gx # apply the regex global to the sentence
);
print "\n";
}

~Mike L.



(This post was edited by chengkai on Oct 17, 2001, 10:07 AM)


Kanji
User / Moderator

Oct 17, 2001, 8:09 PM

Post #7 of 8 (35269 views)
Re: Perl Golf to translate to Pig Latin [In reply to] Can't Post

67 ...


In Reply To

Code
#!/usr/bin/perl 
s/(\b[aeiou]\w*|\w)(\w*)/($2?$2.$1:"$1w")."ay"/egi,print for<DATA>
__DATA__
apple
yellow
bye
Four score and
seven years ago
our forefathers
brought forth...




mhx
Enthusiast / Moderator

Oct 17, 2001, 9:21 PM

Post #8 of 8 (35266 views)
Re: Perl Golf to translate to Pig Latin [In reply to] Can't Post

Looks not too bad (and is _very_ short) but IMO has two errors in its output:

Code
appleway 
ellowyay
yebay
ourFay coresay andway
evensay earsyay agoway
ourway orefathersfay
roughtbay orthfay...

which need to be 'orescay' and 'oughtbray' respectively, since the rules state:

In Reply To
Otherwise everything from the beginning of the word upto (but not including) the first vowel(including 'y') is removed and added on to the end + "ay"

But I'm sure you can come up with another solution. Wink

-- Marcus


Code
s$$ab21b8d15c3d97bd6317286d$;$"=547269736;split'i',join$,,map{chr(($*+= 
($">>=1)&1?-hex:hex)+0140)}/./g;$"=chr$";s;.;\u$&;for@_[0,2];print"@_,"


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives